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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| #include <string>
#include <vector>
#include <map>
#include <exception>
#include <utility>
#include <algorithm>
#include <iostream>
class ArgumentParser
{
struct Option {
std::string name;
std::string shortcut;
std::string defaultValue;
bool valueMandatory;
};
std::map< std::string, std::string > m_options;
std::vector< Option > m_possible;
void AddOption(std::string const & iName, std::string const & iDefaultValue, bool iValueMandatory)
{
Option option;
option.name = iName;
option.shortcut = iName.substr(0,1);
option.defaultValue = iDefaultValue;
option.valueMandatory = iValueMandatory;
m_possible.push_back(option);
}
public:
ArgumentParser()
{}
virtual ~ArgumentParser()
{}
void AddOption(std::string const & iName)
{
AddOption(iName,std::string(""),false);
}
void AddOption(std::string const & iName, char * const iDefaultValue)
{
AddOption(iName,std::string(iDefaultValue),false);
}
void AddOption(std::string const & iName, std::string const & iDefaultValue)
{
AddOption(iName,iDefaultValue,false);
}
void AddOption(std::string const & iName, bool iValueMandatory)
{
AddOption(iName,std::string(""),iValueMandatory);
}
void ParseCommandeLine(int iArgc, char * iArgv[])
{
std::vector< std::string > cmdContent;
for(int i = 0; i < iArgc; i++)
cmdContent.push_back(std::string(iArgv[i]));
for(std::vector< Option >::iterator it = m_possible.begin(); it != m_possible.end(); it++)
{
std::vector< std::string >::iterator foundOption;
std::string pattern = std::string("--") + it -> name;
foundOption = std::find(cmdContent.begin(), cmdContent.end(), pattern);
if(foundOption == cmdContent.end() )
{
pattern = std::string("-") + it -> shortcut;
foundOption = std::find(cmdContent.begin(), cmdContent.end(), pattern);
}
if(foundOption != cmdContent.end() )
{
std::vector< std::string >::iterator nextText = foundOption + 1;
bool hasNext = (nextText != cmdContent.end());
bool nextIsOption = hasNext ? (nextText -> substr(0,1) == "-") : true;
if(it -> valueMandatory && nextIsOption)
{
throw std::exception("Valeur obligatoire pour une option n'a pas été spécifiée");
}
else
{
if(nextIsOption)
m_options.insert(std::make_pair(it -> name, it -> defaultValue));
else
m_options.insert(std::make_pair(it -> name, *nextText));
}
}
}
}
bool HasValue(std::string const & iName)
{
return (0 < m_options.count(iName));
}
bool GetValue(std::string const & iName, std::string& iValue)
{
bool result = HasValue(iName);
if(result)
iValue = m_options[iName];
return result;
}
};
int main(int argc, char* argv[])
{
ArgumentParser options;
options.AddOption("help");
options.AddOption("roger","ouai");
options.AddOption("marcel",true);
options.ParseCommandeLine(argc,argv);
if(options.HasValue("help"))
{
std::cout << "Affichage de l'aide !" << std::endl;
}
else
{
std::string RogerValue;
if(options.GetValue("roger",RogerValue))
std::cout << "Roger vaut : " << RogerValue << std::endl;
std::string MarcelValue;
if(options.GetValue("marcel",MarcelValue))
std::cout << "Marcel vaut : " << MarcelValue << std::endl;
}
return 0;
} |
Partager