Version 1.2.2 2006/04/13
Download
Change log
Simple example
Full example
Aargh
Automated Argument Helper
Summary
Aargh generates C or C++ code to parse a command line, using the
getopt() facility available in UNIX and UNIX-like environments.
It supports command-line options with integer arguments, string
arguments, and no arguments. The generated code is commented and
carefully indented for readability. Aargh is written in C++ and
licensed under the GNU General Public License (GPL).
Synopsis
aargh [-l language] [-v] [-x ext] [-X hdr_ext] [filename]
The -l option (that's a lower case L) specifies the language to be
generated. Aargh currently supports "C" and "C++". If you don't
specify a language, aargh defaults to C++.
The -v option requests verbose mode, in which aargh writes a serious of
informational messages to standard output.
The -x option specifies the file name extension to be applied to the
generated source code file, if not specified in the XML input file.
If not specified in the XML input file nor on the aargh command
line, the file name extension defaults to "c" for C and "C" for
C++.
The -X option is similar to the -x option, except that it applies
to the file name of the generated header. The file name extension
for the header defaults to "h" if not otherwise specified.
The filename argument specifies an XML file defining the options
that you want your program to support. If you don't specify an
input file, aargh reads standard input.
Introduction
It's nice to have lots of command line options, but it's a real pain
to code for them.
Not that it's difficult. It's just tedious, time-consuming, error
prone, and no fun. I used to wince at the thought of adding
command line options.
Not any more. All I have to do is define the options I want in a
small XML file and run it through aargh. Instantly I
have C or C++ source code that I can compile and link into my program.
If I want to add another option or otherwise change the rules, I can
edit the XML and regenerate the code. Now I can spend more time on the
interesting parts of the project.
You can download aargh in the form of C++
source code and compile it yourself. It is licensed under the GNU
General Public License (GPL).
Using Aargh
To use aargh, write an XML file to describe the options you want.
Aargh reads the file and generates two source files.
One file is a header that declares a C struct or a C++ class to
represent the options. By default this struct or class is named "Opts,"
but you can specify a different name. The other file is the
implementation of Opts (or whatever you decide to call it), providing
a function to parse the command line. For C, the results are
available as members of the struct. For C++, member functions perform
the parsing and make the results available.
In your own code, declare an instance of Opts and call the parsing
function, passing it the argc and argv arguments from main().
The parsing function returns zero if the command line follows
the rules that you have outlined in the XML file, or non-zero
otherwise. If the parsing is successful, the Opts object stores the
results.
This process is easier to visualize with a simple example. A
full example demonstrates all the
available features in greater detail.
What You Can and Can't Do With Aargh
Aargh can generate code to do most of what you ever need to do with a
command line:
-
You can enforce upper and/or lower limits on the number of non-option
arguments.
-
You can distinguish between options that take arguments and those that
don't.
-
You can specify that an option is required.
-
You can require that the argument to an option be a non-negative
integer.
-
For an integer argument you can enforce upper and/or lower limits.
-
For a string argument you can enforce upper and/or lower limits on
the string length.
-
For a string argument you can call a function of your own to validate
the argument.
-
For a string argument you can specify a default value to be applied
when the option is omitted.
-
By default the generated software allows only one occurrence of any
given option, but you can allow multiple occurrences. In that case
the generated code stores the arguments in a linked list (for C) or
an STL vector (for C++).
-
You can add customized comments to a comment block at the top of
the generated source files.
If necessary, you can accept long double-hyphen options such as "--print".
However aargh doesn't really work very well for this kind of option. You
have to define a hyphen as the option letter, and treat the rest,
such as "print", as the argument of the option. As a result, such an
option cannot take an argument, because the generated code treats the
option name as the argument of the hyphen option.
There are other situations that aargh can't handle:
-
You can't define an option that sometimes takes an argument
and sometimes doesn't.
-
You can't keep track of the order in which the options occur.
-
You can't generate code for any language other than C or C++.
Support for other languages wouldn't be hard to add, just tedious.
-
You can't validate interactions among different options. For
instance, you can't specify that the -t and -v options are
incompatible, or that the presence of a -i option requires that
there be a -o option as well. You can still enforce those kinds
of rules outside of the generated code.
Aargh is a simple tool for simple needs. You can use it to
produce usable code in a hurry. If you need to do something
fancier than what aargh will do for you, then you have several options:
- Forget about aargh and write your own code from scratch.
- Take the code generated by aargh and modify it.
- Modify aargh to extend its power. If you do, I hope you will
share your enhancements with the world, as permitted (and
encouraged) by the GPL.
Licensing
Aargh is licensed under the GNU General Public License, version 2,
a copy of which is included in the distribution.
This license does not extend to the code generated by aargh, which
belongs to whoever generates it. I claim no rights to the code you
generate. You can license it under whatever terms you choose, or not
license it at all.
Acknowledgements
Aargh uses the
Tinyxml
library to read the XML file. Thanks to Lee Thomason for TinyXml.
Scott McKellar