bwbox/lib/args.nim

50 lines
1.1 KiB
Nim
Raw Normal View History

2021-06-19 16:33:47 +02:00
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]
2022-05-12 17:51:55 +02:00
debug*: bool
2021-06-19 16:33:47 +02:00
2021-08-14 13:15:07 +02:00
proc getCmd*(args: Args): seq[string] =
2022-05-12 17:51:55 +02:00
return args.cmd.get(@[getEnv("SHELL", "/bin/sh")])
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 parseArgs*(): Option[Args] =
2022-05-12 17:51:55 +02:00
var args = Args(debug: false)
2021-06-19 16:33:47 +02:00
var command = newSeq[string]()
2022-05-12 17:51:55 +02:00
var parsingSandboxArgs = true
var i = 1
while i <= paramCount():
var arg = paramStr(i)
2022-05-12 17:51:55 +02:00
if arg == "--name" and parsingSandboxArgs:
args.name = some(paramStr(i + 1))
i += 2
2022-05-12 17:51:55 +02:00
elif arg == "--profile" and parsingSandboxArgs:
args.profile = some(paramStr(i + 1))
i += 2
2022-05-12 17:51:55 +02:00
elif arg == "--debug" and parsingSandboxArgs:
args.debug = true
i += 1
else:
2022-05-12 17:51:55 +02:00
parsingSandboxArgs = false
command.add(arg)
i += 1
2021-08-14 13:15:07 +02:00
if command.len > 0:
args.cmd = some(command)
2021-06-19 16:33:47 +02:00
2021-10-16 13:12:39 +02:00
if args.name.isSome or args.cmd.isSome or args.profile.isSome:
return some(args)
else:
return none(Args)