|
NAMEyaggo - command line switch parser generatorSYNOPSISyaggo [-o|--output FILE] [-l|--license PATH] [-s|--stub] [--zc PATH] [-e|--extended-syntax] [--man] [-h|--help]DESCRIPTIONYaggo stands for Yet Another GenGetOpt. It is inspired by gengetopt software from the FSF.Yaggo generates a C++ class to parse command line switches (usually argc and argv passed to main) using getopt_long. The switches and arguments to the program are specified in a description file. To each description file, yaggo generates one C++ header file containing the parsing code. See the EXAMPLES section for a complete and simple example. OPTIONS
EXAMPLEConsider the description files 'example_args.yaggo' which defines a switch "-i" (or "--int") that takes an unsigned integer and defaults to 42; a switch "-s" (or "--string") that takes a string and can be given multiple times; a switch "--flag" which does not take any argument; a switch "--severity" which can take only 3 values: "low", "middle" and "high".It takes the following arguments: a string followed by zero or more floating point numbers. purpose "Example of yaggo usage" package "example" description "This is just an example. And a multi-line description." option("int", "i") { description "Integer switch" uint32; default "42" } option("string", "s") { description "Many strings" string; multiple } option("flag") { description "A flag switch" flag; off } option("severity") { description "An enum switch" enum "low", "middle", "high" } arg("first") { description "First arg" c_string } arg("rest") { description "Rest of'em" double; multiple } The associated simple C++ program 'examples.cpp' which display information about the switches and arguments passed: #include <iostream> #include "example_args.hpp" int main(int argc, char *argv[]) { example_args args(argc, argv); std::cout << "Integer switch: " << args.int_arg << "\n"; if(args.string_given) std::cout << "Number of string(s): " << args.string_arg.size() << "\n"; else std::cout << "No string switch\n"; std::cout << "Flag is " << (args.flag_flag ? "on" : "off") << "\n"; std::cout << "First arg: " << args.first_arg << "\n"; std::cout << "Severity arg: " << args.severity_arg << " " << example_args::severity::strs[args.severity_arg] << "\n"; if(args.severity_arg == example_args::severity::high) std::cout << "Warning: severity is high\n"; std::cout << "Rest:"; for(example_args::rest_arg_it it = args.rest_arg.begin(); it != args.rest_arg.end(); ++it) std::cout << " " << *it; std::cout << std::endl; return 0; } This can be compiled with the following commands: % yaggo example_args.yaggo % g++ -o example example.cpp The yaggo command above will create by default the file file name can be changed with the 'output' keyword explained below. DESCRIPTION FORMATA description file is a sequence of statements. A statement is a keyword followed by some arguments. Strings must be surrounded by quotes ("" or '') and can span multiple lines. The order of the statements is irrelevant. Statements are separated by new lines or semi-colons ';'.
The following statements are global, not attached to a particular option or argument.
The 'option' statement takes one or two arguments, which must be in parentheses, and a block of statements surrounded by curly braces ({...}). The arguments are the long and short version of the option. Either one of the long or short version can be omitted. The block of statements describe the option in more details, as described below. A switch is named after the long version, or the short version if no long version. An 'option' statement for an option named 'switch' defines one or two public members in the class. For a flag, it creates 'switch_flag' as a boolean. Otherwise, it creates 'switch_arg', with a type as specified, and 'switch_given', a boolean indicating whether or not the switch was given on the command line. For example, the statement: option("integer", "i") { int; default 5 } will add the following members to the C++ class: int integer_arg; bool integer_given; where "integer_arg" is initialized to 5 and "integer_given" is initialized to "false". If the switch "--integer 10" or "-i 10" is passed on the command line "integer_arg" is set to 10 and integer_given is set to "true". The statement: option("verbose") { off } will add the following member to the C++ class: bool verbose_flag; where "verbose_flag" is initialized to "false". Passing the switch "--verbose" on the command line sets "verbose_flag" to true". In addition to the switch created by 'option', the following switches are defined by default (unless some option statement overrides them):
The following statement are recognized in an option block:
A 'arg' statement defines an arg passed to the command line. The statement takes a single argument, the name of the arg, and a block of statements. The block of statements are similar to the option block, except that "hidden", "flag", "on", "off" and "no" are not allowed. At most one arg can have the 'multiple' statement, and it must be the last one. EXAMPLE USAGEThe argument object parses the switches on construction or later on using the parse method. For example, the two pieces code show these two different usage.Using parse method: example_args args; // Global variable with switches int main(int argc, char* argv[]) { args.parse(argc, argv); } Parse on construction: int main(int argc, char* argv[]) { example_args args(argc, argv); } The subclass error can be used to output error messsage (and terminate program). It output an error message, the usage string, etc. The error class behave like an output stream, it can be used to create complicated error message. For example: if(false_condition) example_args::error() << "Failed to open file '" << args.file_arg << "'"; An error object prints an error message and terminate the program with exit upon destruction. An exit code can be passed to error. By default the exit code (passed to exit) is the constant EXIT_FAILURE (normally 1). For example: example_args::error(77) << "Failed with return code 77"; LICENSEThere are 2 parts to the software: the yaggo ruby script itself, and the header files generated by yaggo from the description files. The licenses are as follow:
BUGS
AUTHORGuillaume Marcais (gmarcais@umd.edu)SEE ALSOgetopt_long(3), gengetopt(1), exit(2)
Visit the GSP FreeBSD Man Page Interface. |