Command line parameters - argc and argv[]

E

ern

Is there a standard (or easy) way to parse parameters (configs) that
come from main(int argc, char *argv[]) ?

For example,

If I have the following configurations:

-s <string> // pass a string to main

-f <file> // pass a file to main

-i <integer> // pass an integer to main

-d // set the 'doStuff' flag

as well as being able to send any arbitrary command to main(), are
there any proven methods of parsing the configurations? It seems like
option-parsing code would already exist, but I can't find any. The
option-parsing in the code I'm debugging is messy and unacceptable.
I'd like to start from scratch. For Python minded people, I'm looking
for an equivalent to 'optparse'.
 
C

Christopher Benson-Manica

ern said:
Is there a standard (or easy) way to parse parameters (configs) that
come from main(int argc, char *argv[]) ?

There's nothing included in the standard library, although it's
certainly possible to implement something that does this in standard
C. If you're on a Unix system, check out getopt(); you can get more
help with that on comp.unix.programmer.
 
W

Walter Roberson

Is there a standard (or easy) way to parse parameters (configs) that
come from main(int argc, char *argv[]) ?
For example,
If I have the following configurations:
-s <string> // pass a string to main

It isn't part of the C language itself, but many systems
provide routines named getopt() or similar; there are fully
portable versions of the routine readily available.

Different option parsers offer different facilities. For example,
many of the getopt() routines do not offer a mechanism to type-check
provided arguments, nor to convert the provided argument from
string to integer (or float, or enumeration type, or whatever): they
just break out the strings and let you deal with the semantics.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top