Commit 1a558d76bd97c26d2a454fcd3edb885034596728
1 parent
8d7c4ea4
custom argument help
Showing
2 changed files
with
27 additions
and
11 deletions
src/cxxopts.hpp
| ... | ... | @@ -515,6 +515,7 @@ namespace cxxopts |
| 515 | 515 | std::string l; |
| 516 | 516 | String desc; |
| 517 | 517 | bool has_arg; |
| 518 | + std::string arg_help; | |
| 518 | 519 | }; |
| 519 | 520 | |
| 520 | 521 | struct HelpGroupDetails |
| ... | ... | @@ -550,7 +551,8 @@ namespace cxxopts |
| 550 | 551 | const std::string& s, |
| 551 | 552 | const std::string& l, |
| 552 | 553 | std::string desc, |
| 553 | - std::shared_ptr<const Value> value | |
| 554 | + std::shared_ptr<const Value> value, | |
| 555 | + std::string arg_help | |
| 554 | 556 | ); |
| 555 | 557 | |
| 556 | 558 | int |
| ... | ... | @@ -655,7 +657,8 @@ namespace cxxopts |
| 655 | 657 | const std::string& opts, |
| 656 | 658 | const std::string& desc, |
| 657 | 659 | std::shared_ptr<const Value> value |
| 658 | - = ::cxxopts::value<bool>() | |
| 660 | + = ::cxxopts::value<bool>(), | |
| 661 | + std::string arg_help = "" | |
| 659 | 662 | ); |
| 660 | 663 | |
| 661 | 664 | private: |
| ... | ... | @@ -685,7 +688,8 @@ namespace cxxopts |
| 685 | 688 | ( |
| 686 | 689 | const std::string& s, |
| 687 | 690 | const std::string& l, |
| 688 | - bool has_arg | |
| 691 | + bool has_arg, | |
| 692 | + const std::string& arg_help | |
| 689 | 693 | ) |
| 690 | 694 | { |
| 691 | 695 | String result = " "; |
| ... | ... | @@ -706,7 +710,14 @@ namespace cxxopts |
| 706 | 710 | |
| 707 | 711 | if (has_arg) |
| 708 | 712 | { |
| 709 | - result += " arg"; | |
| 713 | + if (arg_help.size() != 0) | |
| 714 | + { | |
| 715 | + result += " " + toLocalString(arg_help); | |
| 716 | + } | |
| 717 | + else | |
| 718 | + { | |
| 719 | + result += " arg"; | |
| 720 | + } | |
| 710 | 721 | } |
| 711 | 722 | |
| 712 | 723 | return result; |
| ... | ... | @@ -780,7 +791,8 @@ OptionAdder::operator() |
| 780 | 791 | ( |
| 781 | 792 | const std::string& opts, |
| 782 | 793 | const std::string& desc, |
| 783 | - std::shared_ptr<const Value> value | |
| 794 | + std::shared_ptr<const Value> value, | |
| 795 | + std::string arg_help | |
| 784 | 796 | ) |
| 785 | 797 | { |
| 786 | 798 | std::match_results<const char*> result; |
| ... | ... | @@ -794,7 +806,8 @@ OptionAdder::operator() |
| 794 | 806 | const auto& s = result[2]; |
| 795 | 807 | const auto& l = result[3]; |
| 796 | 808 | |
| 797 | - m_options.add_option(m_group, s.str(), l.str(), desc, value); | |
| 809 | + m_options.add_option(m_group, s.str(), l.str(), desc, value, | |
| 810 | + std::move(arg_help)); | |
| 798 | 811 | |
| 799 | 812 | return *this; |
| 800 | 813 | } |
| ... | ... | @@ -987,7 +1000,8 @@ Options::add_option |
| 987 | 1000 | const std::string& s, |
| 988 | 1001 | const std::string& l, |
| 989 | 1002 | std::string desc, |
| 990 | - std::shared_ptr<const Value> value | |
| 1003 | + std::shared_ptr<const Value> value, | |
| 1004 | + std::string arg_help | |
| 991 | 1005 | ) |
| 992 | 1006 | { |
| 993 | 1007 | auto stringDesc = toLocalString(std::move(desc)); |
| ... | ... | @@ -1006,7 +1020,9 @@ Options::add_option |
| 1006 | 1020 | //add the help details |
| 1007 | 1021 | auto& options = m_help[group]; |
| 1008 | 1022 | options.options. |
| 1009 | - emplace_back(HelpOptionDetails{s, l, stringDesc, value->has_arg()}); | |
| 1023 | + emplace_back(HelpOptionDetails{s, l, stringDesc, value->has_arg(), | |
| 1024 | + std::move(arg_help)} | |
| 1025 | + ); | |
| 1010 | 1026 | } |
| 1011 | 1027 | |
| 1012 | 1028 | void |
| ... | ... | @@ -1048,7 +1064,7 @@ Options::help_one_group(const std::string& g) const |
| 1048 | 1064 | |
| 1049 | 1065 | for (const auto& o : group->second.options) |
| 1050 | 1066 | { |
| 1051 | - auto s = format_option(o.s, o.l, o.has_arg); | |
| 1067 | + auto s = format_option(o.s, o.l, o.has_arg, o.arg_help); | |
| 1052 | 1068 | longest = std::max(longest, stringLength(s)); |
| 1053 | 1069 | format.push_back(std::make_pair(s, String())); |
| 1054 | 1070 | } | ... | ... |
src/example.cpp
| ... | ... | @@ -37,14 +37,14 @@ int main(int argc, char* argv[]) |
| 37 | 37 | options.add_options() |
| 38 | 38 | ("a,apple", "an apple", cxxopts::value<bool>(apple)) |
| 39 | 39 | ("b,bob", "Bob") |
| 40 | - ("f,file", "File", cxxopts::value<std::vector<std::string>>()) | |
| 40 | + ("f,file", "File", cxxopts::value<std::vector<std::string>>(), "FILE") | |
| 41 | 41 | ("positional", |
| 42 | 42 | "Positional arguments: these are the arguments that are entered " |
| 43 | 43 | "without an option", cxxopts::value<std::string>()) |
| 44 | 44 | ("long-description", |
| 45 | 45 | "thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace") |
| 46 | 46 | ("help", "Print help") |
| 47 | - ("int", "An integer", cxxopts::value<int>()) | |
| 47 | + ("int", "An integer", cxxopts::value<int>(), "N") | |
| 48 | 48 | ("option_that_is_too_long_for_the_help", "A very long option") |
| 49 | 49 | #ifdef CXXOPTS_USE_UNICODE |
| 50 | 50 | ("unicode", u8"A help option with non-ascii: à. Here the size of the" | ... | ... |