[ < ] [ > ]   [ << ] [ 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:

 
exit 0

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

This document was generated by Bruce Korb on August 30, 2014 using texi2html 1.82.