aboutsummaryrefslogtreecommitdiffstats
path: root/checks
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-01-03 23:07:53 +0000
committerlloyd <[email protected]>2010-01-03 23:07:53 +0000
commita45919c99b36f4440b74e1b0488127c0c6625409 (patch)
tree0ec9b9d64293bb17ec3e856628cbd57d2f2711e7 /checks
parentf153d181968fef80e0d76c449b243b250eed2777 (diff)
Completely inline getopt.cpp into getopt.h so it can be used in toolbox and other examples
Diffstat (limited to 'checks')
-rw-r--r--checks/getopt.cpp90
-rw-r--r--checks/getopt.h91
2 files changed, 85 insertions, 96 deletions
diff --git a/checks/getopt.cpp b/checks/getopt.cpp
deleted file mode 100644
index 23cb2a9fa..000000000
--- a/checks/getopt.cpp
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
-* (C) 2009 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#include "getopt.h"
-
-#include <botan/parsing.h>
-#include <botan/exceptn.h>
-
-OptionParser::OptionParser(const std::string& opt_string)
- {
- std::vector<std::string> opts = Botan::split_on(opt_string, '|');
-
- for(size_t j = 0; j != opts.size(); j++)
- flags.push_back(OptionFlag(opts[j]));
- }
-
-OptionParser::OptionFlag
-OptionParser::find_option(const std::string& name) const
- {
- for(size_t j = 0; j != flags.size(); j++)
- if(flags[j].name() == name)
- return flags[j];
- throw Botan::Exception("Unknown option " + name);
- }
-
-bool OptionParser::is_set(const std::string& key) const
- {
- return (options.find(key) != options.end());
- }
-
-std::string OptionParser::value(const std::string& key) const
- {
- std::map<std::string, std::string>::const_iterator i = options.find(key);
- if(i == options.end())
- throw Botan::Exception("Option " + key + " not found");
- return i->second;
- }
-
-std::string OptionParser::value_if_set(const std::string& key) const
- {
- return is_set(key) ? value(key) : "";
- }
-
-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.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 Botan::Exception("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 Botan::Exception("Option " + opt_name +
- " does not take an argument");
-
- options[arg] = "";
- }
- }
- else
- leftover.push_back(arg);
- }
- }
diff --git a/checks/getopt.h b/checks/getopt.h
index 68e6bd874..67cf979f9 100644
--- a/checks/getopt.h
+++ b/checks/getopt.h
@@ -9,19 +9,92 @@
#include <string>
#include <vector>
+#include <stdexcept>
#include <map>
+#include <botan/parsing.h>
+
class OptionParser
{
public:
std::vector<std::string> leftovers() const { return leftover; }
- bool is_set(const std::string&) const;
- std::string value(const std::string&) const;
- std::string value_if_set(const std::string&) const;
+ bool is_set(const std::string& key) const
+ {
+ return (options.find(key) != options.end());
+ }
+
+ std::string value(const std::string& key) const
+ {
+ std::map<std::string, std::string>::const_iterator i = options.find(key);
+ if(i == options.end())
+ throw std::runtime_error("Option " + key + " not found");
+ return i->second;
+ }
+
+ std::string value_if_set(const std::string& key) const
+ {
+ return value_or_else(key, "");
+ }
+
+ std::string value_or_else(const std::string& key,
+ const std::string& or_else) const
+ {
+ return is_set(key) ? value(key) : or_else;
+ }
+
+ void 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.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);
+ }
+ }
+
+ OptionParser(const std::string& opt_string)
+ {
+ std::vector<std::string> opts = Botan::split_on(opt_string, '|');
+
+ for(size_t j = 0; j != opts.size(); j++)
+ flags.push_back(OptionFlag(opts[j]));
+ }
- void parse(char*[]);
- OptionParser(const std::string&);
private:
class OptionFlag
{
@@ -40,7 +113,13 @@ class OptionParser
bool opt_takes_arg;
};
- OptionFlag find_option(const std::string&) const;
+ OptionFlag find_option(const std::string& name) const
+ {
+ for(size_t j = 0; j != flags.size(); j++)
+ if(flags[j].name() == name)
+ return flags[j];
+ throw std::runtime_error("Unknown option " + name);
+ }
std::vector<OptionFlag> flags;
std::map<std::string, std::string> options;