Skip to main content

env_under_flags

Function env_under_flags 

Source
pub fn env_under_flags(
    command: Command,
    raw: &[OsString],
) -> Result<Command, Error>
Available on crate feature cli only.
Expand description

Removes each variable that the command line overrules, so the command line wins, and refuses two variables that conflict.

Clap treats a variable as given by the user. A variable that conflicts with a flag on the command line, or with its --no-* side, fails the parse (clap#5591). This function parses raw once leniently to learn which options the command line set at each subcommand level. It then unbinds each variable that:

  1. belongs to an option the command line set;
  2. belongs to the other side of a --x/--no-x pair the command line set;
  3. belongs to an option that conflicts with one the command line set. The conflict may be a conflicts_with on either side, a single-choice group they share, or an exclusive option on either side;
  4. is empty. A wrapper that maps every setting to a variable may set one the caller left out. A GitHub Actions composite action does this with an input it was not given;
  5. reads as a boolean flag’s default, false for a flag that sets true, so it changes nothing and cannot conflict.

Clap keeps overrides_with and a group’s own conflicts_with private. So this function sees overrides only between --x/--no-x pairs, and no conflict that a group declares. Call it on the command just before the parse, and parse the command it returns. App does both.

§Errors

An ArgumentConflict error that names both variables when two remaining variables belong to conflicting options. Clap’s own error would name two flags the user never typed.