From 34c1a3bbfb094ac1e794a70d8e7d9e5d9b03900c Mon Sep 17 00:00:00 2001 From: Pavel Belikov Date: Mon, 6 Nov 2017 13:58:51 +0300 Subject: add COMMAND to program line --- args.hxx | 46 ++++++++++++++++++++++++++++++++-------------- test.cxx | 9 ++++----- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/args.hxx b/args.hxx index 1e4b1ab..afefafe 100644 --- a/args.hxx +++ b/args.hxx @@ -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 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::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 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(); diff --git a/test.cxx b/test.cxx index 597eaae..0726619 100644 --- a/test.cxx +++ b/test.cxx @@ -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{}); 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{}); 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{}); s.str(""); s << p; - REQUIRE(s.str() == R"( git {OPTIONS} + REQUIRE(s.str() == R"( git [COMMAND] {OPTIONS} git-like parser -- cgit v1.2.1