| 1 |
#ifndef OPTIONS_HPP |
| 2 |
#define OPTIONS_HPP |
| 3 |
/* |
| 4 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 5 |
* or more contributor license agreements. See the NOTICE file |
| 6 |
* distributed with this work for additional information |
| 7 |
* regarding copyright ownership. The ASF licenses this file |
| 8 |
* to you under the Apache License, Version 2.0 (the |
| 9 |
* "License"); you may not use this file except in compliance |
| 10 |
* with the License. You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, |
| 15 |
* software distributed under the License is distributed on an |
| 16 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 |
* KIND, either express or implied. See the License for the |
| 18 |
* specific language governing permissions and limitations |
| 19 |
* under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <sstream> |
| 24 |
#include <ostream> |
| 25 |
#include <vector> |
| 26 |
#include <stdexcept> |
| 27 |
|
| 28 |
namespace example { |
| 29 |
/** bad_option is thrown for option parsing errors */ |
| 30 |
struct bad_option : public std::runtime_error { |
| 31 |
bad_option(const std::string& s) : std::runtime_error(s) {} |
| 32 |
}; |
| 33 |
|
| 34 |
/** Simple command-line option parser for example programs */ |
| 35 |
class options { |
| 36 |
public: |
| 37 |
|
| 38 |
options(int argc, char const * const * argv) : argc_(argc), argv_(argv), prog_(argv[0]), help_() { |
| 39 |
size_t slash = prog_.find_last_of("/\\"); |
| 40 |
if (slash != std::string::npos) |
| 41 |
prog_ = prog_.substr(slash+1); // Extract prog name from path |
| 42 |
add_flag(help_, 'h', "help", "Print the help message"); |
| 43 |
} |
| 44 |
|
| 45 |
~options() { |
| 46 |
for (opts::iterator i = opts_.begin(); i != opts_.end(); ++i) |
| 47 |
delete *i; |
| 48 |
} |
| 49 |
|
| 50 |
/** Updates value when parse() is called if option is present with a value. */ |
| 51 |
template<class T> |
| 52 |
void add_value(T& value, char short_name, const std::string& long_name, const std::string& description, const std::string var) { |
| 53 |
opts_.push_back(new option_value<T>(value, short_name, long_name, description, var)); |
| 54 |
} |
| 55 |
|
| 56 |
/** Sets flag when parse() is called if option is present. */ |
| 57 |
void add_flag(bool& flag, char short_name, const std::string& long_name, const std::string& description) { |
| 58 |
opts_.push_back(new option_flag(flag, short_name, long_name, description)); |
| 59 |
} |
| 60 |
|
| 61 |
/** Parse the command line, return the index of the first non-option argument. |
| 62 |
*@throws bad_option if there is a parsing error or unknown option. |
| 63 |
*/ |
| 64 |
int parse() { |
| 65 |
int arg = 1; |
| 66 |
for (; arg < argc_ && argv_[arg][0] == '-'; ++arg) { |
| 67 |
opts::iterator i = opts_.begin(); |
| 68 |
while (i != opts_.end() && !(*i)->parse(argc_, argv_, arg)) |
| 69 |
++i; |
| 70 |
if (i == opts_.end()) |
| 71 |
throw bad_option(std::string("unknown option ") + argv_[arg]); |
| 72 |
} |
| 73 |
if (help_) throw bad_option(""); |
| 74 |
return arg; |
| 75 |
} |
| 76 |
|
| 77 |
/** Print a usage message */ |
| 78 |
friend std::ostream& operator<<(std::ostream& os, const options& op) { |
| 79 |
os << std::endl << "usage: " << op.prog_ << " [options]" << std::endl; |
| 80 |
os << std::endl << "options:" << std::endl; |
| 81 |
for (opts::const_iterator i = op.opts_.begin(); i < op.opts_.end(); ++i) |
| 82 |
os << **i << std::endl; |
| 83 |
return os; |
| 84 |
} |
| 85 |
|
| 86 |
private: |
| 87 |
class option { |
| 88 |
public: |
| 89 |
option(char s, const std::string& l, const std::string& d, const std::string v) : |
| 90 |
short_(std::string("-") + s), long_("--" + l), desc_(d), var_(v) {} |
| 91 |
virtual ~option() {} |
| 92 |
|
| 93 |
virtual bool parse(int argc, char const * const * argv, int &i) = 0; |
| 94 |
virtual void print_default(std::ostream&) const {} |
| 95 |
|
| 96 |
friend std::ostream& operator<<(std::ostream& os, const option& op) { |
| 97 |
os << " " << op.short_; |
| 98 |
if (!op.var_.empty()) os << " " << op.var_; |
| 99 |
os << ", " << op.long_; |
| 100 |
if (!op.var_.empty()) os << "=" << op.var_; |
| 101 |
os << std::endl << " " << op.desc_; |
| 102 |
op.print_default(os); |
| 103 |
return os; |
| 104 |
} |
| 105 |
|
| 106 |
protected: |
| 107 |
std::string short_, long_, desc_, var_; |
| 108 |
}; |
| 109 |
|
| 110 |
template <class T> |
| 111 |
class option_value : public option { |
| 112 |
public: |
| 113 |
option_value(T& value, char s, const std::string& l, const std::string& d, const std::string& v) : |
| 114 |
option(s, l, d, v), value_(value) {} |
| 115 |
|
| 116 |
bool parse(int argc, char const * const * argv, int &i) { |
| 117 |
std::string arg(argv[i]); |
| 118 |
if (arg == short_ || arg == long_) { |
| 119 |
if (i < argc-1) { |
| 120 |
set_value(arg, argv[++i]); |
| 121 |
return true; |
| 122 |
} else { |
| 123 |
throw bad_option("missing value for " + arg); |
| 124 |
} |
| 125 |
} |
| 126 |
if (arg.compare(0, long_.size(), long_) == 0 && arg[long_.size()] == '=' ) { |
| 127 |
set_value(long_, arg.substr(long_.size()+1)); |
| 128 |
return true; |
| 129 |
} |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
virtual void print_default(std::ostream& os) const { os << " (default " << value_ << ")"; } |
| 134 |
|
| 135 |
void set_value(const std::string& opt, const std::string& s) { |
| 136 |
std::istringstream is(s); |
| 137 |
is >> value_; |
| 138 |
if (is.fail() || is.bad()) |
| 139 |
throw bad_option("bad value for " + opt + ": " + s); |
| 140 |
} |
| 141 |
|
| 142 |
private: |
| 143 |
T& value_; |
| 144 |
}; |
| 145 |
|
| 146 |
class option_flag: public option { |
| 147 |
public: |
| 148 |
option_flag(bool& flag, const char s, const std::string& l, const std::string& d) : |
| 149 |
option(s, l, d, ""), flag_(flag) |
| 150 |
{ flag_ = false; } |
| 151 |
|
| 152 |
bool parse(int /*argc*/, char const * const * argv, int &i) { |
| 153 |
if (argv[i] == short_ || argv[i] == long_) { |
| 154 |
flag_ = true; |
| 155 |
return true; |
| 156 |
} else { |
| 157 |
return false; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
private: |
| 162 |
bool &flag_; |
| 163 |
}; |
| 164 |
|
| 165 |
typedef std::vector<option*> opts; |
| 166 |
|
| 167 |
int argc_; |
| 168 |
char const * const * argv_; |
| 169 |
std::string prog_; |
| 170 |
opts opts_; |
| 171 |
bool help_; |
| 172 |
}; |
| 173 |
} |
| 174 |
|
| 175 |
#endif // OPTIONS_HPP |