command line argument help

This is a discussion on command line argument help within the C++ Programming forums, part of the General Programming Boards category; I've read the command line tutorial and I under stand it, but whats the "standard" process to accept arugments with ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    command line argument help

    I've read the command line tutorial and I under stand it, but whats the "standard" process to accept arugments with switches ?

    eg
    PROGRAM: myApp
    OPTIONS: -a somethingA
    -b somethingB
    -c somethingC

    how do I go about acception

    myApp -a somethingA -b somethingB
    or
    myApp -b somethingB -a somethingA

    ie in different orders?

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    I would just iterate through them. Here's a quick example I cooked up:

    Code:
    #include <iostream>
    #include <string>
    
    int parseParams(int count, char * arg[]);
    
    #define PARSE_SUCCESS    0
    #define PARSE_TOOFEW    -1
    #define PARSE_BADSWITCH -2
    
    int main(int argc, char * argv[])
    {
    	int i = parseParams(argc-1, argv + 1);
    	if (i == PARSE_SUCCESS) std::cout << "Parse Success" << std::endl;
    	if (i == PARSE_TOOFEW) std::cout << "Too Few Parameters" << std::endl;
    	if (i == PARSE_BADSWITCH) std::cout << "Unknown Switch" << std::endl;
    }
    
    int parseParams(int count, char * arg[]){
    	
    	std::string sSwitch;
    	int c = 0;
    
    	while(true){
    		if (count - c < 1) return PARSE_SUCCESS; // We're out of parameters
    		sSwitch = arg[c];
    		c++;
    
    		if (sSwitch == "-a"){
    			if (count - c < 2) return PARSE_TOOFEW;
    			std::cout << "Switch A: " << arg[c] << ", " << arg[c+1] << std::endl;
    			c += 2;
    		}
    		else if (sSwitch == "-z"){
    			std::cout << "Switch Z" << std::endl;
    		}
    		else if (sSwitch == "-m"){
    			if (count - c < 1) return PARSE_TOOFEW;
    			std::cout << "Switch M:" << arg[c] << std::endl;
    			c++;
    		}
    		else return PARSE_BADSWITCH;
    	}
    }
    Here, c is the number of parameters we've parsed (so count - c is the number left to parse).

    We use argc -1, argv + 1 because we don't want to parse the first entry of argv (which is the executable name).

    Sample output:

    C:\>test -m 4 -a "Hello there" "My friend" -z
    Switch M:4
    Switch A: Hello there, My friend
    Switch Z
    Parse Success
    Last edited by Cat; 10-11-2003 at 01:25 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 09:09 AM
  2. template argument
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2008, 04:01 AM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 12:53 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM

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