home *** CD-ROM | disk | FTP | other *** search
-
-
- Command-Line Parsing Routines
-
- These routines are used to help you parse the C command line.
- As far as I know, I haven't used any compiler-specific code, so
- the routines should run on any C compiler.
-
- The command line consists of several strings that are passed to
- MAIN in the <argv> vector. They are not interpreted by the compiler
- at all; that's left up to the program. These parsing routines help
- the programmer sort out command-line parameters of the form:
-
- keyword and keyword=value
-
- where "keyword" is a mnemonic (for the user) and "value" is a number
- or string. The programmer may elect to give the user the option of
- abbreviating the keyword:
-
- key=value or k=value
-
- Each of the four parse routine searches the argument vector for the
- specified keyword. If it is found, that argument is DELETED FROM THE
- ARGUMENT VECTOR. If it isn't found, or if an error occurs, <argv> isn't
- changed. This is what makes these routines so useful: You don't have to
- worry about parsing keywords and then ignoring them later. As an example,
- if you're searching the command line for a flag called "-flag", and the
- command line is as follows:
-
- command start -flag end
-
- and you call the routine GET_FLAG, the command line <argv> will be
- changed to:
-
- command start end
-
- and you won't have to worry about "parsing around" the already-parsed
- parameter.
-
-
-
- The parse routines are as follows:
-
- GET_FLAG searches for a command-line parameter of the form:
-
- keyword
-
- GET_INT searches for a command-line parameter of the form:
-
- keyword=number where <number> is an integer.
-
- GET_FLOAT searches for a command-line parameter of the form:
-
- keyword=number where <number> is any number
-
- GET_CHAR searches for a command-line parameter of the form:
-
- keyword=word
- or keyword="word or words"
-
- In the latter case, the surrounding quotes are removed.
-
-
- The syntax of the routines are as follows:
-
- argc = get_xxxx (keyword, must, out, argv);
-
- <keyword> is a string containing the keyword to search for.
-
- <must> is an int and is the minimum number of characters in the
- keyword that must be there. For example, if <keyword> is "name"
- and <must> is 2, then valid keywords (successful matches) are:
- na nam name
- Invalid keywords (unsuccessful matches) are:
- n naming namer
-
- <out> is a pointer to the output, and depends on the routine:
-
- routine type returns
-
- GET_FLAG int *out 1 if found, 0 otherwise
- GET_INT int *out <number> if found; unchanged otherwise
- GET_FLOAT float *out <number> if found; unchanged otherwise
- GET_CHAR char *out <word or words> if found; unchaned otherwise
-
- <argv> is the argument vector, as passed to your MAIN routine.
-
- <argc> is the number of arguments in the argument vector after the
- call. It is negative if an error was detected.
-
-
- Error conditions
-
- An error condition (indicated by a negative return value) means that
- one or more of the following happened:
-
- <keyword> was NULL or pointed to a null string.
- <must> was zero or negative.
- <out> was a NULL pointer.
- The user specified "too much" of the keyword. For example: your
- program is looking for the keyword "-name", and one of the
- command-line parameters is -names=Jack. The extra 's' is an
- error.
- The user specified enough of the keyword, but the rest is junk. For
- example, your program is looking for the keyword "-name" and
- <must> is 3. That means that the user must specify at least "-na"
- to be recognized. If he types "-namqr", that's an error.
- The user used a character for an INT parameter that wasn't in the
- following set: "0123456789-"
- The user used a character for a FLOAT parameter that wasn't in the
- following set: "0123456789.-eE"
- The user used an equals sign for a FLAG parameter.
-
- Please note that the user won't know what parse routines that you're
- using, and what he types, he types innocently and ignorantly. That's the
- purpose of these routines: to make input more user friendly and still not
-
-
-
- burden down the programmer too much. These routines, in my mind, provide
- the user with the ability to use more "plain english" without slowing down
- the experienced user. Here's a typical command line:
-
- command input=file1 output=file2 loops=5
-
- That's a cinch to parse with my routines, and it makes sense to a beginner.
- The more experienced user, who doesn't like typing, does this:
-
- command i=file1 o=file2 l=5
-
- (assuming your program called the routines with <must> = 1)
-
-
- Conclusion
-
- Hopefully, these routines will make your job a little easier. I have
- included all the source code, and a sample program that demonstrates all
- of the options. Feel free to make copies for yourself and your friends.
- I only ask two things: a) please don't give someone copies without this
- doc file, and b) please make copies only of the original files; keep your
- "custom" versions to yourself. I ask this latter since I have worked to
- get all of the bugs out of this package, and I can't account for any bugs
- that you may inadvertently add.
-
-
- Questions, comments, etc:
-
- Eric J. Roode
- 119 Hoosick St
- Troy, NY 12180
- (current as of 6/17/87)
-
-