Processing command line args...

This is a discussion on Processing command line args... within the C Programming forums, part of the General Programming Boards category; Is there a "standard" or typical way to process command-line arguments in argv? In the past, I've always just used ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    21

    Processing command line args...

    Is there a "standard" or typical way to process command-line arguments in argv?

    In the past, I've always just used simple if statements, but that is when the args needed to be provided in a specific order. I now have an assignment that requires that the application be invoked like this:
    Code:
    progname -o outfile n q
    where n and q are just integers and are unrelated to each other.

    The way I interpret this is that since the output file is specified with the -o switch, the arguments could be passed in any order at the CLI as long as the integers n & q are in the proper order. For example:
    Code:
    progname n -o outfile q
    would be a valid call. This makes my simple if statements that check the indices of argv in a sequential manner obsolete.

    I know I could handle this in several ways without much difficulty, but it might be messy. If there is a standard way to handle this situation, since so many programs use such switches, then I might as well do it properly so that I can do it this way from now on.

    If someone could point me in the right direction, either with a URL, or link, a keyword to Google on, or even just an idea, it would be much appreciated.

    Cheers,
    ~arker

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    There are different libraries on different systems. You might as well do it yourself or look for something online. I just did a google on "command line parser" and found some cool stuff.

    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    21
    Originally posted by vVv

    But you can't just ``throw in a bunch of numbers'' and expect it to parse that properly.
    That's what I thought. It seemed kinda silly that some of the arguments in the specs had switches and some did not.

    It seems that I won't be doing this in any standard way then. But thanks for the ideas and information. I will most certainly use them when the specs given are more 'conventional'.

    Cheers,
    ~arker

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Piping/Redirection/Background processing help needed
    By cr80expert5 in forum C Programming
    Replies: 1
    Last Post: 04-27-2009, 10:25 PM
  2. processing command line args
    By bhttogm in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2008, 10:30 AM
  3. Using a lot of processing time!
    By nickname_changed in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2003, 03:44 AM
  4. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21