I am using the following snippet to parse command line arguments and store them in a table.
var args = initTable[string, string]()
for kind, key, val in getopt():
args.add(key,val)
However, it works only if I pass =
in the command line
./mytool -i=somefile.txt
In this case, args
is {i: somefile.txt}
, which is what I want (a key:value pair).
But if I use ./mytool -i somefile.txt
then args
is {somefile.txt: , i: }
, which is definitely not what I would expect (two keys and no values).
What is the proper way of parsing arguments without using =
?
Here the printout of kind, key and val in the two cases:
$ ./diceof -a=ACTGCTGTGTGCACAGTGTCACGTGT -b=ACTGCTGTGTGCACAGTGTCACGTGa
kind:cmdShortOption
key :a
val :ACTGCTGTGTGCACAGTGTCACGTGT
kind:cmdShortOption
key :b
val :ACTGCTGTGTGCACAGTGTCACGTGa
$ ./diceof -a ACTGCTGTGTGCACAGTGTCACGTGT -b ACTGCTGTGTGCACAGTGTCACGTGa
kind:cmdShortOption
key :a
val :
kind:cmdArgument
key :ACTGCTGTGTGCACAGTGTCACGTGT
val :
kind:cmdShortOption
key :b
val :
kind:cmdArgument
key :ACTGCTGTGTGCACAGTGTCACGTGa
val :
Of course, I could check if val
is found, if not add the next key
as val
of the previous one. But I am looking for a more elegant solution.