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

8.8 AutoOpts for Shell Scripts

AutoOpts may be used with shell scripts by automatically creating a complete program that will process command line options and pass back the results to the invoking shell by issuing shell variable assignment commands. It may also be used to generate portable shell code that can be inserted into your script.

The functionality of these features, of course, is somewhat constrained compared with the normal program facilities. Specifically, you cannot invoke callout procedures with either of these methods. Additionally, if you generate a shell script:

  1. You cannot obtain options from RC/INI files.
  2. You cannot obtain options from environment variables.
  3. You cannot save the option state to an option file.
  4. Option conflict/requirement verification is disabled.

Both of these methods are enabled by running AutoGen on the definitions file with the additional global attribute:

 
test-main [ = proc-to-call ] ;

If you do not supply a proc-to-call, it will default to putBourneShell. That will produce a program that will process the options and generate shell text for the invoking shell to interpret. If you supply the name, putShellParse, then you will have a program that will generate a shell script that can parse the options. If you supply a different procedure name, you will have to provide that routine and it may do whatever you like.

In summary, you will need to issue approximately the following two commands to have a working program:

 
autogen -L <opt-template-dir> program.def
cc -o progopts -L <opt-lib-dir> -I <opt-include-dir> \
        -DTEST_program_OPTS program.c -lopts

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

 
eval `./progopts $@`
if [ -z "${OPTION_CT}" ] ; then exit 1 ; fi
shift ${OPTION_CT}

If you had used "test-main = putShellParse" instead, then you can, at this point, merely run the program and it will write the parsing script to standard out. You may also provide this program with command line options to specify the shell script file to create or edit, and you may specify the shell program to use on the first shell script line. That program's usage text would look something like this:

 
/bin/sh: .tmp/genshellopt: No such file or directory


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

This document was generated by Bruce Korb on February, 4 2002 using texi2html