Skip to content

Commit

Permalink
fixed output of help text for program options
Browse files Browse the repository at this point in the history
  • Loading branch information
Neverlord committed Nov 5, 2012
1 parent 5548468 commit 3c15db6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
7 changes: 6 additions & 1 deletion cppa/opt.hpp
Expand Up @@ -59,7 +59,12 @@ detail::add_arg_functor<T> add_arg(std::vector<T>& storage) {
return {storage};
}

typedef std::map<std::string, std::map<std::pair<char, std::string>, std::string> >
struct option_info {
std::string help_text;
size_t num_args;
};

typedef std::map<std::string,std::map<std::pair<char,std::string>,option_info> >
options_description;

detail::opt_rvalue_builder<true> on_opt(char short_opt,
Expand Down
26 changes: 21 additions & 5 deletions src/opt.cpp
Expand Up @@ -43,7 +43,8 @@ detail::opt_rvalue_builder<true> on_opt(char short_opt,
string help_text,
string help_group) {
if (desc) {
(*desc)[help_group].insert(make_pair(make_pair(short_opt, long_opt), help_text));
option_info oinf{help_text, 1};
(*desc)[help_group].insert(make_pair(make_pair(short_opt, long_opt), move(oinf)));
}
const char short_flag_arr[] = {'-', short_opt, '\0' };
const char* lhs_str = short_flag_arr;
Expand Down Expand Up @@ -73,7 +74,8 @@ on_vopt(char short_opt,
string help_text,
string help_group) {
if (desc) {
(*desc)[help_group].insert(make_pair(make_pair(short_opt, long_opt), help_text));
option_info oinf{help_text, 0};
(*desc)[help_group].insert(make_pair(make_pair(short_opt, long_opt), move(oinf)));
}
const char short_flag_arr[] = {'-', short_opt, '\0' };
vector<string> opt_strs = { short_flag_arr };
Expand All @@ -85,6 +87,10 @@ on_vopt(char short_opt,
function<void()> print_desc(options_description* desc, ostream& out) {
return [&out, desc] {
if (!desc) return;
if (desc->empty()) {
out << "please use '-h' or '--help' for a list "
"of available program options\n";
}
for (auto& opt_group : *desc) {
out << opt_group.first << ":\n";
for (auto& opt : opt_group.second) {
Expand All @@ -93,10 +99,20 @@ function<void()> print_desc(options_description* desc, ostream& out) {
ostringstream tmp;
auto& names = opt.first;
if (names.first != '\0') {
tmp << "-" << names.first << " <arg> | ";
tmp << "-" << names.first;
for (size_t num = 1; num <= opt.second.num_args; ++num) {
tmp << " <arg" << num << ">";
}
tmp << " | ";
}
tmp << "--" << names.second;
if (opt.second.num_args > 0) {
tmp << "=<arg1>";
}
for (size_t num = 2; num <= opt.second.num_args; ++num) {
tmp << ",<arg" << num << ">";
}
tmp << "--" << names.second << " <arg>";
out << tmp.str() << opt.second << "\n";
out << tmp.str() << opt.second.help_text << "\n";
}
out << "\n";
}
Expand Down

0 comments on commit 3c15db6

Please sign in to comment.