parameter-passingnim-lang

How to properly pass arguments via command line in NIM?


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.


Solution

  • Based on the documentation for parseopt2 and the discussion in commandeer's issues (see #10), parseopt2 can set values for keys only with a = or :, other than that I don't know if there is a 'proper' way to parse option values.

    Commandeer works with options where the key and value are separated by a space by checking if the next token is a cmdArgument and assigning the value.

    var nextToken = cliTokens.pop()
    if nextToken.kind == parseopt2.cmdArgument:
      try:
        assign(nextToken.key)
      except ValueError:
        exitWithErrorMessage(getCurrentExceptionMsg())
      else:
        cliTokens.add(nextToken)