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

7.15 Using getopt(3C)

There is a template named, ‘getopt.tpl’ that is distributed with AutoOpts. Using that template instead of ‘options.tpl’ will produce completely independent source code that will parse command line options. It will utilize either the standard getopt(3C) or the GNU getopt_long(3GNU) function to drive the parsing. Which is used is selected by the presence or absence of the long-opts program attribute. It will save you from being dependent upon the libopts library and it produces code ready for internationalization. However, it also carries with it some limitations on the use of AutoOpts features and some requirements on the build environment.

PLEASE NOTE: in processing the option definitions to produce the usage text, it is necessary to compile some generated code in a temporary directory. That means that all the include directories needed to compile the code must be full path names and not relative directory names. “.” is a relative directory name. To specify “-I.” in the CFLAGS environment variable, you must expand it. For example, use:

 
CFLAGS=-I`pwd`

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

7.15.1 getopt feature limitations

This list of limitations is relative to the full list of AutoOpts supported features, See section AutoOpts Features.

  1. You cannot automatically take advantage of environment variable options or automated parsing of configuration files (rc or ini files). Consequently, the resulting code does not support ‘--load-opts’ or ‘--save-opts’ options automatically.
  2. You cannot use set membership, enumerated, range checked or stacked argument type options. In fact, you cannot use anything that depends upon the libopts library. You are constrained to options that take string arguments, though you may handle the option argument with a callback procedure.
  3. Special disablement and/or enablement prefixes are not recognized.
  4. Option coordination with external libraries will not work.
  5. Every option must be settable because the emitted code depends upon the SET_OPT_XXX macros having been defined. Specify this as a global (program) attribute.
  6. You must specify a main procedure attribute (see section Generating main procedures). The ‘getopt.tpl’ template depends upon being able to compile the traditional .c file into a program and get it to emit the usage text.
  7. For the same reason, the traditional option parsing table code must be emitted before the ‘getopt.tpl’ template gets expanded.
  8. The usage text is, therefore, statically defined.

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

7.15.2 getopt build requirements

You must supply some compile and link options via environment variables.

srcdir

In case the option definition file lives in a different directory.

CFLAGS

Any special flags required to compile. The flags from autoopts-config cflags will be included automatically. Since the creation of the option parsing code includes creating a program that prints out help text, if it is necessary to include files from various directories to compile that program, you will need to specify those directories with ‘-Idirpath’ text in the CFLAGS. Some experimentation may be necessary in that case.

NOTE: the ‘-Idirpath’ text is only needed if your option callback functions include code that require additional #include directives.

LDFLAGS

Any special flags required to link. The flags from autoopts-config ldflags will be included automatically. This is required only if additional link flags for the help text emission program might be needed.

CC

This is needed only if cc cannot be found in $PATH (or it is not the one you want).

To use this, set the exported environment variables and specify getopt as the default template in your option definitions file (see section The Identification Definition). You will have four new files. Assuming your definitions were in a file named ‘myprog-opts.def’ and your program name was specified as ‘progname’, the resulting files would be created: ‘myprog-opts.h’, ‘myprog-opts.c’, ‘getopt-progname.h’ and ‘getopt-progname.c’. You must compile and link both ‘.c’ files into your program. If there are link failures, then you are using AutoOpts features that require the ‘libopts’ library. You must remove these features, See section getopt feature limitations.

These generated files depend upon configure defines to work correctly. Therefore, you must specify a config-header attribute (see section Programming Details) and ensure it has #defines for either HAVE_STDINT_H or HAVE_INTTYPES_H; either HAVE_SYS_LIMITS_H or HAVE_LIMITS_H; and HAVE_SYSEXITS_H, if the ‘sysexits.h’ header is available. The required header files for these defines are, respectively, the ‘/usr/include’ files named:

The following header files must also exist on the build platform:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

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