Next: , Previous: , Up: Generating main procedures   [Contents][Index]


7.5.4.2 shell-process: emit Bourne shell results

This will produce a main() procedure that parses the command line options and emits to stdout Bourne shell commands that puts the option state into environment variables. This can be used within a shell script as follows:

unset OPTION_CT
eval "`opt_parser \"$@\"`"
test ${OPTION_CT} -gt 0 && shift ${OPTION_CT}

If the option parsing code detects an error or a request for usage or version, it will emit a command to exit with an appropriate exit code to stdout. This form of main will cause all messages, including requested usage and version information, to be emitted to stderr. Otherwise, a numeric value for OPTION_CT is guaranteed to be emitted, along with assignments for all the options parsed, something along the lines of the following will be written to stdout for evaluation:

OPTION_CT=4
export OPTION_CT
MYPROG_SECOND='first'
export MYPROG_SECOND
MYPROG_ANOTHER=1 # 0x1
export MYPROG_ANOTHER

If the arguments are to be reordered, however, then the resulting set of operands will be emitted and OPTION_CT will be set to zero. For example, the following would be appended to the above:

set -- 'operand1' 'operand2' 'operand3'
OPTION_CT=0

OPTION_CT is set to zero since it is not necessary to shift off any options.