468,512 Members | 1,457 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Command line parameters - argc and argv[]

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'.

Aug 17 '06 #1
2 3272
ern <er*******@gmail.comwrote:
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.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 17 '06 #2
In article <11**********************@i3g2000cwc.googlegroups. com>,
ern <er*******@gmail.comwrote:
>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.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Aug 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Bernd Danberg | last post: by
25 posts views Thread by David Bernier | last post: by
34 posts views Thread by Roman Mashak | last post: by
3 posts views Thread by kitty | last post: by
40 posts views Thread by raphfrk | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.