ctl-core 0.6.21

Shared clap chassis for the *ctl CLIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Bind every long option to an environment variable.

use std::ffi::OsString;

use clap::error::ErrorKind;
use clap::parser::ValueSource;
use clap::{Arg, ArgAction, ArgMatches, Command, Id};

/// Binds each long option of a [`Command`] and of every subcommand to
/// `<PREFIX>_<LONG>`, so the environment can set any flag a CLI declares.
///
/// Put it on the root parser as a derive attribute, with the trait in scope
/// (the prelude exports it): `#[command(env_prefix = "VERCTL")]`. Clap's
/// derive applies a root attribute after it has added every argument and
/// subcommand, so `C::command()` returns the bound graph to help, the Usage
/// spec, the pre-parse view, and the parse alike.
///
/// A command-line flag wins over its variable, which wins over the default,
/// once [`env_under_flags`] has run before the parse. A CLI that also reads a
/// config file applies a file value only where neither was given.
///
/// An option is left unbound when it already declares its own `env` (the
/// override), has no long name, is hidden, is the `--no-*` side of a pair,
/// or is help or version. Not calling `env_prefix` disables the binding.
pub trait EnvPrefix {
    /// Bind every eligible long option under `prefix`.
    #[must_use]
    fn env_prefix(self, prefix: &str) -> Self;
}

impl EnvPrefix for Command {
    fn env_prefix(self, prefix: &str) -> Self {
        self.mut_args(|arg| bind(arg, prefix))
            .mut_subcommands(|subcommand| subcommand.env_prefix(prefix))
    }
}

/// The variable a long option reads: `dry-run` under `VERCTL` is
/// `VERCTL_DRY_RUN`.
#[must_use]
fn env_name(prefix: &str, long: &str) -> String {
    format!("{prefix}_{}", long.to_ascii_uppercase().replace('-', "_"))
}

fn bind(arg: Arg, prefix: &str) -> Arg {
    let name = match arg.get_long() {
        Some(long)
            if arg.get_env().is_none()
                && !arg.is_hide_set()
                && !long.starts_with("no-")
                && !matches!(
                    arg.get_action(),
                    ArgAction::Help
                        | ArgAction::HelpShort
                        | ArgAction::HelpLong
                        | ArgAction::Version
                ) =>
        {
            env_name(prefix, long)
        }
        _ => return arg,
    };
    arg.env(name)
}

/// 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](https://github.com/clap-rs/clap/issues/5591)). This
/// parses `raw` once leniently to learn which options the command line set
/// at each subcommand level, 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, either way, with one the command
///    line set: a `conflicts_with` on either side, a single-choice group they
///    share, or either side being `exclusive`;
/// 4. is empty, as a composite action sets 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 sees overrides only between `--x`/`--no-x` pairs, and no conflict
/// that a group declares. Call this on the command just before the parse,
/// and parse the command it returns; `App` does.
///
/// # 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.
pub fn env_under_flags(command: Command, raw: &[OsString]) -> Result<Command, clap::Error> {
    let mut probe = command.clone().ignore_errors(true);
    let Ok(matches) = probe.try_get_matches_from_mut(raw) else {
        return Ok(command);
    };
    let levels = levels(&probe, &matches, &command);
    let placed = placed(&levels);
    let mut unset = Vec::new();
    let mut kept = Vec::<&Placed<'_>>::new();
    for option in placed
        .iter()
        .filter(|option| option.arg.get_env().is_some())
    {
        let overruled = !option.is_set()
            || option.given
            || placed
                .iter()
                .any(|given| given.given && (option.negates(given) || option.conflicts(given)));
        if overruled {
            unset.push((option.depth, option.arg.get_id().clone()));
        } else if let Some(other) = kept.iter().find(|other| option.conflicts(other)) {
            let mut command = levels
                .last()
                .map_or_else(|| probe.clone(), |(level, _)| (*level).clone());
            return Err(clap::Error::raw(
                ErrorKind::ArgumentConflict,
                format!(
                    "the variables `{}` and `{}` cannot both be set\n",
                    other.env_name(),
                    option.env_name()
                ),
            )
            .format(&mut command));
        } else {
            kept.push(option);
        }
    }
    let path = levels
        .iter()
        .skip(1)
        .map(|(level, _)| level.get_name())
        .collect::<Vec<_>>();
    Ok(unbind(command, &path, 0, &unset))
}

