auto args = ["prog", "--foo", "-b"]; bool foo; bool bar; auto rslt = getopt(args, "foo|f", "Some information about foo.", &foo, "bar|b", "Some help message about bar.", &bar); if (rslt.helpWanted) { defaultGetoptPrinter("Some information about the program.", rslt.options); }
Parse and remove command line options from an string array.
Synopsis:
The getopt function takes a reference to the command line (as received by main) as its first argument, and an unbounded number of pairs of strings and pointers. Each string is an option meant to "fill" the value pointed-to by the pointer to its right (the "bound" pointer). The option string in the call to getopt should not start with a dash.
In all cases, the command-line options that were parsed and used by getopt are removed from args. Whatever in the arguments did not look like an option is left in args for further processing by the program. Values that were unaffected by the options are not touched, so a common idiom is to initialize options to their defaults and then invoke getopt. If a command-line argument is recognized as an option with a parameter and the parameter cannot be parsed properly (e.g. a number is expected but not present), a ConvException exception is thrown. If std.getopt.config.passThrough was not passed to getopt and an unrecognized command-line argument is found, a GetOptException is thrown.
Depending on the type of the pointer being bound, getopt recognizes the following kinds of options:
To set verbose to true, invoke the program with either --verbose or --verbose=true.
To set debugging to false, invoke the program with --debugging=false.
To set timeout to 5, invoke the program with either --timeout=5 or --timeout 5.
Invoking the program with "--paranoid --paranoid --paranoid" will set paranoid to 3. Note that an incremental option never expects a parameter, e.g. in the command line "--paranoid 42 --paranoid", the "42" does not set paranoid to 42; instead, paranoid is set to 2 and "42" is not considered as part of the normal program arguments.
To set color to Color.yes, invoke the program with either --color=yes or --color yes.
Invoking the program with "--output=myfile.txt" or "--output myfile.txt" will set outputFile to "myfile.txt". If you want to pass a string containing spaces, you need to use the quoting that is appropriate to your shell, e.g. --output='my file.txt'.
Invoking the program with "--output=myfile.txt --output=yourfile.txt" or "--output myfile.txt --output yourfile.txt" will set outputFiles to [ "myfile.txt", "yourfile.txt" ].
Alternatively you can set arraySep as the element separator:
With the above code you can invoke the program with "--output=myfile.txt,yourfile.txt", or "--output myfile.txt,yourfile.txt".
Invoking the program with e.g. "--tune=alpha=0.5 --tune beta=0.6" will set tuningParms to [ "alpha" : 0.5, "beta" : 0.6 ].
Alternatively you can set arraySep as the element separator:
With the above code you can invoke the program with "--tune=alpha=0.5,beta=0.6", or "--tune alpha=0.5,beta=0.6".
In general, the keys and values can be of any parsable types.
Options with multiple names
Sometimes option synonyms are desirable, e.g. "--verbose", "--loquacious", and "--garrulous" should have the same effect. Such alternate option names can be included in the option specification, using "|" as a separator:
Case
By default options are case-insensitive. You can change that behavior by passing getopt the caseSensitive directive like this:
In the example above, "--foo", "--bar", "--FOo", "--bAr" etc. are recognized. The directive is active til the end of getopt, or until the converse directive caseInsensitive is encountered:
The option "--Foo" is rejected due to std.getopt.config.caseSensitive, but not "--Bar", "--bAr" etc. because the directive std.getopt.config.caseInsensitive turned sensitivity off before option "bar" was parsed.
"Short" versus "long" options
Traditionally, programs accepted single-letter options preceded by only one dash (e.g. -t). getopt accepts such parameters seamlessly. When used with a double-dash (e.g. --t), a single-letter option behaves the same as a multi-letter option. When used with a single dash, a single-letter option is accepted. If the option has a parameter, that must be "stuck" to the option without any intervening space or "=":
To set timeout to 5, use either of the following: --timeout=5, --timeout 5, --t=5, --t 5, or -t5. Forms such as -t 5 and -timeout=5 will be not accepted.
For more details about short options, refer also to the next section.
Bundling
Single-letter options can be bundled together, i.e. "-abc" is the same as "-a -b -c". By default, this option is turned off. You can turn it on with the std.getopt.config.bundling directive:
In case you want to only enable bundling for some of the parameters, bundling can be turned off with std.getopt.config.noBundling.
Required
An option can be marked as required. If that option is not present in the arguments an exceptin will be thrown.
Only the option direclty following std.getopt.config.required is required.
Passing unrecognized options through
If an application needs to do its own processing of whichever arguments getopt did not understand, it can pass the std.getopt.config.passThrough directive to getopt:
An unrecognized option such as "--baz" will be found untouched in args after getopt returns.
Help Information Generation
If an option string is followed by another string, this string serves as an description for this option. The function getopt returns a struct of type GetoptResult. This return value contains information about all passed options as well a bool indicating if information about these options where required by the passed arguments.
Options Terminator
A lonesome double-dash terminates getopt gathering. It is used to separate program options from other parameters (e.g. options to be passed to another program). Invoking the example above with "--foo -- --bar" parses foo but leaves "--bar" in args. The double-dash itself is removed from the argument array.