Skip to main content

Module cli

Module cli 

Source
Expand description

The command line as a layer.

The highest layer there is, and the one the fleet gets least right. hk declares eighteen sources.cli bindings and reads five — the flags live in a second, hand-maintained struct and are consumed ad hoc. mise hand-copies thirteen flags into its settings in a forty-nine-line function, and --jobs bypasses even that by going through the environment. pitchfork’s --help documents a CLI layer it does not have.

What they are all writing is this: for each setting a flag was given for, one entry at the top of the merge. The part that is easy to get wrong is given, which is why this layer takes values rather than a struct — a bool field is false whether the flag was absent or explicitly negated, and a layer that cannot tell those apart makes --no-colour indistinguishable from saying nothing, which silently outranks every file on the machine.

use usage_config::{resolve, CliLayer, Layers, PropMeta, Registry, Ty, Value};

static PROPS: &[PropMeta] = &[PropMeta {
    cli: &["--jobs", "-j"],
    ..PropMeta::new("jobs", Ty::Uint)
}];
const REGISTRY: Registry = Registry::new(PROPS);

// What a parser produces: the settings a flag was actually given for.
let cli = CliLayer::new([("jobs", "8")]);
let resolved = resolve(REGISTRY, Layers::new().then(&cli))?;

assert_eq!(resolved.get_key("jobs"), Some(&Value::Int(8)));
// Named as the flag rather than as "the command line", so an explanation is actionable.
assert_eq!(
    resolved.origin_key("jobs").unwrap().describe(),
    "--jobs",
);

Structs§

CliLayer
Settings given on the command line.