14. Bigloo A practical Scheme compiler User manual for version 4.0a January 2013 -- Command Line Parsing

main page

top: | Bigloo A practical Scheme compiler User manual for version 4.0a January 2013

Command Line Parsing

Chapters

Bigloo supports command line argument parsing. That is, when an application is spawn from an Unix shell, the `main` function is called and its argument is bound to the list of the command line arguments, See 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>      ==> (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>)

Each elements of `list` are match against the `rules`. If one of these matches, `args-parse` proceeds as follows:

1. The matched argument elements of `list` are removed from the list.
2. The `<s-expression>` associated to the matching rule is evaluated in an environment where the rule variables are bound.
3. The argument parsing is resumed with the rest of `list`.

In addition to parsing the command line arguments, `args-parse` enables help message printing.

args-parse-usage fmt | bigloo 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.

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

This Html page has been produced by Skribe.
Last update Thu Jan 24 17:08:36 2013.