home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume31 / cmdline / part03 / Overview
Encoding:
Text File  |  1992-07-27  |  10.3 KB  |  258 lines

  1.  
  2.  
  3.                     An overview of CmdLine and cmdparse
  4.                     ===================================
  5.  
  6.                  by Brad Appleton <brad@ssd.csd.harris.com>
  7.  
  8.  
  9.  
  10.  Introduction
  11.  ------------
  12.  CmdLine is a C++ Library for parsing command-line arguments. It is 
  13.  approximately 2000 lines of C++ code (excluding comments).
  14.  
  15.  Cmdparse is a command-line interface to CmdLine for Unix shell-scripts.
  16.  It is approximately 1200 lines of C++ code (excluding comments).
  17.  
  18.  
  19.  CmdLine(3C++)
  20.  -------------
  21.  CmdLine is a set of classes to parse command-line arguments.  Unlike
  22.  getopt() and its variants, CmdLine does more than just split up the
  23.  command-line into some canonical form.  CmdLine will actually parse
  24.  the command-line, assigning the appropriate command-line values to
  25.  the corresponding variables, and will verify the command-line syntax
  26.  (and print a usage message if necessary) all in one member function
  27.  call.  Furthermore, many features of CmdLine's parsing behavior are
  28.  configurable at run-time.  These features include the following:
  29.  
  30.      o  Prompting the user for missing arguments.
  31.      o  Allowing keywords (-count=4) and/or options (-c4).
  32.      o  Ignoring bad syntax instead of terminating.
  33.      o  Ignoring upper/lower case on the command-line.
  34.      o  Suppressing the printing of syntax error messages.
  35.      o  Controlling the verboseness of usage messages.
  36.      o  Controlling whether or not options may be processed
  37.           after positional parameters have been seen.
  38.  
  39.  CmdLine also allows for options that take an optional argument, options
  40.  that take a (possibly optional) list of one or more arguments, options
  41.  whose argument must reside in the same token as the option itself, and
  42.  options whose argument must reside in a separate token from the option
  43.  itself.
  44.  
  45.  CmdLine consists of a set of C++ classes to parse arguments from an
  46.  input source called a CmdLineArgIter (which is a base class for iterating
  47.  over arguments from an arbitrary input source).  Argument iterators are
  48.  defined for an argv[] array (with or without a corresponding argc), for
  49.  a string of tokens that are separated by a given set of delimiters, and
  50.  for an input-stream.  Users can easily extend CmdLine to parse arguments
  51.  from other input sources simply by creating their own argument iterator
  52.  classes derived from the CmdLineArgIter class defined in <cmdline.h>.
  53.  
  54.  Command-line arguments are themselves objects that contain a specific
  55.  command-line interface, and a function that performs the desired actions
  56.  when its corresponding argument is seen on the command line.  Predefined
  57.  command-line argument types (derived from the abstract class CmdArg in
  58.  <cmdline.h>) exist for boolean, integer, floating-point, character, and
  59.  string arguments, and for lists of integers, floats, and strings.  These
  60.  predefined subclasses of CmdArg may be found in <cmdargs.h>.  Users can
  61.  also create their own command-argument types on the fly by defining and
  62.  implementing an appropriate subclass of the CmdArg class.
  63.  
  64.  Using CmdLine is relatively easy - you need to construct your arguments,
  65.  your command-line, and your argument iterator.  Then all that is left to
  66.  do is call the "parse" member function of your CmdLine object.  The
  67.  following is a simple example:
  68.  
  69.     #include <stdlib.h>
  70.     #include <iostream.h>
  71.     #include <cmdargs.h>
  72.  
  73.     int  main(int argc, char * argv[])
  74.     {
  75.           // Declare arguments
  76.        CmdArgInt  count('c', "count", "number", "number of copies to print.");
  77.        CmdArgBool xflag('x', "xmode", "turn on 'x'-mode.");
  78.        CmdArgChar fdsep('s', "separator", "char", "field-separator to use.");
  79.        CmdArgStr  input("input-file",  "input file to read.");
  80.        CmdArgStrList  output("[output-file ...]",  "where to print output.");
  81.  
  82.           // Declare command object and its argument-iterator
  83.        CmdLine  cmd(*argv, &count, &xflag, &fdsep, &input, &output, NULL);
  84.        CmdArgvIter  arg_iter(--argc, ++argv);
  85.  
  86.           // Initialize arguments to appropriate default values.
  87.        count = 1;
  88.        xflag = 0;
  89.        fdsep = ',';
  90.  
  91.           // Parse arguments
  92.        cmd.parse(arg_iter);
  93.  
  94.           // Print arguments
  95.        cout << "count=" << count << endl ;
  96.        cout << "xflag=" << (xflag ? "ON" : "OFF") << endl ;
  97.        cout << "fdsep='" << (char) fdsep << "'" << endl ;
  98.        cout << "input=\"" << input << "\"" << endl ;
  99.        
  100.        for (int i = 0 ; i < output.count() ; i++) {
  101.           cout << "output[" << i << "]=" << output[i] << endl ;
  102.        }
  103.  
  104.        return  0;
  105.     }
  106.  
  107.  
  108.  The Unix command-line syntax for the above program would be as follows:
  109.  
  110.     Usage: progname [-c number] [-x] [-s char] input-file [output-file ...]
  111.  
  112.     Options/Arguments:
  113.             -c number        number of copies to print.
  114.             -x               turn on 'x'-mode.
  115.             -s char          field-separator to use.
  116.             input-file       input file to read.
  117.             output-file ...  where to print output.
  118.  
  119.  
  120.  The Unix command-line syntax using long-options (keywords) for the above
  121.  program would be as follows:
  122.  
  123.     Usage: progname [--count number] [--xmode] [--separator char]
  124.                     input-file [output-file ...]
  125.  
  126.     Options/Arguments:
  127.             --count number    number of copies to print.
  128.             --xmode           turn on 'x'-mode.
  129.             --separator char  field-separator to use.
  130.             input-file        input file to read.
  131.             output-file ...   where to print output.
  132.  
  133.  
  134.  By default, CmdLine allows both options and long-options to appear on the
  135.  command-line. You can instruct CmdLine to disallow one or the other however.
  136.  As an "extra", when options are disallowed, the "-" prefix is assumed to
  137.  denote a long-option instead of an option (hence either "-" or "--" denotes
  138.  a keyword in this case).  Using this feature, CmdLine can be used to supply
  139.  the type of long-option syntax that is now becoming quite popular in the
  140.  Unix world. Using this "new" syntax, the command-line syntax for the above
  141.  command would be the following:
  142.  
  143.     Usage: progname [-count number] [-xmode] [-separator char]
  144.                     input-file [output-file ...]
  145.  
  146.     Options/Arguments:
  147.             -count number    number of copies to print.
  148.             -xmode           turn on 'x'-mode.
  149.             -separator char  field-separator to use.
  150.             input-file       input file to read.
  151.             output-file ...  where to print output.
  152.  
  153.  
  154.  It should be mentioned that, when long-options are used, only a unique
  155.  prefix of the keyword needs to be given (and character-case is ignored).
  156.  Hence, in the above example, "-x", "-X", and "-xm" will match "-xmode".
  157.  
  158.  
  159.  cmdparse(1)
  160.  -----------
  161.  Using "cmdparse" is even easier than using CmdLine. You declare your
  162.  arguments in a string and then you invoke cmdparse with the command
  163.  line of your shell-script and cmdparse will output a script of variable
  164.  settings for you to evaluate.  The following is an example (using the
  165.  same arguments as in our sample program):
  166.  
  167.     #!/bin/sh
  168.     NAME="`/bin/basename $0`"
  169.  
  170.     ARGS='
  171.        ArgInt   count  "[c|count number]"    "number of copies to print."
  172.        ArgBool  xflag  "[x|xmode]"           "turn on x-mode."
  173.        ArgChar  fdsep  "[s|separator char]"  "field-separator to use."
  174.        ArgStr   input  "input-file"          "input file to read."
  175.        ArgStr   output "[output-file ...]"   "where to print output."
  176.     '
  177.  
  178.     if  cmdparse -shell=sh -decls="$ARGS" -- $NAME "$@" > tmp$$
  179.     then
  180.        . tmp$$
  181.        /bin/rm -f tmp$$
  182.     else
  183.        EXITVAL=$?
  184.        /bin/rm -f tmp$$
  185.        exit $EXITVAL
  186.     fi
  187.  
  188.     echo "xflag=" $xflag
  189.     echo "count=" $count
  190.     echo "fdsep=" $fdsep
  191.     echo "input=" $input
  192.     if [ "$output" ] ; then
  193.        echo "output=" $output
  194.     fi
  195.  
  196.  
  197.  Note that you declare the syntax of an argument differently for cmdparse
  198.  than for CmdLine. The syntax for a single argument for cmdparse looks like
  199.  the following:
  200.  
  201.     <arg-type>  <arg-name>  <syntax>  <description>
  202.  
  203.  Where <arg-type> is one of the following:
  204.  
  205.     ArgInt     --  an integer value (or list of values)
  206.     ArgFloat   --  a floating-point value (or list of values)
  207.     ArgChar    --  a character value (or list of values)
  208.     ArgStr     --  a string value (or list of values)
  209.     ArgBool    --  a boolean flag that is turned ON
  210.     ArgClear   --  a boolean flag that is turned OFF
  211.     ArgToggle  --  a boolean flag that is toggled
  212.     ArgUsage   --  print usage and exit
  213.     ArgDummy   -- a dummy argument
  214.  
  215.  If desired, the leading "Arg" portion may be omitted from the type-name.
  216.  
  217.  <arg-name> is simply the name of the variable in your script that you wish
  218.  to contain the resultant value from the command-line.  Any default value
  219.  must be assigned to the variable before invoking cmdparse.
  220.  
  221.  <syntax> and <description> *MUST* be enclosed in either single or double
  222.  quotes! <description> is simply that, the description of the argument.
  223.  
  224.  <syntax> is a little trickier, there are three basic forms of syntax:
  225.  
  226.    1)  "c|keyword"        -- an option the takes no value
  227.    2)  "c|keyword value"  -- an option that takes a value
  228.    3)  "value"            -- a positional parameter
  229.  
  230.  Note that the option-character MUST precede the keyword-name and that
  231.  there must be NO spaces surrounding the '|' in "c|keyword"!
  232.  
  233.  Any "optional" parts of the argument should appear inside square-brackets
  234.  ('[' and ']') and a list of values is denoted by an ellipsis (" ...").
  235.  Most options will be inside of square brackets to reflect the fact that
  236.  they are "optional".
  237.  
  238.  Some example <syntax> strings follow:
  239.  
  240.     "c|keyword"                -- a required option
  241.     "[c|keyword]"              -- an option with no value
  242.     "[c|keyword value]"        -- an option that takes a value
  243.     "[c|keyword [value]]"      -- an option that takes an optional value
  244.     "[c|keyword value ...]"    -- an option that takes 1 or more values
  245.     "[c|keyword [value ...]]"  -- an option that takes 0 or more values
  246.     "value"                    -- a required positional parameter
  247.     "[value]"                  -- an optional positional-parameter
  248.     "[c|keyword] value"        -- a required argument that may be matched
  249.                                   either positionally or by keyword!
  250.  
  251.  
  252.  Further Information
  253.  -------------------
  254.  This is just a brief overview of what the CmdLine package can do. Please
  255.  read the documentation for a more thorough explanation of this products'
  256.  capabilities and limitations!
  257.  
  258.