| Did you know ... | Search Documentation: |
| Pack logtalk -- logtalk-3.98.0/docs/handbook/_sources/libraries/command_line_options.rst.txt |
.. _library_command_line_options:
command_line_options
This library provides command-line parsing predicates where command-line
options are defined as objects that import the command_line_option
category. Predicates are also provided for generating help text.
Parsed options are returned by default as a list of Name(Value)
terms. A parser option, output_functor(Functor), allows returning
options as Functor(Key, Value) terms instead.
Adapted with significant changes from the SWI-Prolog optparse
library by Marcus Uneson. Most documentation, parsing options, and help
options retained from the original library. Command line options
specification changed to an object-based representation. Parsing
simplified, no longer using DCGs. Comprehensive set of tests added,
based on documentation examples. Full portability to all supported
backends.
Open the `../../apis/library_index.html#command_line_options <../../apis/library_index.html#command_line_options>`__ file in a web browser.
To load all entities in this library, load the loader.lgt utility
file:
::
| ?- logtalk_load(command_line_options(loader)).
To test this library predicates, load the tester.lgt file:
::
| ?- logtalk_load(command_line_options(tester)).
The terminology is partly borrowed from Python (see
http://docs.python.org/library/optparse.html#terminology). Arguments
are what you provide on the command line and for many Prolog systems
show up as a list of atoms Args in
current_prolog_flag(argv, Args). They can be divided into:
--.true/false for booleans filled in).
Positional arguments are used for mandatory arguments without sensible defaults (e.g., input file names). Options offer flexibility by letting you change a default setting. This library has no notion of mandatory or required options.
Define command-line options by creating objects that import the
command_line_option category. Each object represents one
command-line option and overrides the predicates to specify its
properties.
Create an object that imports command_line_option and override the
following predicates:
[v]
for -v). Default: [].[verbose] for
--verbose). Default: [].boolean, atom,
integer, float, or term. Default: term.''.''.Option objects provide two predicates for validating their definitions:
type library).term, the
default value matches the declared type.
The parse/4-5 and help/2-3 predicates automatically call
check/0 on all option objects before processing, ensuring invalid
definitions are caught early with clear error messages.
Example:
::
| ?- verbose_option::check. yes
| ?- verbose_option::valid. yes
::
:- object(verbose_option,
imports(command_line_option)).
key(verbose).
short_flags([v]).
long_flags([verbosity]).
type(integer).
default(2).
meta('V').
help('verbosity level, 1 <= V <= 3').
:- end_object.
:- object(mode_option,
imports(command_line_option)).
key(mode).
short_flags([m]).
long_flags([mode]).
type(atom).
default('SCAN').
help(['data gathering mode, one of',
' SCAN: do this',
' READ: do that',
' MAKE: fabricate numbers',
' WAIT: do nothing']).
:- end_object.
:- object(cache_option,
imports(command_line_option)).
key(cache).
short_flags([r]).
long_flags(['rebuild-cache']).
type(boolean).
default(true).
help('rebuild cache in each iteration').
:- end_object.
:- object(outfile_option,
imports(command_line_option)).
key(outfile).
short_flags([o]).
long_flags(['output-file']).
type(atom).
meta('FILE').
help('write output to FILE').
:- end_object.
% Configuration parameter without command-line flags
:- object(path_option,
imports(command_line_option)).
key(path).
default('/some/file/path/').
:- end_object.
Use the command_line_options object parse/4-5 predicates:
::
| ?- command_line_options::parse(
[verbose_option, mode_option, cache_option, outfile_option],
['-v', '5', '-m', 'READ', '-ooutput.txt', 'input.txt'],
Options,
PositionalArguments
).
Options = [verbose(5), mode('READ'), cache(true), outfile('output.txt')],
PositionalArguments = ['input.txt'].
Use the command_line_options::help/2 predicate for default
formatting:
::
| ?- command_line_options::help([verbose_option, mode_option, cache_option], Help).
Or use command_line_options::help/3 with custom help options:
::
| ?- command_line_options::help([verbose_option, mode_option], Help, [line_width(100)]).
The help/3 predicate supports the following help options:
line_width(Width): Maximum line width for help text. Default:
80.min_help_width(Width): Minimum width for help text column.
Default: 40.break_long_flags(Boolean): If true, break long flags across
multiple lines. Default: false.suppress_empty_meta(Boolean): If true (default), suppress
empty metasyntactic variables in help output.The parse/5 predicate supports the following parse options:
output_functor(Functor): When defined, options are returned as
Functor(Key, Value) terms instead of Key(Value) terms. No
default.duplicated_flags(Keep): How to handle duplicate options. One of
keepfirst, keeplast, keepall. Default: keeplast.allow_empty_flag_spec(Boolean): If true (default), options
without flags are allowed (useful for configuration parameters). Set
to false to raise errors on empty flags.term, which subsumes integer, float,
and atom. However, always specifying types is recommended for
reliable parsing and clear error messages.-sbar is taken as -s bar, not as -s -b -a -r (no
clustering).-s=foo is disallowed.keeplast (controllable via the
duplicated_flags parse option).