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

7.12.1 Parsing with an Executable

The following commands are approximately all that is needed to build a shell script command line option parser from an option definition file:

 
autogen -L <opt-template-dir> test-errors.def
cc -o test-errors -L <opt-lib-dir> -I <opt-include-dir> \
        -DTEST_PROGRAM_OPTS test-errors.c -lopts

The resulting program can then be used within your shell script as follows:

 
eval `./test-errors "$@"`
if [ -z "${OPTION_CT}" ] ; then exit 1 ; fi
test ${OPTION_CT} -gt 0 && shift ${OPTION_CT}

Here is the usage output example from AutoOpts error handling tests. The option definition has argument reordering enabled:

 
test_errors - Test AutoOpts for errors
Usage:  errors [ -<flag> [<val>] | --<name>[{=| }<val>] ]... arg ...
  Flg Arg Option-Name    Description
   -o no  option         The option option descrip
   -s Str second         The second option descrip
                                - may appear up to 10 times
   -i --- ignored        we have dumped this
   -X no  another        Another option descrip
                                - may appear up to 5 times
   -? no  help           display extended usage information and exit
   -! no  more-help      extended usage information passed thru pager
   -> opt save-opts      save the option state to a config file
   -< Str load-opts      load options from a config file
                                - disabled as '--no-load-opts'
                                - may appear multiple times

Options are specified by doubled hyphens and their name or by a single
hyphen and the flag character.
Operands and options may be intermixed.  They will be reordered.

The following option preset mechanisms are supported:
 - reading file errorsRC
Packaged by Bruce (2015-08-21)
Report test_errors bugs to bkorb@gnu.org

Using the invocation,

 
  test-errors operand1 -s first operand2 -X -- -s operand3

you get the following output for your shell script to evaluate:

 
OPTION_CT=4
export OPTION_CT
TEST_ERRORS_SECOND='first'
export TEST_ERRORS_SECOND
TEST_ERRORS_ANOTHER=1 # 0x1
export TEST_ERRORS_ANOTHER
set -- 'operand1' 'operand2' '-s' 'operand3'
OPTION_CT=0

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

This document was generated by Bruce Korb on August 21, 2015 using texi2html 1.82.