A simple package for parsing command line arguments with basic functionality. This is intended for quick arg parsing with minimum dependencies, for more complex scenarios, see alternatives like the argparse package.
The essential usage is to create a parsing schema and then parse command line arguments
var p: ParseSchema
p.initParser("my tool"):
p.addOption("-i", "--input", help="path to input", required=true, default="input.txt"):
p.addOption("-o", "--out", help="Output file")
p.addFlag(long="--flag_on", help="This flag is true")
var opts = p.parseOptions()
The help message is automatically print if --help
or -h
are parsed from command-line.
-
An argument value can be retrieved using
[]
notation on the object fromparseOptions()
, likeopts["input"]
. In this case a string is returned. The same works for positional arguments using an int likeopts[0]
. -
You can also automatically get the correct var type using the
getOpt
method on a previously declared var, likevar x: int opts.getOpt("input", x)
This will raise and exception if the value from the argument cannot be casted to the requested data type.
-
You can use
isSet
method to check if an argument was set at the command line, likeopts.isSet("input")
return true if--input
was used at the command line. Note that at the moment this check strictly so the above example returns false if the short version of the option-i
was used instead.
-
At the moment when only long option name is specified this needs to be explicit. So this works
addOption(long="--input", help="help text")
, but this does not workaddOption("--input", help="help text")
. -
The return object after parsing will contain both short and long option names if both were set.
-
No support for option with multiple values, if an option is seen multiple times during parsing this will result in an error.
-
When option are set at command line using the short name, the value need to be specified with
=
. So this worksmytool -i=input.txt
, but this does not workmytool -i input.txt
.