Available on crate feature
cli only.Expand description
The flags every CLI built on ctl-core shares, as Clap structs to flatten into a parser.
OutputArgsadds-f/--format,-c/--color,--no-color, and-q/--quiet.FormatArgs,FormatLong, andColorLongare the variants for a CLI that already owns-cor-f.DryRunArgsadds-n/--dry-run, with--previewas an alias.EnvPrefixbinds every long option to an environment variable.chassis_warningsandwarn_oppositesfind repeated or contradictory flags, whichemit_warningsprints.
use clap::Parser;
use ctl_core::color::ColorMode;
use ctl_core::flags::OutputArgs;
use ctl_core::format::OutputFormat;
#[derive(Parser)]
struct Cli {
#[command(flatten)]
output: OutputArgs,
}
let cli = Cli::parse_from(["toy", "--format", "json", "--no-color"]);
assert_eq!(cli.output.format, OutputFormat::Json);
assert_eq!(cli.output.color(), ColorMode::Never);Structs§
- Color
Long --colorand--no-colorwithout the-cshort flag, for CLIs that already use-cfor config.- DryRun
Args -n/--dry-run, with--previewas a visible alias.- Flag
Warning - One flag clash. Its
Displaytext is the sentence a warning line holds after the{bin}: warning:prefix. - Format
Args -f/--formatand-q/--quiet, with no colour flags.- Format
Long --formatand-q/--quietwithout the-fshort flag, for CLIs that already use-ffor a domain flag such as--file.- Output
Args - Output flags for the root parser: format, colour, and quiet. Flatten it with
#[command(flatten)].
Enums§
- Flag
Clash - The type of clash between flags that a
FlagWarningreports. The last value still wins in every case.
Traits§
- EnvPrefix
- Binds each long option of a
Commandand of every subcommand to<PREFIX>_<LONG>, so the environment can set any flag a CLI declares.
Functions§
- chassis_
warnings - Find clashes among ctl-core’s shared flags in
args. - emit_
warnings - Print each warning to stderr as
{bin}: warning: …, one per line. - env_
under_ flags - Removes each variable that the command line overrules, so the command line wins, and refuses two variables that conflict.
- resolve_
color - The colour mode in force:
ColorMode::Neverwhenno_coloris set, otherwisecolor. - switch
- The value of a
--x/--no-xpair declared with Clap’soverrides_withboth ways:on, the--xflag, returned unchanged. - warn_
opposites - Find a
--foo/--no-foopair given together inargs.