Command Line Parsing
Bigloo supports command line argument parsing. That is, when an application is spawn from an Unix shell, themain
function
is called and its argument is bound to the list of the command line
arguments, Module Declaration. The
args-parse
form may be used to parse these.
args-parse list rules [null-rule] [else-rule] ...bigloo syntax
The argument list is a list of strings. Rules is defined by the following grammar:<rule> ⇒ (Each elements of list are match against the rules. If one of these matches,section
<string>) | ((<option> <help>) <s-expression>) | ((<option>) <s-expression>) | ((<flag> <var> <var> ...) <s-expression>) | ((<flag> <var> <var> ... <help>) <s-expression>) <null-rule> ⇒ (()
<s-expression>) <else-rule> ⇒ (else
<s-expression>) <option> ⇒ <flag> | <string><var> <flag> ⇒ <string> | (<string>+) <var> ⇒ an identifier leaded by the?
character <help> ⇒ (help
<s-expression>) | (help
<string> <s-expression>)
args-parse
proceeds as follows:
.keep
In addition to parsing the command line arguments, args-parse
enables
help message printing.
args-parse-usage fmtbigloo procedure
This is a procedure of one argument, an boolean.Args-parse-usage
constructs an help message from all the option described in a args-parse
form. Args-parse-usage
is only defined in the <s-expression>
of an args-parse
form.
.keep
At last, if no rule matches an argument and if the args-parse
form contains an else
rule, this is evaluated. In the
<s-expression>
part of that rule, the pseudo-variable
else
is bound to the first unmatched argument and the pseudo-variable
rest
is bound to all the unmatched arguments.
Here is an example of argument parsing deploying all the possible rules:
(module args-example (main main)) (define (main argv) (args-parse (cdr argv) (section "Help") (("?") (args-parse-usage #f)) ((("-h" "--help") (help "?,-h,--help" "This help message")) (args-parse-usage #f)) (section "Misc") ((("-v" "--version") (help "Version number")) (print *version*)) (("-o" ?file (help "The output file")) (set! *dest* file)) (("--input=?file" (help "The input file")) (set! *input* file)) (else (print "Illegal argument `" else "'. Usage:") (args-parse-usage #f))))Invoking the compiled
args-example
module could produce:
> bigloo.new args.scm args.scm: > a.out toto Illegal argument `toto'. Usage: Help: ?,-h,--help -- This help message Misc: -v,--version -- Version number -o <file> -- The output file --input=<file> -- The input file