Skip to content

Commit

Permalink
Merge branch 'fix_error_reporting'
Browse files Browse the repository at this point in the history
  • Loading branch information
mmahnic committed Dec 16, 2019
2 parents 1d8979d + ff901b0 commit 440bc29
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/argparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class argument_parser
friend class Parser;

private:
bool mTopLevel = true;
ParserDefinition mParserDef;
std::set<std::string> mHelpOptionNames;
std::vector<std::shared_ptr<Options>> mTargets;
Expand Down Expand Up @@ -148,6 +149,7 @@ class argument_parser
std::vector<ArgumentHelpResult> describe_arguments() const;

private:
static argument_parser createSubParser();
void resetOptionValues();
void assignDefaultValues();
void verifyDefinedOptions();
Expand Down
9 changes: 8 additions & 1 deletion src/argparser_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

namespace argparse {

CPPARGPARSE_INLINE argument_parser argument_parser::createSubParser()
{
auto parser = argument_parser{};
parser.mTopLevel = false;
return parser;
}

CPPARGPARSE_INLINE ParserConfig& argument_parser::config()
{
return mParserDef.mConfig;
Expand Down Expand Up @@ -186,7 +193,7 @@ CPPARGPARSE_INLINE ParseResult argument_parser::parse_args( ArgumentStream& args
assignDefaultValues();
validateParsedOptions( result );

if ( result.hasArgumentProblems() ) {
if ( mTopLevel && result.hasArgumentProblems() ) {
result.signalErrorsShown();
auto res = std::move( result.getResult() );
describe_errors( res );
Expand Down
2 changes: 1 addition & 1 deletion src/parser_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ CPPARGPARSE_INLINE void Parser::setValue( Option& option, std::string_view value
CPPARGPARSE_INLINE void Parser::parseCommandArguments(
Command& command, ArgumentStream& argStream, ParseResultBuilder& result )
{
auto parser = argument_parser{};
auto parser = argument_parser::createSubParser();
auto commandpath = mParserDef.getConfig().program + " " + command.getName();
parser.config().program( commandpath ).description( command.getHelp() );

Expand Down
23 changes: 23 additions & 0 deletions test/command_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,26 @@ TEST( ArgumentParserCommandTest, shouldStoreInstantiatedCommandsInParseResults )
EXPECT_EQ( "works", pCmdTwo->str.value_or( "" ) );
EXPECT_FALSE( pCmdTwo->count.has_value() );
}

TEST( ArgumentParserCommandTest, shouldReportErrorsOnlyInTopLevelParser )
{
std::stringstream strout;
auto parser = argument_parser{};
parser.config().cout( strout );

parser.add_command<CmdOneOptions>( "one" );

// -- WHEN
auto res = parser.parse_args( { "one", "--bad-option" } );
EXPECT_FALSE( static_cast<bool>( res ) );

// -- THEN
auto help = strout.str();
auto lines = splitLines( help );
int count = 0;
for ( auto line: lines ) {
if ( strHasText( line, "--bad-option" ) )
++count;
}
EXPECT_EQ( 1, count );
}

0 comments on commit 440bc29

Please sign in to comment.