14. Bigloo -- Command Line Parsing

Browsing
Home: | Bigloo A ``practical Scheme compiler'' for Bigloo version 2.5a April 2002
Previous chapter: | Macro expansion
Next chapter: | Explicit typing
Chapters
    Copyright
    Acknowledgements
    1. Table of contents
    2. Overview of Bigloo
    3. Modules
    4. Core Language
    5. Standard Library
    6. Pattern Matching
    7. Object System
    8. Threads
    9. Regular parsing
    10. Lalr(1) parsing
    11. Errors and Assertions
    12. Eval and code interpretation
    13. Macro expansion
    14. Command Line Parsing
    15. Explicit typing
    16. The C interface
    17. The Java interface
    18. Bigloo Libraries
    19. SRFIs
    20. DSSSL support
    21. Compiler description
    22. User Extensions
    23. Bigloo Development Environment
    24. Global Index
    25. Library Index
    Bibliography
| 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 [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>)
    <else-rule> ==> (else <s-expression>)
    <option>    ==> <flag>
                  | <string><var>
    <flag>      ==> <string>
                  | (<string>+)
    <var>       ==> an identifier leaded by the ? character
    <help>      ==> (help <string>)
                  | (help <string> <string>)
Each elements of `list` are match against the `rules`. If one of these matches, `args-parse` proceeds as follows: The matched argument elements of `list` are removed from the list. The `<s-expression` associated to the matching rule is evaluated in an environment where the rule variables are bound. 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-usafe` 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, a pseudo-variable `else` is bound to the unmatched argument. 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 Scribe page has been generated by `scribeinfo`.
Last update Thu Apr 25 09:40:15 2002