home *** CD-ROM | disk | FTP | other *** search
- {
-
- cmdln.c
- 6-4-90
- DOS Command line option parser
-
- Copyright 1990
- John W. Small
- All rights reserved
-
- PSW / Power SoftWare
- P.O. Box 10072
- McLean, VA 22102 8072
- (703) 759-3838
-
-
- Notes:
-
- 1. Call CmdLnParams.init() with the options string to
- parse command line arguments. The options string is a
- string of the option characters that may appear on your
- programs command line after the DOS switch character,
- '/' or '-'. If an option takes an argument then a colon
- must immediately follow that option character in the
- options string to let CmdLnParams.getOption know to look
- for the argument. The syntax for the option string is
- as follows:
-
- options string ::= (optch[:])*
-
- Your read the syntax as: An options string is zero or
- more (the "*" indicates this) option characters any of
- which may be followed by a colon to indicate that the
- option takes an argument.
-
-
- 2. GetOption returns the current option character being
- processed along with any argument in the object variable
- optArg. OptArg is valid only on options requiring
- arguments. If the argument is missing then optArg = ''.
- If the current option character being processed is
- unrecognized, i.e. not in the options string passed to
- init(), then getOption returns '?' with the
- unrecognized character stored in the object variable,
- optNot. GetOption returns #0 when there are no more
- options to process. The object variable, paramNo, is
- the index into ParamStr() of the first non-switch
- command line parameter. The object variable, optCh,
- maintains a copy of the latest value returned by
- getOption.
-
-
- 3. When your program is invoked, getOption recognizes
- clusters of command line options. A cluster is the
- switch character followed immediately by any number of
- option characters, no white space. The options clusters
- must preceed any other command line parameters since
- getOption stops processing on the first parameter that
- is not a switch. If an option takes an argument then
- the option's character must be the last option in the
- cluster with the argument immediately following or
- separated by white space. The argument must not have
- any white space, though it may contain the switch
- character.
-
- command line option cluster ::=
-
- ('/'|'-')([optch]*argch[whitespace]argument)|optch+
-
- Wow! That reads: a command line option cluster starts
- with the switch character ('/' or '-' in DOS) with one
- or more option characters (+ means one or more) or
- (| means or) any number of option characters, only the
- last of which is allowed to take an argument. The
- argument can be either tacked on to the end of the
- option cluster or stand off by itself. In either case,
- argument contains no white space! If a switch character
- appears in a cluster by itself or if two switch
- characters lead off a cluster then option parsing is
- terminated and the next parameter starts the non switched
- arguments. This allows the first non switched argument
- to start with the switch character, i.e. let the
- preceeding cluster be either a single switch character
- or lead off with two switch characters.
-
-
- 4. For example, if init is called with the options
- string, 'C:af:z', then 'C' and 'f' take arguments. A
- valid command line would be:
-
- fake /afnew /zC cmdfile outfile
-
- with repeated calls to getOption returning:
-
- 'a'
- 'f' with optArg = 'new'
- 'z'
- 'C' with optArg = 'cmdfile'
- #0 with paramNo = 4
-
-
-
- 5. After getOption has processed all options in the
- command line it will return #0. Now your application
- can pick up the rest of the command line parameters
- using the object variable, paramNo, to index ParamStr()
- for the first non switch parameter. Okay let's see all
- this in action!
-
-
- program fake;
- uses cmdln;
- var params : CmdLnParams;
- ofile : string;
- begin
- params.init('C:af:z');
- while (params.getOption <> #0) do
- case (params.optCh) of
- 'C': if length(params.optArg) > 0 then
- // process 'C' option
- else
- writeln('Option C: missing argument');
- 'a': // process 'a' option
- 'f': if length(params.optArg) > 0 then
- // process 'f' option
- else
- writeln('Option f: missing argument');
- 'z': // process 'z' option
- end;
- if (params.paramNo < ParamCount) then
- ofile := ParamStr(params.ParamNo);
- ...
- end.
-
-
- Besure to run and study cmdln.dem for a real example of
- using init(), getOption, nextCluster, and paramNo.
-
- }
-
-
- unit cmdln;
-
- interface
-
- type
-
- CmdLnParams = object
- { private: }
- options,
- cluster : string;
- option : byte;
- optEnd : boolean;
- { public: }
- optCh,
- optNot : char;
- optArg : string;
- paramNo : word;
- constructor init(opts : string);
- function getOption : char;
- procedure nextCluster;
- end;