/*! \page gislib_cmdline_parsing Command Line Parsing \tableofcontents \section gislibcmd_Introduction Introduction This section describes a standard mechanism for command line parsing in GRASS. The system is usually referred as parser, G_parser() or g.parser (because of the related \gmod{g.parser} module). Use of the provided set of functions will standardize GRASS modules that expect command line arguments, creating a family of GRASS modules that is easy for users to learn. The standardization is important because as soon as a GRASS user familiarizes himself with the general form of command line input as defined by the parser, it will greatly simplify the necessity of remembering or at least guessing the required command line arguments for any GRASS command. It is strongly recommended, almost mandatory, that GRASS programmers use this set of functions for all command line parsing. With their use, the programmer is freed from the burden of generating user interface code for every command. The parser will limit the programmer to a pre-defined look and feel, but limiting the interface is well worth the shortened user learning curve. Moreover, system enables to generate module interface descriptions which can be used by GUI to generate a graphical interface for a module. \note There are also standard options and flags, which ensure even better standardization of names, values and descriptions. \section gislibcmd_Description Description The GRASS parser is the collection of functions and structures defined in the GRASS gis.h header file. These structures and functions allow the programmer to define the options and flags that make up the valid command line input of a GRASS command. The parser functions behave in one of three ways: \note Python modules uses the same system but instead of functions and structures, comments in files are used to define options and flags. \section Parser_Interface Parser interface The parser functions described below use two structures as defined in the GRASS gis.h header file. This is a basic list of members of the Option and Flag structures. A comprehensive description of all elements of these two structures and their possible values can be found in \ref Complete_Structure_Members_Table. \subsection Option_structure Option structure The basic usage of the Option structure is as follows. You declare a pointer to the Option structure. \code struct Option *opt; \endcode And then you call G_define_option() function, which allocates memory for the Option structure and returns a pointer to it. \code opt = G_define_option(); \endcode Then you set the structure members, basic members are: - key - The option name on the command line. - description - The option description to display to the user. - type - Variable type of the user's answer to the option. - required - Whether this option is mandatory or not. For the full list of members see the Option structure documentation. \subsection Flag_structure Flag structure The basic usage of the Flag structure is as follows. You declare a pointer to the Flag structure. \code struct Flag *flag; \endcode And then you call G_define_flag() function, which allocates memory for the Flag structure and returns a pointer to it. \code flag = G_define_flag() \endcode Then you set the structure members, basic members are: - key - A single letter flag name on the command line. - description - The flag description to display to the user. For the full list of members see the Flag structure documentation. \subsection Running_Parser Running Parser To process and check the command line parameters of your module, you need to call the G_parser() function. The command line parameters argv and the number of parameters argc from the main() routine should be passed directly to the G_parser() function. The function accepts the command line input entered by the user, and parses this input according to the input options and/or flags that were defined by the programmer. G_parser() returns 0 if successful. If not successful, it displays a usage statement, which describes the expected and/or required options and flags, and returns a non-zero value. \subsection Parser_Additional_checks Additional checks of command line parameters When the G_parser() function is not sufficient to check all the details about the options and flags and their combinations, the code has to implement the required logic through custom additional checks. The code will typically call the G_usage() function if these checks are not successful. G_usage() prints a usage message, which explains the allowed and the required command line input to the user. This description is given according to the programmer's definitions for options and flags. This function becomes useful when the user enters options and/or flags on the command line that are syntactically valid to the parser, but functionally invalid for the command (e.g. an invalid file name). For example, the parser logic doesn't directly support grouping options. If two options must be specified together or not at all, the parser must be told that these options are not required and the programmer must check that if one option is specified the other is as well. If this additional check fails after G_parser() has succeeded, the programmer can then call G_usage() to print the standard usage message and then would separately need to print the additional information on how the two options work together. \subsection Parser_Displaying_multiple_answers Multiple answer default values Providing multiple default values (answers) for an option that allows multiple values is possible using: \code char *def[] = {"One", "Two", "Last", NULL}; opt->multiple = YES; opt->answers = def; \endcode The programmer must not forget the last NULL value. New in GRASS 5. \subsection Parser_Disabling_interactive Disabling interactive mode This is mainly historical feature which enables to disable interactive prompting in command line. When a user runs a command with no arguments on the command line, the parser will enter its own standardized interactive session and prompt the user for all flags and options interactively. A call to G_disable_interactive() disables this feature. \section Parser_Programming_Examples Parser Programming Examples The use of the parser in the programming process is demonstrated here. Both a basic step by step example and a full code example are presented. \subsection Step_by_Step_Use_of_the_Parser Step by Step Use of the Parser Below are four basic steps to use the GRASS parser in a GRASS command: \subsubsection gislib_cmdline_Allocate Allocate memory for Flags and Options Options and flags are pointers to structures (Option and Flag structures) allocated through the parser functions G_define_option() and G_define_flag() as described in \ref Parser_Interface. \code #include ; /* The standard GRASS include file */ /* ... */ struct Option *opt; /* Establish an Option pointer for each option */ struct Flag *flag; /* Establish a Flag pointer for each option */ opt = G_define_option(); /* Request a pointer to memory for each option */ flag = G_define_flag(); /* Request a pointer to memory for each flag */ \endcode \subsubsection gislib_cmdline_Define Define members of Flag and Option structures Define the characteristics of each required option and flag, for example: \code opt->key = "option"; /* The name of this option is "option". */ opt->description = _("Option test"); /* The option description is "Option test" */ opt->type = TYPE_STRING; /* The data type of the answer to the option */ opt->required = YES; /* This option *is* required from the user */ flag->key = "t"; /* Single letter name for flag */ flag->description = _("Flag test"); /* The flag description is "Flag test" */ \endcode \note There are more options defined in \ref Complete_Structure_Members_Table. You should for sure explore the label, the options and the multiple structure members. \subsubsection gislib_cmdline_Call Call the parser \code int main(int argc, char *argv[]); /* command line args passed into main() */ /* ... options and flags definitions */ if (G_parser(argc, argv)) /* Returns 0 if successful, non-zero otherwise */ exit(EXIT_FAILURE); /* ... additional checks */ /* ... module code */ \endcode \subsubsection gislib_cmdline_Extracting Extracting information from the parser structures The following code extracts the information about an option and a flag and prints it to the standard output. \code fprintf(stdout, "For the option "%s" you chose: <%s>\n", opt->description, opt->answer); fprintf(stdout, "The flag "-%s" is %s.\n", flag->key, flag->answer ? "set" : "not set"); \endcode \subsubsection gislib_cmdline_Running Running the example program Once such a module has been compiled (see \ref Compiling_and_Installing_GRASS_Modules), execution will result in the following user interface scenarios. Lines that begin with '$' (dollar sign) indicate a command line session with user commands. \verbatim $ r.mysample --help \endverbatim This is a standard user request for basic help information on the module. The command line options (in this case, --help) are sent to the parser via G_parser(). The parser recognizes the --help command line option and returns the list of options and/or flags that are applicable for the specific command. Note how the option and the flag information specified above appears in the output. \verbatim r.mysample [-t] option=name Flags: -t Flag test Parameters: option Option test \endverbatim Now consider the following command: \verbatim $ r.mysample -t \endverbatim This command does not contain the required option. Note that the output provides this information along with the standard usage message (as already shown above): \verbatim Required parameter