CLAP
(Command Line Argument Parsing)
Version 0.9
CLAP is a C++ package that enables a programmer to define and parse
command line arguments easily.
It was developed by the developers of the
Computational Geometry
Projects in the Computer
Science Department of
Ben-Gurion University
of the Negev, Israel.
This page was designed by
Haggai David.
Send your comments to
davidcha@cs.bgu.ac.il
Copyright © 1997 Haggai David,
All rights reserved.
Introduction
The purpose of this package is to make life easier of parsing the command
line arguments.
In your program each command line argument should be presented as an
object of its type, but first you should get familiar with the container
of the command argument objects.
CmdLine
CmdLine(const char*
progName, CmdArg* ... )
Constructs new CmdLine object.
Parameters:
progName - the program name (your program)
CmdArg* - variable number of pointers to Command Line Arguments (The
last argument must be NULL).
parse(int argc, char*
argv[])
Parses the command line and checks the validity of the supplied arguments.
Parameters:
argc, argv - as passed to main()
usage(void)
Displays a usage information based on the definitions of the specific
arguments.
CmdArg - Introduction
CmdArg is a base class of all Command Line Argument classes. It supplies
the following:
enum CmdArg::e_CmdArgSyntax
| Name |
Value |
Meaning |
| isOPT |
0x01 |
argument is optional |
| isREQ |
0x02 |
argument is required |
| isVALOPT |
0x04 |
argument value is optional |
| isVALREQ |
0x08 |
argument value is required |
| isHIDDEN |
0x10 |
argument is not to be printed in usage |
all bitwise operations are allowed
For example: (CmdArg::isOPT | CmdArg::isVALREQ) means that
the argument is optional for the program but if the argument is supplied
then a value must be supplied as well.
CmdArgInt
CmdArgInt(
const char optChar,
cosnt char *keyword,
char *valueName,
char *description,
unsigned int syntaxFlags = (CmdArg::isREQ | CmdArg::isVALREQ),
int defaultValue
= 0)
Constructs new Command Line Argument that represents an integer value.
Parameters:
optChar - optional char to replace the keyword.
keyword - the keyword of the variable.
valueName - the name of the value to be displayed in the usage part.
description - the description of the argument that will be displayed
in the usage part.
syntaxFlags - the appearance definition of the argument.
defaultValue - default integer value (more effective when using the
isOPT option).
operator int(void)
CmdArgFloat
CmdArgFloat(
const char optChar,
cosnt char *keyword,
char *valueName,
char *description,
unsigned int syntaxFlags = (CmdArg::isREQ | CmdArg::isVALREQ),
double defaultValue = 0)
Constructs new Command Line Argument that represent a float value.
Parameters:
optChar - optional char to replace the keyword.
keyword - the keyword of the variable.
valueName - the name of the value to be displayed in the usage part.
description - the description of the argument that will be displayed
in the usage part.
syntaxFlags - the appearance definition of the argument.
defaultValue - default value (more effective when using the isOPT option).
operator float(void)
Casting operator to float.
CmdArgBool
CmdArgBool(
const char optChar,
cosnt char *keyword,
char *description,
unsigned int syntaxFlags = (CmdArg::isOPT | CmdArg::isVALOPT))
Constructs new Command Line Argument that represent a boolean value.
If the keyword was supplied in the command line than it has true
value otherwise it has false value.
Parameters:
optChar - optional char to replace the keyword.
keyword - the keyword of the variable.
description - the description of the argument that will be displayed
in the usage part.
syntaxFlags - the appearance definition of the argument.
operator bool(void)
Casting operator to bool.
CmdArgChar
CmdArgChar(
const char optChar,
cosnt char *keyword,
char *valueName,
char *description,
unsigned int syntaxFlags = (CmdArg::isREQ | CmdArg::isVALREQ),
char defaultValue
= '\0')
Constructs new Command Line Argument that represent char value.
Parameters:
optChar - optional char to replace the keyword.
keyword - the keyword of the variable.
valueName - the name of the value to be displayed in the usage part.
description - the description of the argument that will be displayed
in the usage part.
syntaxFlags - the appearance definition of the argument.
defaultValue - default char value (more effective when using the isOPT
option).
operator char(void)
Casting operator to char.
CmdArgStr
CmdArgStr(
const char optChar,
cosnt char *keyword,
char *valueName,
char *description,
unsigned int syntaxFlags = (CmdArg::isREQ | CmdArg::isVALREQ),
char* defaultValue =
NULL)
Constructs new Command Line Argument that represent an integer value.
Parameters:
optChar - optional char to replace the keyword.
keyword - the keyword of the variable.
valueName - the name of the value to be displayed in the usage part.
description - the description of the argument that will be displayed
in the usage part.
syntaxFlags - the appearance definition of the argument.
defaultValue - default string (more effective when using the isOPT
option).
operator char*(void)
Casting operator to char*.
CmdArgLists - Introduction
The package gives an option to get lists from the command line by using
the lists objects.
The lists objects has the following interface:
int size(void)
returns the actual size of the list (i.e. the number of arguments supplied
in the command line).
void reset(void)
resets the interior list iterator to the beginning of the list.
Type operator[](int
index)
random access to the list. Type is the type of the
list elements (treats the list as an array).
Parameters:
index - the index in the list.
Each list object can initialize with minimum and maximum expected arguments
and with various delimiters.
CmdArgIntList
CmdArgIntList(
const char optChar,
cosnt char *keyword,
char *valueName,
char *description,
unsigned int syntaxFlags = (CmdArg::isREQ | CmdArg::isVALREQ),
int minSize
= 1,
int maxSize
= 100,
const char *delimiters = ",~/")
Constructs an empty list of integers.
Parameters:
optChar - optional char to replace the keyword.
keyword - the keyword of the variable.
valueName - the name of the value of an element to be displayed in
the usage part.
description - the description of the argument that will be displayed
in the usage part.
syntaxFlags - the appearance definition of the argument.
minSize - minimum expected size of the list.
maxSize - maximum expected size of the list.
delimiters - string that contains the delimiters between items of the
list.
CmdArgFloatList
CmdArgFloatList(
const char optChar,
cosnt char *keyword,
char *valueName,
char *description,
unsigned int syntaxFlags = (CmdArg::isREQ | CmdArg::isVALREQ),
int minSize
= 1,
int maxSize
= 100,
const char *delimiters = ",~/")
Constructs an empty list of float numbers.
Parameters:
optChar - optional char to replace the keyword.
keyword - the keyword of the variable.
valueName - the name of the value of an element to be displayed in
the usage part.
description - the description of the argument that will be displayed
in the usage part.
syntaxFlags - the appearance definition of the argument.
minSize - minimum expected size of the list.
maxSize - maximum expected size of the list.
delimiters - string that contains the delimiters between items of the
list.
CmdArgCharList
CmdArgCharList(
const char optChar,
cosnt char *keyword,
char *valueName,
char *description,
unsigned int syntaxFlags = (CmdArg::isREQ | CmdArg::isVALREQ),
int minSize
= 1,
int maxSize
= 100,
const char *delimiters = ",~/.-")
Constructs an empty list of characters.
Parameters:
optChar - optional char to replace the keyword.
keyword - the keyword of the variable.
valueName - the name of the value of an element to be displayed in
the usage part.
description - the description of the argument that will be displayed
in the usage part.
syntaxFlags - the appearance definition of the argument.
minSize - minimum expected size of the list.
maxSize - maximum expected size of the list.
delimiters - string that contains the delimiters between items of the
list.
CmdArgStrList
CmdArgStrList(
const char optChar,
cosnt char *keyword,
char *valueName,
char *description,
unsigned int syntaxFlags = (CmdArg::isREQ | CmdArg::isVALREQ),
int minSize
= 1,
int maxSize
= 100,
const char *delimiters = ",~/.-")
Constructs an empty list of strings.
Parameters:
optChar - optional char to replace the keyword.
keyword - the keyword of the variable.
valueName - the name of the value of an element to be displayed in
the usage part.
description - the description of the argument that will be displayed
in the usage part.
syntaxFlags - the appearance definition of the argument.
minSize - minimum expected size of the list.
maxSize - maximum expected size of the list.
delimiters - string that contains the delimiters between items of the
list.
How To Use
All you need to do in order to combine the package in your program:
-
Include Files
-
The relevant include files are in ~cgproj/package/include/
-
use the directive #include <cmdline.hh>
-
libraries
-
The relevant library is in ~cgproj/package/lib/
-
link with the library libcmd.a
-
Makefiles
-
Use the example
to compile it right.
Simple Example
This example demonstrates handling of three command line arguments:
| Name |
Type |
Default Value |
Syntax |
| height |
int |
0 |
isREQ | isVALREQ |
| width |
int |
10 |
isREQ | isVALREQ |
| filename |
char* |
unname.txt |
isOPT | isVALREQ |
You can find the source and running examples here.
More Complex Example
This example demonstrates the use of booleans chars and floats
You can find the source and running example here.
Bug Reports
For users who want to report for bugs please mail to davidcha@cs.bgu.ac.il
Questions are welcome as well.
Thanks
To friends who helped.
This page was designed by Haggai
David.
Send your comments to davidcha@cs.bgu.ac.il
Copyright © 1997 Haggai David, All rights reserved.
Back