Skip to content

CLI::Option::envname() should trigger validators only if corresponding subcommand is called #903

Closed

Description

@w3lld0ne

Basic example:

auto CliApp = new CLI::App("myapp");
auto sub1 = CliApp->add_subcommand("sub1");
std::filesystem::path someFile = "";
sub1->add_option("-f,--file", someFile)->envname("SOME_FILE")->required()->expected(1)->check(CLI::ExistingFile);
auto sub2 = CliApp->add_subcommand("sub2");
int completelyUnrelatedToSub1 = 0;
sub2->add_option("-v,--value", completelyUnrelatedToSub1)->required()->expected(1);

try
{
	CliApp->parse(argsCount, args);
}
catch (const CLI::ParseError& e)
{
	return CliApp->exit(e);
}

Here if I call myapp.exe sub2 -v 111 and I have %SOME_FILE% env var defined as invalid/non-existing file path (it's not happening if there is no such env var defined), CLI::ExistingFile validator will abort the execution, even though I haven't even called the subcommand it's defined for. If I'd want this check to happen in sub2, I'd simply add the same option to it as well.

Even better: try to call myapp.exe (no arguments) or myapp.exe -h and we'll be greeted with the same validation error. It's funny because the error states Run with --help for more information. and that's what we do :) The only thing that works fine is myapp.exe -v.

If I remove ->envname("SOME_FILE") part from --file option registration (or delete %SOME_FILE% var from environment), everything works as it should: it's still marked as ->required(), but this is checked only for execution of sub1 subcommand.

P.S. I'm on Windows 11 22H2, compiling release v2.3.2 in VS2022 17.6.4 in C++20 mode. Will try the main branch soon.

Activity

  1. phlptp commented on Jul 9, 2023

    @phlptp
    Collaborator

    I will check on this, thanks.

  2. added a commit that references this issue on Jul 9, 2023
    ff648c8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions