manpagez: man pages & more
info bigloo
Home | html | info | man
[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14 Command Line Parsing

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 section Module declaration. The args-parse form may be used to parse these.

bigloo syntax: args-parse list rules [null-rule] [else-rule] …

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.

bigloo procedure: args-parse-usage fmt

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))
       (print "Illegal argument `" else "'. Usage:")
       (args-parse-usage #f))))

Invoking the compiled args-example module could produce:

> args.scm
> a.out toto        
Illegal argument ‘toto’. Usage:

   ?,-h,–help    –  This help message

   -v,–version   –  Version number
   -o <file>      –  The output file
   –input=<file> –  The input file

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on March 31, 2014 using texi2html 5.0.

© 2000-2018
Individual documents may contain additional copyright information.