Simple Cmd Argument Parser Class In C++

Namaste Friends,





In Today's Post, I am going to share with you a simple C++ Script, That can help a newbie in Understanding Argument Parsing Concept.


Codes Section (Include Header Files)


// Include header file
#include <iostream>
#include <string>



Codes Section 2


// Option Parser Extracting Class
class OptionParser
{
 private:

  char **argv;
  int   argc;
  int   ret, b;
        std::string temp;
        std::string a;
        
  
 public:

  // Define Functions
  std::string getvalue(const char *s); // Get Argument Index In Return
  std::string getargv(int i);          // Get Argument As String In Return 
  int check(const char *s);            // check char availability and return Index
  void load(int argc, char **argv);   // Load Data 
 
};


Codes Section 3


/*  
Functions 

*/

std::string OptionParser::getvalue(const char *p){

            b=0;
            a="";

            b=check(p);

            if ((b!=0)&&((b+1)<argc)){
                
                a = getargv(b+1);
                
            }
        
            return a;

}; 

std::string OptionParser::getargv(int i){

    temp = argv[i];
    return temp;


};


Codes Section 4


int OptionParser::check(const char *s){

    ret = 0;
    temp = "";
    
    // Iter arguments
    for(int i=0; i<argc; i++){
        // Take argument
        temp = getargv(i);
    
        if(temp==s){
                ret = i;
            }

        }
    return ret;


};

void OptionParser::load(int argn, char **argm){
    argv = argm;
    argc = argn;
};


Codes Section 5


// main function
int main(int argc, char **argv){
 
 // Create Object handler
 OptionParser parse;
 
 // Load Data Into Object
 parse.load(argc, argv);
 
 // Get Loaded Argument;
        std::cout << " No. Of Argument  : " << argc << "\n";
 std::cout << " First Argument   : " << parse.getargv(argc-1) << "\n";
 
 // check input
 std::cout << " Check Input (-h) : " << parse.check("-h") << "\n";
 
        if (parse.check("-h"))
     {
      //std::string x;
  std::cout << " Get Value Of h  : " << parse.getvalue("-h") << "\n";
     }

 
 
 return 0;
}


Codes Section 6


Complete Code


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
// Include header file
#include <iostream>
#include <string>


// Option Parser Extracting Class
class OptionParser
{
 private:

  char **argv;
  int   argc;
  int   ret, b;
        std::string temp;
        std::string a;
        
  
 public:

  // Define Functions
  std::string getvalue(const char *s); // Get Argument Index In Return
  std::string getargv(int i);          // Get Argument As String In Return 
  int check(const char *s);            // check char availability and return Index
  void load(int argc, char **argv);   // Load Data 
 
};

/*  
Functions 

*/

std::string OptionParser::getvalue(const char *p){

            b=0;
            a="";

            b=check(p);

            if ((b!=0)&&((b+1)<argc)){
                
                a = getargv(b+1);
                
            }
        
            return a;

}; 

std::string OptionParser::getargv(int i){

    temp = argv[i];
    return temp;


};

int OptionParser::check(const char *s){

    ret = 0;
    temp = "";
    
    // Iter arguments
    for(int i=0; i<argc; i++){
        // Take argument
        temp = getargv(i);
    
        if(temp==s){
                ret = i;
            }

        }
    return ret;


};

void OptionParser::load(int argn, char **argm){
    argv = argm;
    argc = argn;
};




// main function
int main(int argc, char **argv){
 
 // Create Object handler
 OptionParser parse;
 
 // Load Data Into Object
 parse.load(argc, argv);
 
 // Get Loaded Argument;
        std::cout << " No. Of Argument  : " << argc << "\n";
 std::cout << " First Argument   : " << parse.getargv(argc-1) << "\n";
 
 // check input
 std::cout << " Check Input (-h) : " << parse.check("-h") << "\n";
 
        if (parse.check("-h"))
     {
      //std::string x;
  std::cout << " Get Value Of h  : " << parse.getvalue("-h") << "\n";
     }

 
 
 return 0;
}



After Compiling These Codes, Run To Test it.



C_Programs :~# ./argument -h bitforestinfo
 No. Of Argument  : 3
 First Argument   : bitforestinfo
 Check Input (-h) : 1
 Get Value Of h  : bitforestinfo


Share this

Related Posts

Previous
Next Post »