C++ Parsed Command Line Arguments


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: C++ Parsed Command Line Arguments

  1. #1
    Join Date
    May 2007
    Posts
    843

    Thumbs up C++ Parsed Command Line Arguments

    Hello all expert C++ programmer,

    i have a noob question which about the command line arguments.

    My program requirements.

    Create a new class 'Options' whose constructor accepts command line arguments (int argc, char **argv).

    This class should have no dependancies on existing RKB-B code.

    The class should parse the parameters and store the results internally.

    The class should provide 3 public methods (it can have as many private methods as are necessary):

    bool isSet(std::string opt)
    bool getOptionValues(std::string opt, std::vector<std::string> & values)
    void getInvalidOptions(std::vector<std::string> & options)


    isSet returns true if opt was specified on the command line.

    getOptionValues populates the vector 'values' with the values of the cmd-line option. It returns false if the option is not set.


    getInvalidOptions populates the vector 'options' with the values of cmd-line options that we don't support. The list of valid options can be found by downloading the latest version of RKH and looking in the 'rkhunter' script.


    The 5 acceptable formats for options are shown below. Also shown are the tests that should be run and the expected results.

    1. -x
    * isSet("x") returns true
    2. -xy
    * isSet("x") returns true.
    * isSet("y") returns true.
    * isSet("xy") returns false
    3. --word
    * isSet("word") returns true.
    * isSet("w") returns false.
    * getOptionValues("word", values) returns true, values is an empty vector
    4. --word value
    * isSet("word") returns true.
    * isSet("w") returns false.
    * getOptionValues("word", values) returns true, values contains 1 entry
    5. --word "value1 value2 ... valueN"
    * isSet("word") returns true.
    * isSet("w") returns false.
    * getOptionValues("word", values) returns true, values contains N entries>



    To test this class you create a new class called 'OptionsTest'
    OptionsTest should have the 13 tests outlines above implemented as well as any other tests you think are relevant.



    Missing:

    # Your constructor doesn't actually do anything. It should split the argc variable up into the different options. So for example if someone types "rkhb -w --word value -z --test "v1 v2" on the command line the line should be parsed into the 4 different options. You should be able to use google to find techniques to do this.
    # Create a OptionsTest.h and OptionsTest.cpp. Put the CppUnit code in here.

    My program so far.

    Code:
    #ifndef _Options_
    #define _Options_
    
    #include<vector>
    #include<string>
    #include<cstdlib>
    #include<cctype>
    #include "Invalid.h"
    
    using std::string;
    using std::vector;
    using std::iterator;
    
    using namespace std;
    
    class options
    {
    	string opt, vector<string> values;
    public:
    	options(int argc, char *argv[])
    	{
    		int loop;
    		for (loop=0;loop<argc;++loop)
    		{
    		}
    	}
    		
    
    	bool isSet(string opt)
    	{
    		//bool state = false; 
    		try
    		{
    			if(isalnum(opt))
    			{
    				invalid error("Cannot Enter Numeric as options");
    				throw error;
    			}
    		}
    		catch(invalid &error)
    		{
    			cout << error.getDescription();
    		}
    
    		if (opt.empty())
    		{
    			return false;
    		}// If not set means the string is empty, return false
    		else
    		{
    			return true;
    		}// If set means the string is not empty, return true
    
    	bool getOptionsValues(string opt, vector<string>&values)
    	{
    		int loop = 0;
    		while(!values.empty())
    		{
    			values[loop].push_back(values);
    			loop++;
    		}
    	
    		if (opt.empty() || values.empty())
    		{
    			return false;
    		}
    		else
    		{
    			return true;
    		}
    	}
    		
    
    
    	~options(){}
    };
    
    #endif

    Code:
    // Exception Class
    
    #ifndef _Invalid_
    #define _Invalid_
    
    #include<string>
    
    using std::string;
    
    class invalid
    {
    	string description;
    public:
    	invalid(string description) :description(description){}
    
    	string getDescription()
    	{
    		return description;
    	}
    	~invalid(){}
    };
    
    #endif;
    Thanks for your help.
    Your help is greatly appreciated by me and others.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    can you ask a question? trying to muddle through your code in parallel with your assignment to determine what you have and have not yet done is a great deal of effort.

  3. #3
    Join Date
    May 2007
    Posts
    843
    My question is how to analysis the command line arguments ?"rkhb -w --word value -z --test "v1 v2" on the command line the line should be parsed into the 4 different options.

    Thanks for your effort. I very appreciated your help.

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    use the argc value to determing how many commandline things were passed in and loop over them. The inside of the loop would examine the value and determine what to do next (set a flag, wait to read the next option, or whatever your tasks are for each option).

    The main options will be exact match (string) while the parameters to options (these follow the options) could be anything.

Similar Threads

  1. Replies: 2
    Last Post: 12-05-2006, 07:05 AM
  2. Command Line Arguments
    By Kevin in forum .NET
    Replies: 7
    Last Post: 07-24-2002, 02:27 PM
  3. Command Line Arguments in C#
    By Vinay Thombre in forum .NET
    Replies: 4
    Last Post: 07-09-2002, 10:23 PM
  4. optional arguments in a VBScript subprocedure?
    By Robert Dyke in forum ASP.NET
    Replies: 3
    Last Post: 08-07-2001, 02:17 PM
  5. Using VB's Conditional Compilation Arguments
    By Larry Rebich in forum vb.announcements
    Replies: 0
    Last Post: 05-22-2000, 01:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center