A.30.1 Notes and tips
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • The SWI-Prolog library
        • library(optparse): command line parsing
          • Notes and tips
            • opt_arguments/3
            • opt_parse/4
            • opt_parse/5
            • opt_help/2
            • parse_type/3
    • Packages
Availability::- use_module(library(optparse)).(can be autoloaded)
Source[det]opt_parse(+OptsSpec, +ApplArgs, -Opts, -PositionalArgs, +ParseOptions)
Parse the arguments Args (as list of atoms) according to OptsSpec. Any runtime arguments (typically terminated by’--') are assumed to be removed already.

Opts is a list of parsed options in the form Key(Value), or (with the option functor(Func) given) in the form Func(Key, Value). Dashed args not in OptsSpec are not permitted and will raise error (see tip on how to pass unknown flags in the module description). PositionalArgs are the remaining non-dashed args after each flag has taken its argument (filling in true or false for booleans). There are no restrictions on non-dashed arguments and they may go anywhere (although it is good practice to put them last). ParseOptions are

output_functor(Func)
Set the functor Func of the returned options Func(Key,Value). Default is the special value’OPTION' (upper-case), which makes the returned options have form Key(Value).
duplicated_flags(Keep)
Controls how to handle options given more than once on the commad line. Keep is one of keepfirst, keeplast, keepall with the obvious meaning. Default is keeplast.
allow_empty_flag_spec(Bool)
If true (default), a flag specification is not required (it is allowed that both shortflags and longflags be either [] or absent). Flagless options cannot be manipulated from the command line and will not show up in the generated help. This is useful when you have (also) general configuration parameters in your OptsSpec, especially if you think they one day might need to be controlled externally. See example in the module overview. allow_empty_flag_spec(false) gives the more customary behaviour of raising error on empty flags.