AutoGen: 7.14 Using getopt(3C)

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

## 7.14 Using getopt(3C)

There is now a template named, "`getopt.tpl`" that is distributed with autoopts. With it, you will have another source file generated for you that will utilize either the standard `getopt(3C)` or the GNU `getopt_long(3GNU)` function for parsing the command line arguments. 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:

1. You cannot automatically take advantage of environment variable options or rc (ini) files.
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. You must specify every option as "settable" because the emitted code depends upon the `SET_OPT_XXX` macros having been defined.
4. You must specify a main procedure of type "main". 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.
5. For the same reason, the traditional option parsing table code must be emitted before the ``getopt.tpl'` template gets expanded.
6. The usage text is, therefore, statically defined.
7. 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. This should minimally include the output from running the `autoopts-config cflags` script.

   ``LDFLAGS'`

     Any special flags required to link. This should minimally include the output from running the `autoopts-config ldflags` script.

   ``CC'`

     Set this 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 then invoke autogen twice, in the following order:

    autogen myprog-opts.def
    autogen -T getopt.tpl myprog-opts.def

and you will have three new files: ``myprog-opts.h'`, ``myprog-opts.c'`, and ``getopt-progname.c'`, where "progname" is the name specified with the global `prog-name` attribute in the option definition file.

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

This document was generated by Bruce Korb on January, 8 2006 using texi2html 1.76.