32 typedef std::string String;
33 typedef std::vector<String> Strings;
42 Value(String
const& value, String
const& description) {
46 String
usage()
const {
48 " ", value_.c_str(), description_.c_str());
50 String value()
const {
53 bool operator==(Value
const& b)
const {
54 return (value_ == b.value_);
57 typedef std::vector<Value> Values;
62 Values values_accept_;
63 String value_default_;
72 Option(String
const& description) {
75 description_ = description;
78 Option(String
const& description,
80 String
const& default_value,
84 description_ = description;
86 value_default_ = default_value;
89 Strings
const& values()
const {
92 String value(
size_t index)
const {
93 if (!has_value_)
return "";
94 if (!has_setup_ || index >= values_.size())
return value_default_;
95 return values_[index];
97 ssize_t valueAdd(String
const& value) {
102 if (values_accept_.size() > 0 &&
103 std::find(values_accept_.begin(), values_accept_.end(),
104 Value(value,
"")) == values_accept_.end())
106 values_.push_back(value);
108 return values_.size() - 1;
110 bool valueAcceptAdd(String
const& value, String
const& description) {
111 if (!has_value_)
return false;
112 if (std::find(values_accept_.begin(), values_accept_.end(),
113 Value(value,
"")) == values_accept_.end()){
114 values_accept_.push_back(Value(value, description));
118 bool valueAcceptChk(String
const& value){
119 if (!has_value_)
return false;
120 if (values_accept_.size() == 0)
return true;
121 return (std::find(values_accept_.begin(), values_accept_.end(),
122 Value(value,
"")) != values_accept_.end());
124 bool hasSetup()
const{
return has_setup_; }
125 bool hasValue()
const{
return has_value_; }
126 bool chkSetup()
const{
return !(must_setup_ && !has_setup_); }
128 String
usage(
unsigned char opt,
bool detail)
const {
131 if (has_value_) ret += value_type_;
132 if (!must_setup_) ret =
"[" + ret +
"]";
135 ret += value_type_ +
" ";
136 String default_string(
"");
137 if (value_default_ !=
"")
138 default_string =
"defalut='" + value_default_ +
"'";
139 String optional_string(
"");
141 optional_string =
"optional";
143 if (default_string.size() + optional_string.size() > 0) {
144 if (default_string.size() > 0 && optional_string.size() > 0) {
145 ret +=
"(" + optional_string +
", " + default_string +
")";
147 ret +=
"(" + optional_string + default_string +
")";
152 String accept_string;
153 for (
size_t i = 0; i < values_accept_.size(); i++) {
155 accept_string += (i + 1 < values_accept_.size()
157 accept_string +=
"'" + values_accept_[i].value() +
"'";
159 if (accept_string.size() == 0) accept_string =
"... (anything)";
164 accept_string) +
"\n";
165 for (
size_t i = 0; i < values_accept_.size(); i++) {
166 ret += values_accept_[i].usage();
173 typedef std::map<unsigned char, Option> Options;
174 typedef Options::const_iterator OptionsIterator;
177 Strings usage_begin_;
179 Strings proc_arguments_;
206 options_ = usage.options_;
207 usage_begin_ = usage.usage_begin_;
208 usage_end_ = usage.usage_end_;
209 proc_arguments_ = usage.proc_arguments_;
220 it =
usage.options_.begin(); it !=
usage.options_.end(); ++it) {
221 if (options_.find(it->first) != options_.end())
225 it =
usage.options_.begin(); it !=
usage.options_.end(); ++it) {
226 options_[it->first] = it->second;
228 for (
size_t i = 0; i <
usage.usage_begin_.size(); ++i)
229 usage_begin_.push_back(
usage.usage_begin_[i]);
230 for (
size_t i = 0; i <
usage.usage_end_.size(); ++i)
231 usage_end_.push_back(
usage.usage_end_[i]);
243 it = usage.options_.begin(); it != usage.options_.end(); ++it) {
244 if (options_.find(it->first) == options_.end())
continue;
245 for(
size_t i = 0, I = it->second.values().size(); i < I; i++){
246 options_[it->first].valueAdd(it->second.value(i));
260 if (options_.find(opt) != options_.end())
return false;
261 options_[opt] = Option(des);
276 String
const& val_type,
277 String
const& val_default,
279 if (options_.find(opt) != options_.end())
return false;
280 options_[opt] = Option(des, val_type, val_default, must);
295 if (options_.find(opt) == options_.end())
return false;
296 return options_[opt].valueAcceptAdd(val, des);
306 return (options_.find(opt) != options_.end() &&
307 options_.find(opt)->second.hasSetup());
317 if(options_.find(opt) == options_.end())
return 0;
318 return options_.find(opt)->second.values().size();
329 if (options_.find(opt) == options_.end()) {
332 return options_.find(opt)->second.value(index);
341 return proc_arguments_.size();
351 if (index >= proc_arguments_.size()) {
354 return proc_arguments_[index];
363 return proc_arguments_;
390 Usage::String out =
stringPrintf(
"USAGE\n %s", name_.c_str());
392 it = options_.begin(); it != options_.end(); ++it)
393 out +=
" " + it->second.usage(it->first,
false);
394 out +=
"\n\nDESCRIPTION\n";
395 for (
size_t i = 0; i < usage_begin_.size(); ++i) {
396 out +=
" " + usage_begin_[i] +
"\n\n";
399 it = options_.begin(); it != options_.end(); ++it) {
400 out += it->second.usage(it->first,
true);
402 for (
size_t i = 0; i < usage_end_.size(); ++i) {
403 out +=
" " + usage_end_[i] +
"\n\n";
420 String& err = (errmsg == NULL ? zzz : *errmsg);
421 for (it = options_.begin(); it != options_.end(); ++it) {
422 s += (char)(it->first);
423 if (it->second.hasValue()) s +=
":";
426 for (
int opt; (opt = getopt(argc, argv, s.c_str())) != -1; ) {
427 if (options_.find(opt) == options_.end()) {
428 if(options_.find(optopt) == options_.end()){
437 if (options_[opt].valueAdd(optarg == NULL ?
"" : optarg) < 0) {
438 err +=
stringPrintf(
"Option argument '%s' to '-%c' is not allowed\n"
444 for (it = options_.begin(); it != options_.end(); it++) {
445 if (it->second.chkSetup() ==
false) {
452 for (
int i = optind; i < argc; i++) {
453 proc_arguments_.push_back(String(argv[i]));
461 #endif // MEOW_USAGE_H__