Command line parameters - argc and argv[]

Discussion in 'C Programming' started by ern, Aug 17, 2006.

  1. ern

    ern Guest

    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'.
    ern, Aug 17, 2006
    #1
    1. Advertising

  2. ern <> wrote:

    > 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.
    Christopher Benson-Manica, Aug 17, 2006
    #2
    1. Advertising

  3. In article <>,
    ern <> wrote:
    >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
    Walter Roberson, Aug 17, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. userfriendly

    argc and argv

    userfriendly, Feb 29, 2004, in forum: C++
    Replies:
    3
    Views:
    5,985
    Buster
    Mar 1, 2004
  2. Claudio Varini

    use of argc and argv

    Claudio Varini, Oct 28, 2004, in forum: C++
    Replies:
    3
    Views:
    2,120
    Howard
    Oct 28, 2004
  3. Billy Patton

    How to alter argc and argv

    Billy Patton, May 31, 2005, in forum: C++
    Replies:
    5
    Views:
    850
    red floyd
    Jun 3, 2005
  4. Hal Styli
    Replies:
    14
    Views:
    1,613
    Old Wolf
    Jan 20, 2004
  5. scooper
    Replies:
    0
    Views:
    430
    scooper
    Oct 15, 2009
Loading...

Share This Page