The modclopt module implements command line option parsing via the clopt class. Both short- and long-style arguments are supported. There are also methods that aid in validating and converting arguments.
An application implements a command line parser by loading the modclopt module, subclassing the clopt class, and defining option-handling methods. At a minimum, this looks something like:
$modclopt mclass:singleton:load $myopts clopt < >< `--help' { `Help yourself' ;error_print 1 die } > cdef argv myopts:new:parse
There are five option formats. Handler methods ``names'' must be strings. The absence or presence of the ``:'', ``?'', and ``='' characters at the ends of the handler method names determines whether the options take arguments:
The -- command line argument terminates argument parsing, and any remaining arguments are returned by the parse method.
clopt subclasses vclass.
Input(s) Method Output(s) |
Description |
Class-context methods | |
- new instance |
Constructor. |
Instance-context methods | |
argv parse remainder false |
Successfully parse argv. |
argv parse true |
Unsuccessfully parse argv. |
errstr error_print - |
Print an error. |
errstr error_escape - |
Handle an error, and unwind to the parse method. |
- progname_get progname |
Return the program name. |
- argv_get argv |
Return argv. |
- index_get index |
Return the current argv index. |
- flag_get flag |
Return the current option flag. |
- arg_get arg |
Return the current option argument. |
enum arg_enum_get value |
Map the current option argument to a value. |
- arg_int_get int |
Return the current option argument as an integer. |
- arg_uint_get uint |
Return the current option argument as an unsigned integer. |
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--verbose?' - `--verbose?' { `Verbosity: ' print < $default true `yes' true `no' false > ;arg_enum_get 1 sprint } bind > cdef argv myopts:new:parse
$ ./arg_enum_get.nx --verbose Verbosity: true $ ./arg_enum_get.nx --verbose=yes Verbosity: true $ ./arg_enum_get.nx --verbose=no Verbosity: false $
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--foo?' - `--foo?' { ;arg_get null ne { `--foo --> ' print ;arg_get 1 sprint }{ `--foo\n' print } ifelse } bind > cdef argv myopts:new:parse
$ ./arg_get.nx --foo --foo= --foo=arg --foo --foo --foo --> `' --foo --> `arg' --foo $
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--foo?' - `--foo?' { `--foo --> ' print ;arg_int_get 1 sprint } bind > cdef argv myopts:new:parse
$ ./arg_int_get.nx --foo=42 --foo=+43 --foo=-44 --foo=hi --foo --> 42 --foo --> 43 --foo --> -44 arg_int_get.nx: Error parsing value `hi' for option --foo $
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--foo?' - `--foo?' { `--foo --> ' print ;arg_uint_get 1 sprint } bind > cdef argv myopts:new:parse
$ ./arg_uint_get.nx --foo=0 --foo=42 --foo=+43 --foo=-5 --foo --> 0 --foo --> 42 --foo --> 43 arg_uint_get.nx: Integer must be non-negative for option --foo $
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--foo?' - `--foo?' { `argv: ' print ;argv_get 1 sprint } bind > cdef argv myopts:new:parse
$ ./argv_get.nx --foo -- unprocessed args argv: [`./argv_get.nx' `--foo' `--' `unprocessed' `args'] $
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--bang' - `--bang' { `This is an arror string' ;error_escape } bind > cdef argv myopts:new:parse { `Parse error\n' print }{ `Parse success\n' print } ifelse
$ ./error_escape.nx Parse success $ ./error_escape.nx --bang error_escape.nx: This is an arror string Parse error $
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--bang' - `--bang' { `This is an arror string' ;error_print } bind > cdef argv myopts:new:parse
$ ./error_print.nx $ ./error_print.nx --bang error_print.nx: This is an arror string $
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--foo?' - `--foo?' { `--foo --> ' print ;flag_get 1 sprint } bind > cdef argv myopts:new:parse
$ ./flag_get.nx --foo --foo= --foo=blah --foo --> `--foo' --foo --> `--foo' --foo --> `--foo' $
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--foo?' - `--foo?' { `index: ' print ;index_get 1 sprint } bind > cdef argv myopts:new:parse
$ ./index_get.nx --foo --foo=42 --foo= index: 1 index: 2 index: 3 $
onyx:0> $modclopt mclass:singleton:load onyx:0> clopt:new onyx:1> 1 sprint -instance=$clopt- onyx:0>
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--foo?' - `--foo?' { ;arg_get null ne { `--foo --> ' print ;arg_get 1 sprint }{ `--foo\n' print } ifelse } bind > cdef argv myopts:new:parse { `Error\n' print }{ `Success. Remainder: ' print 1 sprint } ifelse
$ ./parse.nx --foo --foo=bar --foo --foo --> `bar' Success. Remainder: [] $ ./parse.nx --foo --foo=bar -- unprocessed arguments --foo --foo --> `bar' Success. Remainder: [`unprocessed' `arguments'] $ ./parse.nx --foo bang --foo Success. Remainder: [`bang'] $ ./parse.nx --foo --bang parse.nx: Error interpreting option --bang --foo Error $
#!/usr/bin/env onyx # Load the clopt module. $modclopt mclass:singleton:load # Subclass clopt and add option handlers. $myopts clopt < >< #class# #- new #instance $new { ;rnew #instance } bind #instance# #- `--foo?' - `--foo?' { `progname: ' print ;progname_get 1 sprint } bind > cdef argv myopts:new:parse
$ ./progname_get.nx --foo progname: `progname_get.nx' $