/// The probe's command and matches at each level of the subcommand path,
/// as far as the declared command has that subcommand.
fn levels<'a>(
    mut probe: &'a Command,
    mut matches: &'a ArgMatches,
    mut declared: &Command,
) -> Vec<(&'a Command, &'a ArgMatches)> {
    let mut levels = vec![(probe, matches)];
    while let Some((name, sub)) = matches.subcommand() {
        let (Some(next), Some(next_declared)) =
            (probe.find_subcommand(name), declared.find_subcommand(name))
        else {
            break;
        };
        probe = next;
        matches = sub;
        declared = next_declared;
        levels.push((probe, matches));
    }
    levels
}

/// One option on the subcommand path, at the level that declares it.
struct Placed<'a> {
    depth: usize,
    arg: &'a Arg,
    conflicts: Vec<&'a Id>,
    given: bool,
}

/// Every option on the path once. A global option appears at the level that
/// declares it, not at the levels clap copies it into.
///
/// Conflicts are read through a non-global copy of each option: for a global,
/// clap's lookup resolves only argument ids and panics on a group id, while
/// the declaring level holds every id its conflicts may name.
fn placed<'a>(levels: &[(&'a Command, &'a ArgMatches)]) -> Vec<Placed<'a>> {
    let mut options = Vec::new();
    for (depth, (level, matches)) in levels.iter().enumerate() {
        let parent = depth.checked_sub(1).and_then(|up| levels.get(up));
        for arg in level.get_arguments() {
            let copied = arg.is_global_set()
                && parent.is_some_and(|(up, _)| {
                    up.get_arguments()
                        .any(|other| other.get_id() == arg.get_id())
                });
            if copied {
                continue;
            }
            let mut conflicts = level
                .get_arg_conflicts_with(&arg.clone().global(false))
                .into_iter()
                .map(Arg::get_id)
                .collect::<Vec<_>>();
            for group in level.get_groups() {
                if group.get_args().any(|id| id == arg.get_id()) && !group.clone().is_multiple() {
                    conflicts.extend(group.get_args().filter(|id| *id != arg.get_id()));
                }
            }
            options.push(Placed {
                depth,
                arg,
                conflicts,
                given: matches.value_source(arg.get_id().as_str())
                    == Some(ValueSource::CommandLine),
            });
        }
    }
    options
}

impl Placed<'_> {
    fn env_name(&self) -> String {
        self.arg
            .get_env()
            .map(|name| name.to_string_lossy().into_owned())
            .unwrap_or_default()
    }

    /// Whether the variable holds a value that changes the parse.
    fn is_set(&self) -> bool {
        let Some(value) = self.arg.get_env().and_then(std::env::var_os) else {
            return false;
        };
        let action = self.arg.get_action();
        let default = if matches!(action, ArgAction::SetTrue) {
            Some("false")
        } else if matches!(action, ArgAction::SetFalse) {
            Some("true")
        } else {
            None
        };
        !value.is_empty() && default.is_none_or(|default| value != default)
    }

    /// Whether the two options can meet: at one level, or one is a global
    /// declared above the other.
    fn meets(&self, other: &Self) -> bool {
        let upper = if self.depth <= other.depth {
            self
        } else {
            other
        };
        self.depth == other.depth || upper.arg.is_global_set()
    }

    /// Whether clap would refuse the two options together.
    fn conflicts(&self, other: &Self) -> bool {
        let exclusive = self.arg.is_exclusive_set() || other.arg.is_exclusive_set();
        self.meets(other)
            && self.arg.get_id() != other.arg.get_id()
            && (exclusive
                || self.conflicts.contains(&other.arg.get_id())
                || other.conflicts.contains(&self.arg.get_id()))
    }

    /// Whether the two options are the two sides of a `--x`/`--no-x` pair.
    fn negates(&self, other: &Self) -> bool {
        let (Some(long), Some(other_long)) = (self.arg.get_long(), other.arg.get_long()) else {
            return false;
        };
        self.meets(other)
            && (long.strip_prefix("no-") == Some(other_long)
                || other_long.strip_prefix("no-") == Some(long))
    }
}

