General Information
Tutorials
Reference Manuals
Libraries
Translation Tasks
Tools
Administration
|
Command Line ProcessingAccessing the command lineA CLP specification is turned into code that arranges for command line information to be stored in C variables or a simple database. This code is automatically run by the processor startup code generated by Eli. During attribution of a structure tree you can access the variables or use access functions to obtain the command-line information.
The header file `clp.h' will contain
Accessing boolean optionsSince boolean options do not have value information associated with them, all that is needed to represent them is a simple flag rather than a database object. We store the flag as a C integer variable whose name is the option name. For example, given the specification:
GenAssembly "-S" boolean;CLP will generate a variable called GenAssembly . Typical C code
to test for this option would look like:
if (GenAssembly) printf ("GenAssembly specified\n"); else printf ("GenAssembly not specified\n");
Accessing options with integer valuesTo provide access to integer value options, CLP generates a database object which has the appropriate value as a property. The object is referred to by a key-valued variable named after the option. For example, given the specification:
NumCopies "-#" int;CLP will generate a variable called NumCopies . The value of the
variable can be used to access the option value using the
GetClpValue
property access function.
printf ("NumCopies value is %d\n", GetClpValue (NumCopies, 0));Here 0 will be printed if the option was not specified by the user.
Alternatively, presence of the option can be tested for explicitly by testing the key:
if (NumCopies == NoKey) printf ("NumCopies not specified\n"); else printf ("NumCopies value is %d\n", GetClpValue (NumCopies, 0));In this case the default value parameter in the GetClpValue call
will never be used.
Accessing options with string valuesAccess to string value options is provided via a database object which has the appropriate value as a property. The object is referred to by a key-value variable named after the option. For example, given the specification:
TmpFile "-temp=" joinedto string;CLP will generate a variable called TmpFile . GetClpValue
is used to obtain the value which should be interpreted as a string
table index (see Arbitrary-length character strings of Library Reference).
#include "csm.h" if (TmpFile == NoKey) printf ("TmpFile not specified\n"); else printf ("TmpFile value is '%s'\n", StringTable (GetClpValue (TmpFile, 0)));
Accessing options that appear more than onceThe mechanisms described in the previous three sections only apply to options that can appear at most once on the command line. More complicated mechanisms are needed to access values associated with repeated options.
CLP uses linked lists of definition table keys
to provide multiple value access. The lists are implemented using Eli's
Given the specification:
MacroPackage "-m" joinedto strings;the list module lets you print the multiple values (via keys) as follows:
#include "csm.h" #include "clp.h" DefTableKey printkey (DefTableKey k) { printf ("%s", StringTable (GetClpValue (k, 0))); } (void) MapDefTableKeyList (MacroPackage, printkey); Boolean repeated options are an exception to this list approach. Since no value is associated with the option there is little point in having a list of keys. For this reason, boolean repeated options are implemented as a single integer whose value is the number of times the option appeared.
Accessing positional parametersFor each singular positional parameter specification line CLP generates a variable of the appropriate name holding the key to a database object that has the string value of the positional parameter as a property. The value can be accessed as for string value options (see Accessing options with string values). CLP will always make sure that a positional parameter is specified and will arrange for a usage message to be printed otherwise. Thus there is no need to test that the database object described in the previous paragraph is defined. For example, given the specification line:
FileName positional;the following code can be used to print the specified value:
#include "csm.h" printf ("Filename given was '%s'\n", StringTable (GetClpValue (FileName, 0))); When a plural positional parameter specification line is given the mechanisms used for repeated option values are used (see Accessing options that appear more than once). For example, if a processor can take multiple input files a specification like the following might be used:
FileNames positionals;The following code can be used to print these parameters See Linear Lists of Any Type of Specification Module Library: Abstract Data Types.
#include "csm.h" #include "clp.h" DefTableKey printkey (DefTableKey k) { printf ("%s", StringTable (GetClpValue (k, 0))); } (void) MapDefTableKeyList (FileNames, printkey);
Accessing input parametersSince an input parameter is just a special kind of positional parameter, its value (which is the name of the input file) can be accessed as shown in Accessing positional parameters.
The input parameter may also be accessed via the standard name
Reporting open errors
CLP defines the text of an error report that a program can write to
standard error when a file cannot be opened
(see Options that affect usage messages).
The program writes this error report by invoking the
void ClpOpenError(const char *filename, const char *errtext) /* On entry- * filename points to the string to replace any %f escape in the report * errtext points to the string to replace any %t escape in the report * On exit- * The modified report has been written to stderr */
Although the role of the argument strings is completely arbitrary, the
usual practice is to use the name of the file being opened as the value of
#include <errno.h> #include <string.h> ... int infile = open(infilename, 0); if (infile == -1) { ClpOpenError(infilename, strerror(errno)); exit(2); } ...
|