CLI11 icon indicating copy to clipboard operation
CLI11 copied to clipboard

Rules for positional options

Open phlptp opened this issue 6 years ago • 5 comments

Positional arguments seem to be a topic of interest and figuring out how the matching should be done in relation to unlimited positional arguments and some other modifiers is a bit ad-hoc at the moment.

Positional rules A positional argument will match the first option not filled searched in order

  1. of Definition in the main app
  2. By order of definition in any option_groups which are themselves ordered by order of definition.
  3. positional arguments fall through to a parent if not otherwise matched.

if validate_positionals is active, then the additional consideration is that an argument must pass validation to match.

if positionals_at_end is active then positional options with the required flag will be filled with priority over non-required positionals options if insufficient arguments remain to fill any options beyond the required ones.

A question for anyone interested is are these rules sufficient or is more fine grained control necessary and if so what might that look like.

phlptp avatar Aug 19 '19 23:08 phlptp

based on some initial discussion in #306

phlptp avatar Aug 19 '19 23:08 phlptp

One specific issue is vector positionals are never filled unless they have a specific expected value set. So they consume all the positional arguments regardless of any other subsequent definitions.

phlptp avatar Aug 19 '19 23:08 phlptp

I think having the tutorial docs will help here, as we can map out what is currently done (and maybe inspire a plan for what should be done).

henryiii avatar Sep 06 '19 15:09 henryiii

I can't seem to understand how to make parsing fail when 0 positionals are given... Could you help?

Basic idea is: subcommand is required, positionals should be trailing (don't really like placing them at random places) and positionals have the same meaning for all subcommands. Sample call: app --param /some/file subcommand /path1 /path2

CLI::App app;
app.require_subcommand();
app.positionals_at_end();

std::string s;
app.add_option("--param", s, "")->required()->check(CLI::ExistingFile);

std::vector<std::string> filePaths;
// with required() parsing fails when positionals are given after subcommand
app.add_option("pos", filePaths, "Path(s) to file(s)")->expected(-1)->check(CLI::ExistingFile);

// adding positionals_at_end() has no effect
auto dumpCommand = app.add_subcommand("dump", "Dump mode")->fallthrough();

// subcommand params and other subcommands...

CLI11_PARSE(app, argc, argv);

kambala-decapitator avatar May 18 '21 13:05 kambala-decapitator