-
Notifications
You must be signed in to change notification settings - Fork 25
Git like subcommands
c-blake edited this page Apr 23, 2020
·
8 revisions
Your CLI app can gain git-like subcommands by using the dispatchMulti
macro.
Here's an example where the app has subcommands one and two.
- Assign a separate proc for each subcommand.
- Name the proc with the name you want to give to the subcommand (this is not necessary, but suggesting this for simplicity of this example).
- Pass the "subcommand proc" identifiers or symbols to the
dispatchMulti
.
proc one(args: seq[string]; # variable number of "non switch type" string args
foo: bool) = # a mandatory bool switch
echo "args = ", args, " foo = ", foo
proc two(boolS = true; # an optional bool switch with a default value
intS = 123) = # an optional int switch with a default value
echo "boolS = ", boolS, " intS = ", intS
when isMainModule:
import cligen
dispatchMulti(
[one],
[two]
)
Let's name the above file t.nim
. On Unix systems, on compiling it, you'll get an executable named t.
Here are some ways in which this CLI app will work with subcommands:
> ./t one --foo
args = @[] foo = true
> ./t one --foo:true
args = @[] foo = true
> ./t one -f:false
args = @[] foo = false
> ./t o --fo:off
args = @[] foo = false
> ./t one abc def -f
args = @["abc", "def"] foo = true
> ./t two
boolS = true intS = 123
> ./t two -b:false
boolS = false intS = 123
> ./t two -i:999 -b
boolS = false intS = 999
> ./t tw -bi999
boolS = false intS = 999