From 0e56fe29a9afeee00e02e722496678df89d37d50 Mon Sep 17 00:00:00 2001
From: Boris Kolpackov As we have seen in the previous sections, the To make the usage information more descriptive we can document each
+ option in the command line interface definition. This information can
+ also be used to automatically generate program documentation in various
+ formats, such as HTML and man page. For example: If we now save this updated command line interface to
+ We can also generate the program documentation in the HTML
+ ( The resulting This HTML fragment can be combined with custom prologue and epilogue
+ to create a complete program documentation
+ ( This chapter describes the CLI language and its mapping to C++.
- A CLI file consists of zero or more Include
- Directives followed by one or more Namespace Definitions
+ A CLI file consists of zero or more Include
+ Directives followed by one or more Namespace Definitions
or Option Class Definitions. C and C++-style comments
can be used anywhere in the CLI file except in character and
string literals. An option class is mapped to a C++ class with the same name. The
C++ class defines a set of public overloaded constructors, a public
copy constructor and an assignment operator, as well as a set of public
- accessor functions corresponding to option definitions.2.2 Translating CLI Definitions to C++ 2.3 Implementing Application Logic
+ 2.4 Compiling and Running
@@ -177,8 +189,9 @@
2.5 Adding Documentation
@@ -332,10 +345,9 @@ using namespace std;
void
usage ()
{
- cerr << "usage: driver <options> <names>" << endl
- << " [--help]" << endl
- << " [--greeting <string>]" << endl
- << " [--exclamations <integer>]" << endl;
+ cerr << "usage: driver [options] <names>" << endl
+ << "options:" << endl;
+ options::print_usage (cerr);
}
int
@@ -411,19 +423,110 @@ Hi, Jane!!!
3.1 Options Class Definition
- 3.2 Option Definition
- 3.3 Include Directive
+ 3.4 Namespace Definition
+ 3.3 Option Documentation
+ 3.4 Include Directive 3.5 Namespace Definition
$ ./driver -n 3 Jane
unknown option '-n'
-usage: driver <options> <names>
- [--help]
- [--greeting <string>]
- [--exclamations <integer>]
+usage: driver [options] <names>
+options:
+--help
+--greeting <arg>
+--exclamations <arg>
$ ./driver --exclamations abc Jane
invalid value 'abc' for option '--exclamations'
-usage: driver <options> <names>
- [--help]
- [--greeting <string>]
- [--exclamations <integer>]
+usage: driver [options] <names>
+options:
+--help
+--greeting <arg>
+--exclamations <arg>
+
+
+ 2.5 Adding Documentation
+
+ options
+ C++ class provides the print_usage()
function which we
+ can use to display the application usage information. Right now this
+ information is very basic and does not include any description of
+ the purpose of each option:
+$ ./driver --help
+usage: driver [options] <names>
+options:
+--help
+--greeting <arg>
+--exclamations <arg>
+
+
+
+include <string>;
+
+class options
+{
+ bool --help {"Print usage information and exit."};
+
+ std::string --greeting = "Hello"
+ {
+ "<text>",
+ "Use <text> as a greeting phrase instead of the default \"Hello\"."
+ };
+
+ unsigned int --exclamations = 1
+ {
+ "<num>",
+ "Print <num> exclamation marks instead of 1 by default."
+ };
+};
+ hello.cli
and recompile our application, the usage
+ information printed by the program will look like this:
+usage: driver [options] <names>
+options:
+--help Print usage information and exit.
+--greeting <text> Use <text> as a greeting phrase instead of the
+ default "Hello".
+--exclamations <num> Print <num> exclamation marks instead of 1 by
+ default.
+
+
+ --generate-html
CLI option) and man page
+ (--generate-man
CLI option) formats. For example:
+$ cli --generate-html hello.cli
+
+
+ hello.html
file contains the following
+ documentation:--html-prologue/--html-epilogue
options for the HTML
+ output, --man-prologue/--man-epilogue
options for the
+ man page output). For an example of such complete documentation see
+ the CLI
+ Compiler Command Line Manual and the cli(1)
man
+ page. For more information on the option documentation syntax,
+ see Section 3.3, Option Documentation.3 CLI Language
print_usage()
function that
+ can be used to print the usage information for the options
+ defined by the class.
The argc/argv
arguments in the overloaded constructors
are used to pass the command line arguments array, normally as passed
@@ -654,12 +764,12 @@ namespace cli
An option definition consists of there components: type,
- name, and default value. An option type can be any
- C++ type as long as its string representation can be parsed using
- the std::istream
interface. If the option type is
- user-defined then you will need to include its declaration using
- the Include Directive.
An option definition consists of four components: type,
+ name, default value, and documentation.
+ An option type can be any C++ type as long as its string representation
+ can be parsed using the std::istream
interface. If the option
+ type is user-defined then you will need to include its declaration using
+ the Include Directive.
An option of a type other than bool
is expected to
have a value. An option of type bool
is treated as
@@ -672,7 +782,7 @@ namespace cli
by |
. The C++ accessor function name is derived from the
first name by removing any leading special characters, such as
-
, /
, etc., and replacing special characters
- in other places with underscore. For example, the following option
+ in other places with underscores. For example, the following option
definition:
@@ -705,7 +815,7 @@ class options };-
The final component of the option definition is the optional default +
The following component of the option definition is the optional default
value. If the default value is not specified, then the option is
initialized with the default constructor. In particular, this means
that a bool
option will be initialized to false
,
@@ -797,7 +907,127 @@ class options
-m =B
(key is an empty string), -m c=
(value
is an empty string), or -m d
(same as -m d=
).
The last component in the option definition is optional documentation. + It is discussed in the next section.
+ +Option documentation mimics C++ string array initialization:
+ it is enclosed in {}
and consists of one or more
+ documentation strings separated by a comma, for example:
+class options +{ + int --compression = 5 + { + "<level>", + "Set compression to <level> instead of 5 by default. + + With the higher compression levels the program may produce a + smaller output but may also take longer and use more memory." + }; +}; ++ +
The option documentation consists of a maximum of three documentation
+ strings. The first string is the value documentation string.
+ It describes the option value and is only applicable to options
+ with types other than bool
(options of type
+ bool
are flags and don't have an explicit value).
+ The second string (or the first string for options of type
+ bool
) is the short documentation string. It
+ provides a brief description of the option. The last entry
+ in the option documentation is the long documentation string.
+ It provides a detailed description of the option. The short
+ documentation string is optional. If only two strings are
+ present in the option documentation (one string for options
+ of type bool
), then the second (first) string is
+ assumed to be the long documentation string.
Option documentation is used to print the usage information
+ as well as to generate program documentation in the HTML and
+ man page formats. For usage information the short documentation
+ string is used if provided. If only the long string is available,
+ then, by default, only the first sentence from the long string
+ is used. You can override this behavior and include the complete
+ long string in the usage information by specifying the
+ --long-usage
CLI compiler option. When generating
+ the program documentation, the long documentation strings are
+ always used.
The value documentation string can contain text enclosed in
+ <>
which is automatically recognized by the CLI
+ compiler and typeset according to the selected output in all three
+ documentation strings. For example, in usage the level
+ value for the --compression
option presented above
+ will be displayed as <level>
while in the HTML and
+ man page output it will be typeset in italic as
+ level
. Here is another example using the
+ std::map
type:
+include <map>; +include <string>; + +class options +{ + std::map<std::string, std::string> --map + { + "<key>=<value>", + "Add the <key>, <value> pair to the map." + }; +}; ++ +
The resulting HTML output for this option would look like this:
+ + + +As you might have noticed from the examples presented so far, the
+ documentation strings can span multiple lines which is not possible
+ in C++. Also, all three documentation strings support the following
+ basic formatting mechanisms. The start of a new paragraph is indicated
+ by a blank line. A fragment of text can be typeset in monospace font
+ (normally used for code fragments) by enclosing it in the
+ \c{}
block. Similarly, text can be typeset in bold or
+ italic fonts using the \b{}
and \i{}
blocks,
+ respectively. You can also combine several font properties in a single
+ block, for example, \cb{bold code}
. If you need to include
+ literal }
in a formatting block, you can use the
+ \}
escape sequence, for example,
+ \c{int a[] = {1, 2\}}
. The following example shows how we
+ can use these mechanisms:
+class options +{ + int --compression = 5 + { + "<level>", + "Set compression to <level> instead of 5 by default. + + With the higher compression levels the program \i{may} + produce a smaller output but may also \b{take longer} + and \b{use more memory}." + }; +}; ++ +
The resulting HTML output for this option would look like this:
+ + + +If you are using user-defined types in your option definitions,
you will need to include their declarations with the include
@@ -843,7 +1073,7 @@ class options
and name_type
types in the options
class would
be undeclared and result in compilation errors.
Option classes can be placed into namespaces which are translated directly to C++ namespaces. For example:
-- cgit v1.1