diff options
author | lloyd <[email protected]> | 2014-11-12 02:38:28 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-11-12 02:38:28 +0000 |
commit | 17349a1fc49d604f8160f2077538fdf397b702c6 (patch) | |
tree | 2adc34773a5820fbfeac39b17debd32d5570cc8a /src | |
parent | 62ff505146cac307ca751109297332035bdc8b73 (diff) |
Add missing file
Diffstat (limited to 'src')
-rw-r--r-- | src/cmd/getopt.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/cmd/getopt.cpp b/src/cmd/getopt.cpp new file mode 100644 index 000000000..74e6723cb --- /dev/null +++ b/src/cmd/getopt.cpp @@ -0,0 +1,50 @@ +#include "getopt.h" +#include <iostream> + +void OptionParser::parse(char* argv[]) + { + std::vector<std::string> args; + for(int j = 1; argv[j]; j++) + args.push_back(argv[j]); + + for(size_t j = 0; j != args.size(); j++) + { + std::string arg = args[j]; + + if(arg == "help" || arg == "--help" || arg == "-h") + return help(std::cout, argv[0]); + + if(arg.size() > 2 && arg[0] == '-' && arg[1] == '-') + { + const std::string opt_name = arg.substr(0, arg.find('=')); + + arg = arg.substr(2); + + std::string::size_type mark = arg.find('='); + OptionFlag opt = find_option(arg.substr(0, mark)); + + if(opt.takes_arg()) + { + if(mark == std::string::npos) + throw std::runtime_error("Option " + opt_name + + " requires an argument"); + + std::string name = arg.substr(0, mark); + std::string value = arg.substr(mark+1); + + options[name] = value; + } + else + { + if(mark != std::string::npos) + throw std::runtime_error("Option " + opt_name + + " does not take an argument"); + + options[arg] = ""; + } + } + else + leftover.push_back(arg); + } + } + |