Description
Nimble paramCount and paramStr procedures behavior make it difficult to use for parsing command line arguments for tasks.
- The system.paramCount procedure returns one less than the number of arguments.
- The parameter order does not match the order specified on the command line and the order is unpredictable.
- If one or more of the arguments match the name of a task, that task is run as well.
Here are some test steps to see the behavior I'm talking about.
Create a new directory called nimbleargs and run 'nimble init' to make a new binary project:
cd code/nimbleargs
nimble init
Create an alias for nimble to n.
alias n='nimble'
Add three tasks to the nimble file:
task args, "show command line arguments":
let count = system.paramCount()+1
echo "argument count: $1" % $count
for i in 0..count-1:
echo "$1: $2" % [$i, system.paramStr(i)]
task hello, "Say hello":
echo "hello there"
task boo, "Say boo":
echo "boo"
List the tasks with the nimble tasks command.
n tasks
args show command line arguments
hello Say hello
boo Say boo
Run the boo and hello tasks:
n boo
Executing task boo in /Users/steve/code/nimbleargs/nimbleargs.nimble
boo
n hello
Executing task hello in /Users/steve/code/nimbleargs/nimbleargs.nimble
hello there
Run the args task.
n args
Executing task args in /Users/steve/code/nimbleargs/nimbleargs.nimble
argument count: 8
0: /Users/steve/.choosenim/toolchains/nim-1.0.0/bin/nim
1: e
2: --hints:off
3: --verbosity:0
4: -p:/var/folders/xq/7k90fl3j6kl1sr64jcdlwdhc0000gn/T/nimblecache
5: /Users/steve/code/nimbleargs/nimbleargs_59383.nims
6: /var/folders/xq/7k90fl3j6kl1sr64jcdlwdhc0000gn/T/nimble_59383.out
7: args
Run args with several different types of switches and arguments. Notice that the filename arguments are not last. The order of the arguments seems unpredictable.
n args -t -abc -d --long:name filename1 filename2
Executing task args in /Users/steve/code/nimbleargs/nimbleargs.nimble
argument count: 16
0: /Users/steve/.choosenim/toolchains/nim-1.0.0/bin/nim
1: e
2: --hints:off
3: --verbosity:0
4: -p:/var/folders/xq/7k90fl3j6kl1sr64jcdlwdhc0000gn/T/nimblecache
5: /Users/steve/code/nimbleargs/nimbleargs_59445.nims
6: /var/folders/xq/7k90fl3j6kl1sr64jcdlwdhc0000gn/T/nimble_59445.out
7: args
8: filename1
9: filename2
10: -b
11: -t
12: -c
13: --long:name
14: -a
15: -d
Run nimble args task and specify the hello argument. Notice both args and hello tasks run. I only expect args to run.
n args hello
Executing task args in /Users/steve/code/nimbleargs/nimbleargs.nimble
argument count: 9
0: /Users/steve/.choosenim/toolchains/nim-1.0.0/bin/nim
1: e
2: --hints:off
3: --verbosity:0
4: -p:/var/folders/xq/7k90fl3j6kl1sr64jcdlwdhc0000gn/T/nimblecache
5: /Users/steve/code/nimbleargs/nimbleargs_59485.nims
6: /var/folders/xq/7k90fl3j6kl1sr64jcdlwdhc0000gn/T/nimble_59485.out
7: args
8: hello
hello there
Run nimble args task and specify the boo and hello arguments. Notice args, boo and hello tasks run. I only expect args to run. They run in the order different than specified: args, hello, boo.
n args boo hello
Executing task args in /Users/steve/code/nimbleargs/nimbleargs.nimble
argument count: 10
0: /Users/steve/.choosenim/toolchains/nim-1.0.0/bin/nim
1: e
2: --hints:off
3: --verbosity:0
4: -p:/var/folders/xq/7k90fl3j6kl1sr64jcdlwdhc0000gn/T/nimblecache
5: /Users/steve/code/nimbleargs/nimbleargs_60367.nims
6: /var/folders/xq/7k90fl3j6kl1sr64jcdlwdhc0000gn/T/nimble_60367.out
7: args
8: boo
9: hello
hello there
boo