bwbox/lib/args.nim

49 lines
1.0 KiB
Nim
Raw Normal View History

2021-06-19 16:33:47 +02:00
import parseopt
import options
import os
type Args* = object
name*: Option[string]
2021-08-14 13:15:07 +02:00
cmd*: Option[seq[string]]
2021-06-19 16:33:47 +02:00
profile*: Option[string]
2021-08-14 13:15:07 +02:00
proc getCmd*(args: Args): seq[string] =
return args.cmd.get(@[getEnv("SHELL", "/bin/bash")])
2021-06-19 16:33:47 +02:00
2021-06-20 14:09:30 +02:00
proc getProfile*(args: Args): string =
2021-06-19 16:33:47 +02:00
if args.profile.isSome:
return args.profile.unsafeGet
2021-06-20 14:09:30 +02:00
return "default"
2021-06-19 16:33:47 +02:00
proc parseOpt(args: var Args, key: string, value: string): bool =
case key
2021-08-14 13:15:07 +02:00
of "name", "n":
args.name = some(value)
2021-06-19 16:33:47 +02:00
of "profile", "p":
args.profile = some(value)
else:
return false
return true
proc parseArgs*(): Option[Args] =
var p = initOptParser()
var args = Args()
2021-08-14 13:15:07 +02:00
var command = newSeq[string]()
2021-06-19 16:33:47 +02:00
while true:
p.next()
case p.kind
of cmdEnd: break
of cmdShortOption, cmdLongOption:
if p.val == "" or args.parseOpt(p.key, p.val) == false:
echo "Invalid argument ", p.val
return
of cmdArgument:
2021-08-14 13:15:07 +02:00
command.add(p.key.string)
if command.len > 0:
args.cmd = some(command)
2021-06-19 16:33:47 +02:00
return some(args)