/// Unbind each listed option at its depth along the subcommand path.
fn unbind(command: Command, path: &[&str], depth: usize, unset: &[(usize, Id)]) -> Command {
    let command = command.mut_args(|arg| {
        if unset
            .iter()
            .any(|(at, id)| *at == depth && id == arg.get_id())
        {
            arg.env(None::<&'static str>)
        } else {
            arg
        }
    });
    match path.split_first() {
        Some((name, rest)) => command.mut_subcommands(|sub| {
            if sub.get_name() == *name {
                unbind(sub, rest, depth + 1, unset)
            } else {
                sub
            }
        }),
        None => command,
    }
}

#[cfg(test)]
mod tests {
    use clap::{Args, CommandFactory, Parser, Subcommand};

    use super::{EnvPrefix, env_name, env_under_flags};

    #[derive(Parser)]
    #[command(env_prefix = "TOYCTL_UNIT")]
    struct Toy {
        #[arg(long, global = true)]
        format: Option<String>,
        #[arg(long)]
        no_color: bool,
        #[arg(long, hide = true)]
        secret: bool,
        #[arg(long, env = "OWN_NAME")]
        token: Option<String>,
        #[command(subcommand)]
        command: Cmd,
    }

    #[derive(Subcommand)]
    enum Cmd {
        Prepare(PrepareArgs),
        Group {
            #[command(subcommand)]
            command: Nested,
        },
    }

    #[derive(Args)]
    struct PrepareArgs {
        #[arg(short = 'c', long)]
        config: Option<String>,
        path: Option<String>,
    }

    #[derive(Subcommand)]
    enum Nested {
        Show {
            #[arg(long)]
            dry_run: bool,
        },
    }

    fn env_of(command: &clap::Command, id: &str) -> Option<String> {
        command
            .get_arguments()
            .find(|arg| arg.get_id() == id)
            .and_then(|arg| arg.get_env())
            .map(|name| name.to_string_lossy().into_owned())
    }

    #[test]
    fn a_long_name_becomes_the_prefixed_variable() {
        assert_eq!(env_name("VERCTL", "dry-run"), "VERCTL_DRY_RUN");
        assert_eq!(env_name("QCTL", "config"), "QCTL_CONFIG");
    }

    #[test]
    fn every_eligible_long_option_is_bound_at_every_depth() {
        let command = Toy::command();
        assert_eq!(
            env_of(&command, "format").as_deref(),
            Some("TOYCTL_UNIT_FORMAT")
        );
        let prepare = command.find_subcommand("prepare").unwrap();
        assert_eq!(
            env_of(prepare, "config").as_deref(),
            Some("TOYCTL_UNIT_CONFIG")
        );
        let show = command
            .find_subcommand("group")
            .and_then(|group| group.find_subcommand("show"))
            .unwrap();
        assert_eq!(
            env_of(show, "dry_run").as_deref(),
            Some("TOYCTL_UNIT_DRY_RUN")
        );
    }

    #[test]
    fn negations_hidden_positional_and_declared_options_keep_their_shape() {
        let command = Toy::command();
        assert_eq!(env_of(&command, "no_color"), None);
        assert_eq!(env_of(&command, "secret"), None);
        assert_eq!(env_of(&command, "token").as_deref(), Some("OWN_NAME"));
        let prepare = command.find_subcommand("prepare").unwrap();
        assert_eq!(env_of(prepare, "path"), None);
    }

    #[test]
    fn a_global_option_that_conflicts_with_a_group_parses() {
        let command = clap::Command::new("toy")
            .arg(
                clap::Arg::new("quiet")
                    .long("quiet")
                    .global(true)
                    .action(clap::ArgAction::SetTrue)
                    .conflicts_with("loud"),
            )
            .arg(
                clap::Arg::new("verbose")
                    .long("verbose")
                    .action(clap::ArgAction::SetTrue),
            )
            .group(clap::ArgGroup::new("loud").arg("verbose"))
            .env_prefix("TOYCTL_GROUP_UNIT");
        let raw = ["toy", "--quiet"].map(std::ffi::OsString::from);
        let matches = env_under_flags(command, &raw)
            .unwrap()
            .try_get_matches_from(&raw)
            .unwrap();
        assert!(matches.get_flag("quiet"));
    }

    #[test]
    fn a_command_without_the_attribute_binds_nothing() {
        let command = clap::Command::new("bare").arg(clap::Arg::new("format").long("format"));
        assert_eq!(env_of(&command, "format"), None);
        let bound = command.env_prefix("BARE");
        assert_eq!(env_of(&bound, "format").as_deref(), Some("BARE_FORMAT"));
    }
}