diff options
author | Pavel Belikov <pavel.fuchs.belikov@gmail.com> | 2017-11-06 13:58:51 +0300 |
---|---|---|
committer | Pavel Belikov <pavel.fuchs.belikov@gmail.com> | 2017-11-06 13:58:51 +0300 |
commit | 34c1a3bbfb094ac1e794a70d8e7d9e5d9b03900c (patch) | |
tree | 6653b3bc131c48825b1e4582b16ed04ebf6cd3d8 | |
parent | add HelpParams::showCommandChildren and HelpParams::showCommandFullHelp (diff) | |
download | args.hxx-34c1a3bbfb094ac1e794a70d8e7d9e5d9b03900c.tar.xz |
add COMMAND to program line
-rw-r--r-- | args.hxx | 46 | ||||
-rw-r--r-- | test.cxx | 9 |
2 files changed, 36 insertions, 19 deletions
@@ -504,6 +504,10 @@ namespace args /** The postfix for progline when showProglineOptions is true and command has any flags */ std::string proglineOptions = "{OPTIONS}"; + + /** The prefix for progline when command has any subcommands + */ + std::string proglineCommand = "COMMAND"; }; class FlagBase; @@ -587,6 +591,11 @@ namespace args return false; } + virtual bool HasCommand() const + { + return false; + } + virtual std::vector<std::string> GetProgramLine(const HelpParams &) const { return {}; @@ -993,6 +1002,15 @@ namespace args return std::any_of(Children().begin(), Children().end(), [](Base *child) { return child->HasPositional(); }); } + /** Get whether this has any Command children + * + * \return Whether or not there are any Command children + */ + virtual bool HasCommand() const override + { + return std::any_of(Children().begin(), Children().end(), [](Base *child) { return child->HasCommand(); }); + } + /** Count the number of matched children this group has */ std::vector<Base *>::size_type MatchedChildren() const @@ -1393,22 +1411,17 @@ namespace args virtual bool HasFlag() const { - if (selectedCommand != nullptr) - { - return selectedCommand->HasFlag(); - } - - return Matched() ? subparserHasFlag || Group::HasFlag() : false; + return subparserHasFlag || Group::HasFlag(); } virtual bool HasPositional() const { - if (selectedCommand != nullptr) - { - return selectedCommand->HasPositional(); - } + return subparserHasPositional || Group::HasPositional(); + } - return Matched() ? subparserHasPositional || Group::HasPositional() : false; + virtual bool HasCommand() const + { + return true; } std::vector<std::string> GetCommandProgramLine(const HelpParams ¶ms) const @@ -1416,6 +1429,11 @@ namespace args auto res = Group::GetProgramLine(params); res.insert(res.end(), subparserProgramLine.begin(), subparserProgramLine.end()); + if (!params.proglineCommand.empty() && Group::HasCommand()) + { + res.insert(res.begin(), commandIsRequired ? params.proglineCommand : "[" + params.proglineCommand + "]"); + } + if (!Name().empty()) { res.insert(res.begin(), Name()); @@ -1995,14 +2013,14 @@ namespace args */ void Help(std::ostream &help_) const { - const bool hasoptions = HasFlag(); - const bool hasarguments = HasPositional(); - auto &command = SelectedCommand(); const auto &description = command.Description().empty() ? command.Help() : command.Description(); const auto description_text = Wrap(description, helpParams.width - helpParams.descriptionindent); const auto epilog_text = Wrap(command.Epilog(), helpParams.width - helpParams.descriptionindent); + const bool hasoptions = command.HasFlag(); + const bool hasarguments = command.HasPositional(); + std::ostringstream prognameline; prognameline << Prog(); @@ -769,7 +769,7 @@ TEST_CASE("Subparser help works as expected", "[args]") auto d = p.GetDescription(p.helpParams, 0); s << p; - REQUIRE(s.str() == R"( git {OPTIONS} + REQUIRE(s.str() == R"( git [COMMAND] {OPTIONS} git-like parser @@ -797,7 +797,7 @@ TEST_CASE("Subparser help works as expected", "[args]") p.ParseArgs(std::vector<std::string>{}); s.str(""); s << p; - REQUIRE(s.str() == R"( git {OPTIONS} + REQUIRE(s.str() == R"( git [COMMAND] {OPTIONS} git-like parser @@ -809,12 +809,11 @@ TEST_CASE("Subparser help works as expected", "[args]") )"); - p.helpParams.showCommandChildren = true; p.ParseArgs(std::vector<std::string>{}); s.str(""); s << p; - REQUIRE(s.str() == R"( git {OPTIONS} + REQUIRE(s.str() == R"( git [COMMAND] {OPTIONS} git-like parser @@ -833,7 +832,7 @@ TEST_CASE("Subparser help works as expected", "[args]") p.ParseArgs(std::vector<std::string>{}); s.str(""); s << p; - REQUIRE(s.str() == R"( git {OPTIONS} + REQUIRE(s.str() == R"( git [COMMAND] {OPTIONS} git-like parser |