chore: Make format!() calls more readable

This commit is contained in:
Martin 2023-01-29 19:14:47 +00:00
parent 6e5e31a02c
commit 704d0e7a4f
46 changed files with 159 additions and 198 deletions

View file

@ -75,9 +75,9 @@ fn main() {
if let Some(generator) = opt.generator {
let mut cmd = Opt::command();
eprintln!("Generating completion file for {:?}...", generator);
eprintln!("Generating completion file for {generator:?}...");
print_completions(generator, &mut cmd);
} else {
println!("{:#?}", opt);
println!("{opt:#?}");
}
}

View file

@ -103,7 +103,7 @@ fn main() {
if let Some(generator) = matches.get_one::<Shell>("generator") {
let mut cmd = build_cli();
eprintln!("Generating completion file for {}...", generator);
eprintln!("Generating completion file for {generator}...");
print_completions(*generator, &mut cmd);
}
}

View file

@ -10,7 +10,7 @@ pub struct Bash;
impl Generator for Bash {
fn file_name(&self, name: &str) -> String {
format!("{}.bash", name)
format!("{name}.bash")
}
fn generate(&self, cmd: &Command, buf: &mut dyn Write) {
@ -114,9 +114,6 @@ fn all_subcommands(cmd: &Command) -> String {
"{parent_fn_name},{name})
cmd=\"{fn_name}\"
;;",
parent_fn_name = parent_fn_name,
name = name,
fn_name = fn_name,
));
}
@ -221,10 +218,10 @@ fn all_options_for_path(cmd: &Command, path: &str) -> String {
let mut opts = String::new();
for short in utils::shorts_and_visible_aliases(p) {
write!(&mut opts, "-{} ", short).unwrap();
write!(&mut opts, "-{short} ").unwrap();
}
for long in utils::longs_and_visible_aliases(p) {
write!(&mut opts, "--{} ", long).unwrap();
write!(&mut opts, "--{long} ").unwrap();
}
for pos in p.get_positionals() {
if let Some(vals) = utils::possible_values(pos) {
@ -232,11 +229,11 @@ fn all_options_for_path(cmd: &Command, path: &str) -> String {
write!(&mut opts, "{} ", value.get_name()).unwrap();
}
} else {
write!(&mut opts, "{} ", pos).unwrap();
write!(&mut opts, "{pos} ").unwrap();
}
}
for (sc, _) in utils::subcommands(p) {
write!(&mut opts, "{} ", sc).unwrap();
write!(&mut opts, "{sc} ").unwrap();
}
opts.pop();

View file

@ -12,7 +12,7 @@ pub struct Elvish;
impl Generator for Elvish {
fn file_name(&self, name: &str) -> String {
format!("{}.elv", name)
format!("{name}.elv")
}
fn generate(&self, cmd: &Command, buf: &mut dyn Write) {
@ -46,8 +46,6 @@ set edit:completion:arg-completer[{bin_name}] = {{|@words|
$completions[$command]
}}
"#,
bin_name = bin_name,
subcommands_cases = subcommands_cases
);
w!(buf, result.as_bytes());
@ -83,7 +81,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String {
let tooltip = get_tooltip(option.get_help(), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(format!("-{} '{}'", short, tooltip).as_str());
completions.push_str(format!("-{short} '{tooltip}'").as_str());
}
}
@ -91,7 +89,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String {
let tooltip = get_tooltip(option.get_help(), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(format!("--{} '{}'", long, tooltip).as_str());
completions.push_str(format!("--{long} '{tooltip}'").as_str());
}
}
}
@ -101,7 +99,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String {
let tooltip = get_tooltip(flag.get_help(), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(format!("-{} '{}'", short, tooltip).as_str());
completions.push_str(format!("-{short} '{tooltip}'").as_str());
}
}
@ -109,7 +107,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String {
let tooltip = get_tooltip(flag.get_help(), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(format!("--{} '{}'", long, tooltip).as_str());
completions.push_str(format!("--{long} '{tooltip}'").as_str());
}
}
}
@ -119,7 +117,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String {
let tooltip = get_tooltip(subcommand.get_about(), data);
completions.push_str(&preamble);
completions.push_str(format!("{} '{}'", data, tooltip).as_str());
completions.push_str(format!("{data} '{tooltip}'").as_str());
}
let mut subcommands_cases = format!(

View file

@ -12,7 +12,7 @@ pub struct Fish;
impl Generator for Fish {
fn file_name(&self, name: &str) -> String {
format!("{}.fish", name)
format!("{name}.fish")
}
fn generate(&self, cmd: &Command, buf: &mut dyn Write) {
@ -56,7 +56,7 @@ fn gen_fish_inner(
// -n "__fish_use_subcommand" # complete for command "myprog"
// -n "__fish_seen_subcommand_from subcmd1" # complete for command "myprog subcmd1"
let mut basic_template = format!("complete -c {}", root_command);
let mut basic_template = format!("complete -c {root_command}");
if parent_commands.is_empty() {
if cmd.has_subcommands() {
@ -68,10 +68,10 @@ fn gen_fish_inner(
" -n \"{}\"",
parent_commands
.iter()
.map(|command| format!("__fish_seen_subcommand_from {}", command))
.map(|command| format!("__fish_seen_subcommand_from {command}"))
.chain(
cmd.get_subcommands()
.map(|command| format!("not __fish_seen_subcommand_from {}", command))
.map(|command| format!("not __fish_seen_subcommand_from {command}"))
)
.collect::<Vec<_>>()
.join("; and ")
@ -87,7 +87,7 @@ fn gen_fish_inner(
if let Some(shorts) = option.get_short_and_visible_aliases() {
for short in shorts {
template.push_str(format!(" -s {}", short).as_str());
template.push_str(format!(" -s {short}").as_str());
}
}
@ -113,7 +113,7 @@ fn gen_fish_inner(
if let Some(shorts) = flag.get_short_and_visible_aliases() {
for short in shorts {
template.push_str(format!(" -s {}", short).as_str());
template.push_str(format!(" -s {short}").as_str());
}
}

View file

@ -12,7 +12,7 @@ pub struct PowerShell;
impl Generator for PowerShell {
fn file_name(&self, name: &str) -> String {
format!("_{}.ps1", name)
format!("_{name}.ps1")
}
fn generate(&self, cmd: &Command, buf: &mut dyn Write) {

View file

@ -42,7 +42,7 @@ impl FromStr for Shell {
return Ok(*variant);
}
}
Err(format!("invalid variant: {}", s))
Err(format!("invalid variant: {s}"))
}
}

View file

@ -11,7 +11,7 @@ pub struct Zsh;
impl Generator for Zsh {
fn file_name(&self, name: &str) -> String {
format!("_{}", name)
format!("_{name}")
}
fn generate(&self, cmd: &Command, buf: &mut dyn Write) {
@ -111,8 +111,8 @@ _{bin_name_underscore}_commands() {{
all_subcommands.sort();
all_subcommands.dedup();
for &(_, ref bin_name) in &all_subcommands {
debug!("subcommand_details:iter: bin_name={}", bin_name);
for (_, ref bin_name) in &all_subcommands {
debug!("subcommand_details:iter: bin_name={bin_name}");
ret.push(format!(
"\
@ -223,14 +223,12 @@ fn get_subcommands_of(parent: &Command) -> String {
let subcommand_names = utils::subcommands(parent);
let mut all_subcommands = vec![];
for &(ref name, ref bin_name) in &subcommand_names {
for (ref name, ref bin_name) in &subcommand_names {
debug!(
"get_subcommands_of:iter: parent={}, name={}, bin_name={}",
"get_subcommands_of:iter: parent={}, name={name}, bin_name={bin_name}",
parent.get_name(),
name,
bin_name,
);
let mut segments = vec![format!("({})", name)];
let mut segments = vec![format!("({name})")];
let subcommand_args = get_args_of(
parser_of(parent, bin_name).expect(INTERNAL_ERROR_MSG),
Some(parent),
@ -458,8 +456,8 @@ fn write_opts_of(p: &Command, p_global: Option<&Command>) -> String {
Some(val) => val[0].to_string(),
};
let vc = match value_completion(o) {
Some(val) => format!(":{}:{}", vn, val),
None => format!(":{}: ", vn),
Some(val) => format!(":{vn}:{val}"),
None => format!(":{vn}: "),
};
let vc = vc.repeat(o.get_num_args().expect("built").min_values());
@ -502,11 +500,11 @@ fn arg_conflicts(cmd: &Command, arg: &Arg, app_global: Option<&Command>) -> Stri
fn push_conflicts(conflicts: &[&Arg], res: &mut Vec<String>) {
for conflict in conflicts {
if let Some(s) = conflict.get_short() {
res.push(format!("-{}", s));
res.push(format!("-{s}"));
}
if let Some(l) = conflict.get_long() {
res.push(format!("--{}", l));
res.push(format!("--{l}"));
}
}
}
@ -568,13 +566,7 @@ fn write_flags_of(p: &Command, p_global: Option<&Command>) -> String {
if let Some(short_aliases) = f.get_visible_short_aliases() {
for alias in short_aliases {
let s = format!(
"'{conflicts}{multiple}-{arg}[{help}]' \\",
multiple = multiple,
conflicts = conflicts,
arg = alias,
help = help
);
let s = format!("'{conflicts}{multiple}-{alias}[{help}]' \\",);
debug!("write_flags_of:iter: Wrote...{}", &*s);

View file

@ -11,7 +11,7 @@ pub struct Fig;
impl Generator for Fig {
fn file_name(&self, name: &str) -> String {
format!("{}.ts", name)
format!("{name}.ts")
}
fn generate(&self, cmd: &Command, buf: &mut dyn std::io::Write) {
@ -267,11 +267,11 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
let mut flags = vec![];
if let Some(shorts) = flag.get_short_and_visible_aliases() {
flags.extend(shorts.iter().map(|s| format!("-{}", s)));
flags.extend(shorts.iter().map(|s| format!("-{s}")));
}
if let Some(longs) = flag.get_long_and_visible_aliases() {
flags.extend(longs.iter().map(|s| format!("--{}", s)));
flags.extend(longs.iter().map(|s| format!("--{s}")));
}
if flags.len() > 1 {

View file

@ -36,19 +36,19 @@ pub(crate) fn synopsis(roff: &mut Roff, cmd: &clap::Command) {
match (opt.get_short(), opt.get_long()) {
(Some(short), Some(long)) => {
line.push(roman(lhs));
line.push(bold(format!("-{}", short)));
line.push(bold(format!("-{short}")));
line.push(roman("|"));
line.push(bold(format!("--{}", long)));
line.push(bold(format!("--{long}",)));
line.push(roman(rhs));
}
(Some(short), None) => {
line.push(roman(lhs));
line.push(bold(format!("-{} ", short)));
line.push(bold(format!("-{short} ")));
line.push(roman(rhs));
}
(None, Some(long)) => {
line.push(roman(lhs));
line.push(bold(format!("--{}", long)));
line.push(bold(format!("--{long}")));
line.push(roman(rhs));
}
(None, None) => continue,
@ -168,7 +168,7 @@ pub(crate) fn options(roff: &mut Roff, cmd: &clap::Command) {
header.push(roman(rhs));
if let Some(defs) = option_default_values(pos) {
header.push(roman(format!(" {}", defs)));
header.push(roman(format!(" {defs}")));
}
let mut body = vec![];
@ -273,11 +273,11 @@ fn markers(required: bool) -> (&'static str, &'static str) {
}
fn short_option(opt: char) -> Inline {
bold(format!("-{}", opt))
bold(format!("-{opt}"))
}
fn long_option(opt: &str) -> Inline {
bold(format!("--{}", opt))
bold(format!("--{opt}"))
}
fn option_help(opt: &clap::Arg) -> Option<&clap::builder::StyledStr> {
@ -319,7 +319,7 @@ fn option_default_values(opt: &clap::Arg) -> Option<String> {
.collect::<Vec<_>>()
.join(",");
return Some(format!("[default: {}]", values));
return Some(format!("[default: {values}]"));
}
None
@ -343,7 +343,7 @@ fn format_possible_values(possibles: &Vec<&clap::builder::PossibleValue>) -> (Ve
for value in possibles {
let val_name = value.get_name();
match value.get_help() {
Some(help) => lines.push(format!("{}: {}", val_name, help)),
Some(help) => lines.push(format!("{val_name}: {help}")),
None => lines.push(val_name.to_string()),
}
}

View file

@ -23,5 +23,5 @@ fn main() {
let derived_matches = DerivedArgs::from_arg_matches(&matches)
.map_err(|err| err.exit())
.unwrap();
println!("Value of derived: {:#?}", derived_matches);
println!("Value of derived: {derived_matches:#?}");
}

View file

@ -17,5 +17,5 @@ fn main() {
let derived_subcommands = Subcommands::from_arg_matches(&matches)
.map_err(|err| err.exit())
.unwrap();
println!("Derived subcommands: {:#?}", derived_subcommands);
println!("Derived subcommands: {derived_subcommands:#?}");
}

View file

@ -87,5 +87,5 @@ struct Cli {
fn main() {
let args = Cli::parse();
println!("{:#?}", args);
println!("{args:#?}");
}

View file

@ -75,5 +75,5 @@ struct Cli {
fn main() {
let args = Cli::parse();
println!("{:#?}", args);
println!("{args:#?}");
}

View file

@ -102,7 +102,7 @@ fn main() {
match args.command {
Commands::Clone { remote } => {
println!("Cloning {}", remote);
println!("Cloning {remote}");
}
Commands::Diff {
mut base,
@ -136,22 +136,22 @@ fn main() {
);
}
Commands::Push { remote } => {
println!("Pushing to {}", remote);
println!("Pushing to {remote}");
}
Commands::Add { path } => {
println!("Adding {:?}", path);
println!("Adding {path:?}");
}
Commands::Stash(stash) => {
let stash_cmd = stash.command.unwrap_or(StashCommands::Push(stash.push));
match stash_cmd {
StashCommands::Push(push) => {
println!("Pushing {:?}", push);
println!("Pushing {push:?}");
}
StashCommands::Pop { stash } => {
println!("Popping {:?}", stash);
println!("Popping {stash:?}");
}
StashCommands::Apply { stash } => {
println!("Applying {:?}", stash);
println!("Applying {stash:?}");
}
}
}

View file

@ -86,7 +86,7 @@ fn main() {
let base = base.unwrap_or("stage");
let head = head.unwrap_or("worktree");
let path = path.unwrap_or("");
println!("Diffing {}..{} {} (color={})", base, head, path, color);
println!("Diffing {base}..{head} {path} (color={color})");
}
Some(("push", sub_matches)) => {
println!(
@ -100,22 +100,22 @@ fn main() {
.into_iter()
.flatten()
.collect::<Vec<_>>();
println!("Adding {:?}", paths);
println!("Adding {paths:?}");
}
Some(("stash", sub_matches)) => {
let stash_command = sub_matches.subcommand().unwrap_or(("push", sub_matches));
match stash_command {
("apply", sub_matches) => {
let stash = sub_matches.get_one::<String>("STASH");
println!("Applying {:?}", stash);
println!("Applying {stash:?}");
}
("pop", sub_matches) => {
let stash = sub_matches.get_one::<String>("STASH");
println!("Popping {:?}", stash);
println!("Popping {stash:?}");
}
("push", sub_matches) => {
let message = sub_matches.get_one::<String>("message");
println!("Pushing {:?}", message);
println!("Pushing {message:?}");
}
(name, _) => {
unreachable!("Unsupported subcommand `{}`", name)
@ -128,7 +128,7 @@ fn main() {
.into_iter()
.flatten()
.collect::<Vec<_>>();
println!("Calling out to {:?} with {:?}", ext, args);
println!("Calling out to {ext:?} with {args:?}");
}
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachabe!()
}

View file

@ -78,7 +78,7 @@ fn main() {
.map(|s| s.as_str())
.collect();
let values = packages.join(", ");
println!("Searching for {}...", values);
println!("Searching for {values}...");
return;
}
@ -90,18 +90,18 @@ fn main() {
let values = packages.join(", ");
if sync_matches.get_flag("info") {
println!("Retrieving info for {}...", values);
println!("Retrieving info for {values}...");
} else {
println!("Installing {}...", values);
println!("Installing {values}...");
}
}
Some(("query", query_matches)) => {
if let Some(packages) = query_matches.get_many::<String>("info") {
let comma_sep = packages.map(|s| s.as_str()).collect::<Vec<_>>().join(", ");
println!("Retrieving info for {}...", comma_sep);
println!("Retrieving info for {comma_sep}...");
} else if let Some(queries) = query_matches.get_many::<String>("search") {
let comma_sep = queries.map(|s| s.as_str()).collect::<Vec<_>>().join(", ");
println!("Searching Locally for {}...", comma_sep);
println!("Searching Locally for {comma_sep}...");
} else {
println!("Displaying all locally installed packages...");
}

View file

@ -17,7 +17,7 @@ fn main() -> Result<(), String> {
}
}
Err(err) => {
write!(std::io::stdout(), "{}", err).map_err(|e| e.to_string())?;
write!(std::io::stdout(), "{err}").map_err(|e| e.to_string())?;
std::io::stdout().flush().map_err(|e| e.to_string())?;
}
}

View file

@ -35,7 +35,7 @@ fn main() {
// You can check the value provided by positional arguments, or option arguments
if let Some(name) = cli.name.as_deref() {
println!("Value for name: {}", name);
println!("Value for name: {name}");
}
if let Some(config_path) = cli.config.as_deref() {

View file

@ -21,7 +21,7 @@ fn main() {
// matches just as you would the top level cmd
match &cli.command {
Commands::Add { name } => {
println!("'myapp add' was used, name is: {:?}", name)
println!("'myapp add' was used, name is: {name:?}")
}
}
}

View file

@ -21,7 +21,7 @@ const PORT_RANGE: RangeInclusive<usize> = 1..=65535;
fn port_in_range(s: &str) -> Result<u16, String> {
let port: usize = s
.parse()
.map_err(|_| format!("`{}` isn't a port number", s))?;
.map_err(|_| format!("`{s}` isn't a port number"))?;
if PORT_RANGE.contains(&port) {
Ok(port as u16)
} else {

View file

@ -56,10 +56,10 @@ fn main() {
(_, _, true) => patch += 1,
_ => unreachable!(),
};
format!("{}.{}.{}", major, minor, patch)
format!("{major}.{minor}.{patch}")
};
println!("Version: {}", version);
println!("Version: {version}");
// Check for usage of -c
if let Some(config) = cli.config.as_deref() {
@ -67,6 +67,6 @@ fn main() {
.input_file
.as_deref()
.unwrap_or_else(|| cli.spec_in.as_deref().unwrap());
println!("Doing work using input {} and config {}", input, config);
println!("Doing work using input {input} and config {config}");
}
}

View file

@ -66,10 +66,10 @@ fn main() {
.exit();
}
};
format!("{}.{}.{}", major, minor, patch)
format!("{major}.{minor}.{patch}")
};
println!("Version: {}", version);
println!("Version: {version}");
// Check for usage of -c
if let Some(config) = cli.config.as_deref() {
@ -86,6 +86,6 @@ fn main() {
)
.exit()
});
println!("Doing work using input {} and config {}", input, config);
println!("Doing work using input {input} and config {config}");
}
}

View file

@ -54,7 +54,7 @@ where
{
let pos = s
.find('=')
.ok_or_else(|| format!("invalid KEY=value: no `=` found in `{}`", s))?;
.ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}
@ -98,5 +98,5 @@ mod foreign_crate {
fn main() {
let args = Args::parse();
println!("{:?}", args);
println!("{args:?}");
}

View file

@ -4122,7 +4122,7 @@ impl Arg {
if self.val_names.len() > 1 {
self.val_names
.iter()
.map(|n| format!("<{}>", n))
.map(|n| format!("<{n}>"))
.collect::<Vec<_>>()
.join(delim)
} else {
@ -4206,9 +4206,9 @@ impl Arg {
debug_assert!(self.is_takes_value_set());
for (n, val_name) in val_names.iter().enumerate() {
let arg_name = if self.is_positional() && (num_vals.min_values() == 0 || !required) {
format!("[{}]", val_name)
format!("[{val_name}]")
} else {
format!("<{}>", val_name)
format!("<{val_name}>")
};
if n != 0 {

View file

@ -113,7 +113,7 @@ mod test {
assert!(m.is_required_set());
let m = m.unset_setting(ArgSettings::Required);
assert!(!m.is_required_set(), "{:#?}", m);
assert!(!m.is_required_set(), "{m:#?}");
}
#[test]
@ -138,8 +138,8 @@ mod test {
assert!(m.is_last_set());
let m = m.unset_setting(ArgSettings::Required | ArgSettings::Hidden | ArgSettings::Last);
assert!(!m.is_required_set(), "{:#?}", m);
assert!(!m.is_hide_set(), "{:#?}", m);
assert!(!m.is_last_set(), "{:#?}", m);
assert!(!m.is_required_set(), "{m:#?}");
assert!(!m.is_hide_set(), "{m:#?}");
assert!(!m.is_last_set(), "{m:#?}");
}
}

View file

@ -243,7 +243,7 @@ impl Command {
let a = self
.args
.remove_by_name(id)
.unwrap_or_else(|| panic!("Argument `{}` is undefined", id));
.unwrap_or_else(|| panic!("Argument `{id}` is undefined"));
self.args.push(f(a));
self
@ -287,7 +287,7 @@ impl Command {
let subcmd = if let Some(idx) = pos {
self.subcommands.remove(idx)
} else {
panic!("Command `{}` is undefined", name)
panic!("Command `{name}` is undefined")
};
self.subcommands.push(f(subcmd));
@ -822,7 +822,7 @@ impl Command {
let mut styled = StyledStr::new();
let usage = Usage::new(self);
write_help(&mut styled, self, &usage, false);
ok!(write!(w, "{}", styled));
ok!(write!(w, "{styled}"));
w.flush()
}
@ -837,7 +837,7 @@ impl Command {
let mut styled = StyledStr::new();
let usage = Usage::new(self);
write_help(&mut styled, self, &usage, true);
ok!(write!(w, "{}", styled));
ok!(write!(w, "{styled}"));
w.flush()
}
@ -3947,22 +3947,22 @@ impl Command {
sc_names.push_str(sc.name.as_str());
let mut flag_subcmd = false;
if let Some(l) = sc.get_long_flag() {
write!(sc_names, "|--{}", l).unwrap();
write!(sc_names, "|--{l}").unwrap();
flag_subcmd = true;
}
if let Some(s) = sc.get_short_flag() {
write!(sc_names, "|-{}", s).unwrap();
write!(sc_names, "|-{s}").unwrap();
flag_subcmd = true;
}
if flag_subcmd {
sc_names = format!("{{{}}}", sc_names);
sc_names = format!("{{{sc_names}}}");
}
let usage_name = self
.bin_name
.as_ref()
.map(|bin_name| format!("{}{}{}", bin_name, mid_string, sc_names))
.map(|bin_name| format!("{bin_name}{mid_string}{sc_names}"))
.unwrap_or(sc_names);
sc.usage_name = Some(usage_name);
@ -4044,19 +4044,19 @@ impl Command {
sc_names.push_str(sc.name.as_str());
let mut flag_subcmd = false;
if let Some(l) = sc.get_long_flag() {
write!(sc_names, "|--{}", l).unwrap();
write!(sc_names, "|--{l}").unwrap();
flag_subcmd = true;
}
if let Some(s) = sc.get_short_flag() {
write!(sc_names, "|-{}", s).unwrap();
write!(sc_names, "|-{s}").unwrap();
flag_subcmd = true;
}
if flag_subcmd {
sc_names = format!("{{{}}}", sc_names);
sc_names = format!("{{{sc_names}}}");
}
let usage_name = format!("{}{}{}", self_bin_name, mid_string, sc_names);
let usage_name = format!("{self_bin_name}{mid_string}{sc_names}");
debug!(
"Command::_build_bin_names:iter: Setting usage_name of {} to {:?}",
sc.name, usage_name
@ -4334,7 +4334,7 @@ impl Command {
.unwrap_or_default()
};
let display_name = self.get_display_name().unwrap_or_else(|| self.get_name());
format!("{} {}\n", display_name, ver)
format!("{display_name} {ver}\n")
}
pub(crate) fn format_group(&self, g: &Id) -> StyledStr {

View file

@ -40,20 +40,20 @@ pub(crate) fn assert_app(cmd: &Command) {
for sc in cmd.get_subcommands() {
if let Some(s) = sc.get_short_flag().as_ref() {
short_flags.push(Flag::Command(format!("-{}", s), sc.get_name()));
short_flags.push(Flag::Command(format!("-{s}"), sc.get_name()));
}
for short_alias in sc.get_all_short_flag_aliases() {
short_flags.push(Flag::Command(format!("-{}", short_alias), sc.get_name()));
short_flags.push(Flag::Command(format!("-{short_alias}"), sc.get_name()));
}
if let Some(l) = sc.get_long_flag().as_ref() {
assert!(!l.starts_with('-'), "Command {}: long_flag {:?} must not start with a `-`, that will be handled by the parser", sc.get_name(), l);
long_flags.push(Flag::Command(format!("--{}", l), sc.get_name()));
long_flags.push(Flag::Command(format!("--{l}"), sc.get_name()));
}
for long_alias in sc.get_all_long_flag_aliases() {
long_flags.push(Flag::Command(format!("--{}", long_alias), sc.get_name()));
long_flags.push(Flag::Command(format!("--{long_alias}"), sc.get_name()));
}
}
@ -68,26 +68,20 @@ pub(crate) fn assert_app(cmd: &Command) {
);
if let Some(s) = arg.get_short() {
short_flags.push(Flag::Arg(format!("-{}", s), arg.get_id().as_str()));
short_flags.push(Flag::Arg(format!("-{s}"), arg.get_id().as_str()));
}
for (short_alias, _) in &arg.short_aliases {
short_flags.push(Flag::Arg(
format!("-{}", short_alias),
arg.get_id().as_str(),
));
short_flags.push(Flag::Arg(format!("-{short_alias}"), arg.get_id().as_str()));
}
if let Some(l) = arg.get_long() {
assert!(!l.starts_with('-'), "Argument {}: long {:?} must not start with a `-`, that will be handled by the parser", arg.get_id(), l);
long_flags.push(Flag::Arg(format!("--{}", l), arg.get_id().as_str()));
long_flags.push(Flag::Arg(format!("--{l}"), arg.get_id().as_str()));
}
for (long_alias, _) in &arg.aliases {
long_flags.push(Flag::Arg(
format!("--{}", long_alias),
arg.get_id().as_str(),
));
long_flags.push(Flag::Arg(format!("--{long_alias}"), arg.get_id().as_str()));
}
// Name conflicts
@ -429,19 +423,16 @@ fn detect_duplicate_flags(flags: &[Flag], short_or_long: &str) {
for (one, two) in find_duplicates(flags) {
match (one, two) {
(Command(flag, one), Command(_, another)) if one != another => panic!(
"the '{}' {} flag is specified for both '{}' and '{}' subcommands",
flag, short_or_long, one, another
"the '{flag}' {short_or_long} flag is specified for both '{one}' and '{another}' subcommands"
),
(Arg(flag, one), Arg(_, another)) if one != another => panic!(
"{} option names must be unique, but '{}' is in use by both '{}' and '{}'",
short_or_long, flag, one, another
"{short_or_long} option names must be unique, but '{flag}' is in use by both '{one}' and '{another}'"
),
(Arg(flag, arg), Command(_, sub)) | (Command(flag, sub), Arg(_, arg)) => panic!(
"the '{}' {} flag for the '{}' argument conflicts with the short flag \
for '{}' subcommand",
flag, short_or_long, arg, sub
"the '{flag}' {short_or_long} flag for the '{arg}' argument conflicts with the short flag \
for '{sub}' subcommand"
),
_ => {}
@ -529,10 +520,8 @@ fn _verify_positionals(cmd: &Command) -> bool {
assert!(
highest_idx == num_p,
"Found positional argument whose index is {} but there \
are only {} positional arguments defined",
highest_idx,
num_p
"Found positional argument whose index is {highest_idx} but there \
are only {num_p} positional arguments defined",
);
for arg in cmd.get_arguments() {

View file

@ -183,7 +183,7 @@ impl std::fmt::Display for ValueRange {
impl std::fmt::Debug for ValueRange {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
write!(f, "{self}")
}
}

View file

@ -89,7 +89,7 @@ impl FromStr for ValueHint {
"hostname" => ValueHint::Hostname,
"url" => ValueHint::Url,
"emailaddress" => ValueHint::EmailAddress,
_ => return Err(format!("unknown ValueHint: `{}`", s)),
_ => return Err(format!("unknown ValueHint: `{s}`")),
})
}
}

View file

@ -385,7 +385,7 @@ pub trait ValueEnum: Sized + Clone {
.matches(input, ignore_case)
})
.cloned()
.ok_or_else(|| format!("invalid variant: {}", input))
.ok_or_else(|| format!("invalid variant: {input}"))
}
/// The canonical argument value.

View file

@ -461,7 +461,7 @@ fn did_you_mean(styled: &mut StyledStr, context: &str, valid: &ContextValue) {
fn escape(s: impl AsRef<str>) -> String {
let s = s.as_ref();
if s.contains(char::is_whitespace) {
format!("{:?}", s)
format!("{s:?}")
} else {
s.to_owned()
}

View file

@ -692,7 +692,7 @@ impl<F: ErrorFormatter> Error<F> {
Some((flag, None)) => {
err = err.insert_context_unchecked(
ContextKind::SuggestedArg,
ContextValue::String(format!("--{}", flag)),
ContextValue::String(format!("--{flag}")),
);
}
None => {}
@ -782,7 +782,7 @@ impl<F: ErrorFormatter> Display for Error<F> {
if let Some(backtrace) = self.inner.backtrace.as_ref() {
ok!(writeln!(f));
ok!(writeln!(f, "Backtrace:"));
ok!(writeln!(f, "{}", backtrace));
ok!(writeln!(f, "{backtrace}"));
}
Ok(())
}

View file

@ -410,7 +410,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
self.none("\n\n");
}
first = false;
self.header(format!("{}:\n", heading));
self.header(format!("{heading}:\n"));
self.write_args(&args, heading, option_sort_key);
}
}
@ -485,7 +485,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
debug!("HelpTemplate::short");
if let Some(s) = arg.get_short() {
self.literal(format!("-{}", s));
self.literal(format!("-{s}"));
} else if arg.get_long().is_some() {
self.none(" ");
}
@ -498,7 +498,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
if arg.get_short().is_some() {
self.none(", ");
}
self.literal(format!("--{}", long));
self.literal(format!("--{long}"));
}
}
@ -738,7 +738,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
.map(|pvs| pvs.to_string_lossy())
.map(|pvs| {
if pvs.contains(char::is_whitespace) {
Cow::from(format!("{:?}", pvs))
Cow::from(format!("{pvs:?}"))
} else {
pvs
}
@ -746,7 +746,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
.collect::<Vec<_>>()
.join(" ");
spec_vals.push(format!("[default: {}]", pvs));
spec_vals.push(format!("[default: {pvs}]"));
}
let als = a
@ -758,7 +758,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
.join(", ");
if !als.is_empty() {
debug!("HelpTemplate::spec_vals: Found aliases...{:?}", a.aliases);
spec_vals.push(format!("[aliases: {}]", als));
spec_vals.push(format!("[aliases: {als}]"));
}
let als = a
@ -773,7 +773,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
"HelpTemplate::spec_vals: Found short aliases...{:?}",
a.short_aliases
);
spec_vals.push(format!("[short aliases: {}]", als));
spec_vals.push(format!("[short aliases: {als}]"));
}
let possible_vals = a.get_possible_values();
@ -792,7 +792,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
.collect::<Vec<_>>()
.join(", ");
spec_vals.push(format!("[possible values: {}]", pvs));
spec_vals.push(format!("[possible values: {pvs}]"));
}
let connector = if self.use_long { "\n" } else { " " };
spec_vals.join(connector)
@ -835,11 +835,11 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
styled.literal(subcommand.get_name());
if let Some(short) = subcommand.get_short_flag() {
styled.none(", ");
styled.literal(format!("-{}", short));
styled.literal(format!("-{short}"));
}
if let Some(long) = subcommand.get_long_flag() {
styled.none(", ");
styled.literal(format!("--{}", long));
styled.literal(format!("--{long}"));
}
longest = longest.max(styled.display_width());
ord_v.push((subcommand.get_display_order(), styled, subcommand));
@ -902,7 +902,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
let mut short_als = a
.get_visible_short_flag_aliases()
.map(|a| format!("-{}", a))
.map(|a| format!("-{a}"))
.collect::<Vec<_>>();
let als = a.get_visible_aliases().map(|s| s.to_string());
short_als.extend(als);
@ -916,7 +916,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
"HelpTemplate::spec_vals: Found short flag aliases...{:?}",
a.get_all_short_flag_aliases().collect::<Vec<_>>()
);
spec_vals.push(format!("[aliases: {}]", all_als));
spec_vals.push(format!("[aliases: {all_als}]"));
}
spec_vals.join(" ")

View file

@ -105,7 +105,7 @@ mod tests {
assert_eq!(ch.width().unwrap(), 1, "char: {}", desc);
#[cfg(not(feature = "unicode"))]
assert_eq!(ch_width(ch), 1, "char: {}", desc);
assert_eq!(ch_width(ch), 1, "char: {desc}");
}
}
@ -123,7 +123,7 @@ mod tests {
assert!(ch.width().unwrap() <= 2, "char: {}", desc);
#[cfg(not(feature = "unicode"))]
assert_eq!(ch_width(ch), 1, "char: {}", desc);
assert_eq!(ch_width(ch), 1, "char: {desc}");
}
}

View file

@ -217,9 +217,9 @@ impl ArgMatcher {
raw_vals: Default::default(),
trailing_idx: None,
});
debug_assert_eq!(pending.id, *id, "{}", INTERNAL_ERROR_MSG);
debug_assert_eq!(pending.id, *id, "{INTERNAL_ERROR_MSG}");
if ident.is_some() {
debug_assert_eq!(pending.ident, ident, "{}", INTERNAL_ERROR_MSG);
debug_assert_eq!(pending.ident, ident, "{INTERNAL_ERROR_MSG}");
}
if trailing_values {
pending.trailing_idx.get_or_insert(pending.raw_vals.len());

View file

@ -27,10 +27,7 @@ impl MatchesError {
}
Err(err) => err,
};
panic!(
"Mismatch between definition and access of `{}`. {}",
id, err
)
panic!("Mismatch between definition and access of `{id}`. {err}",)
}
}
@ -42,8 +39,7 @@ impl std::fmt::Display for MatchesError {
Self::Downcast { actual, expected } => {
writeln!(
f,
"Could not downcast to {:?}, need to downcast to {:?}",
expected, actual
"Could not downcast to {expected:?}, need to downcast to {actual:?}"
)
}
Self::UnknownArgument {} => {

View file

@ -1302,10 +1302,9 @@ impl ArgMatches {
if arg == Id::EXTERNAL || self.valid_args.iter().any(|s| *s == arg) {
} else {
panic!(
"`{:?}` is not an id of an argument or a group.\n\
"`{arg:?}` is not an id of an argument or a group.\n\
Make sure you're using the name of the argument itself \
and not the name of short or long flags.",
arg
and not the name of short or long flags."
);
}
}
@ -1320,7 +1319,7 @@ impl ArgMatches {
{
if name.is_empty() || self.valid_subcommands.iter().any(|s| *s == name) {
} else {
panic!("`{}` is not a name of a subcommand.", name);
panic!("`{name}` is not a name of a subcommand.");
}
}

View file

@ -895,8 +895,7 @@ impl<'cmd> Parser<'cmd> {
debug_assert_eq!(
res,
Ok(()),
"tracking of `flag_subcmd_skip` is off for `{:?}`",
short_arg
"tracking of `flag_subcmd_skip` is off for `{short_arg:?}`"
);
while let Some(c) = short_arg.next_flag() {
let c = match c {
@ -982,7 +981,7 @@ impl<'cmd> Parser<'cmd> {
Ok(ParseResult::FlagSubCommand(name))
} else {
Ok(ParseResult::NoMatchingArg {
arg: format!("-{}", c),
arg: format!("-{c}"),
})
};
}
@ -1570,7 +1569,7 @@ impl<'cmd> Parser<'cmd> {
did_you_mean.is_none() && !trailing_values && self.cmd.has_positionals();
ClapError::unknown_argument(
self.cmd,
format!("--{}", arg),
format!("--{arg}"),
did_you_mean,
suggested_trailing_arg,
Usage::new(self.cmd)

View file

@ -519,13 +519,10 @@ fn gather_direct_conflicts(cmd: &Command, id: &Id) -> Vec<Id> {
} else if let Some(group) = cmd.find_group(id) {
gather_group_direct_conflicts(group)
} else {
debug_assert!(false, "id={:?} is unknown", id);
debug_assert!(false, "id={id:?} is unknown");
Vec::new()
};
debug!(
"Conflicts::gather_direct_conflicts id={:?}, conflicts={:?}",
id, conf
);
debug!("Conflicts::gather_direct_conflicts id={id:?}, conflicts={conf:?}",);
conf
}

View file

@ -82,7 +82,7 @@ impl std::str::FromStr for ColorChoice {
return Ok(*variant);
}
}
Err(format!("invalid variant: {}", s))
Err(format!("invalid variant: {s}"))
}
}

View file

@ -21,7 +21,7 @@ fn single_long_arg_without_value() {
let r = cmd.try_get_matches_from(vec!["cmd", "--config" /* missing: , "config file" */]);
assert!(r.is_ok(), "unexpected error: {:?}", r);
assert!(r.is_ok(), "unexpected error: {r:?}");
let m = r.unwrap();
assert!(m.contains_id("config"));
}

View file

@ -179,7 +179,7 @@ fn positional_possible_values() {
Arg::new("positional").index(1).value_parser(["test123"]),
])
.try_get_matches_from(vec!["", "-f", "test123"]);
assert!(r.is_ok(), "{:#?}", r);
assert!(r.is_ok(), "{r:#?}");
let m = r.unwrap();
assert!(m.contains_id("positional"));
assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));

View file

@ -195,7 +195,7 @@ pub fn check_complex_output(args: &str, out: &str) {
}
if let Some(p) = matches.get_one::<String>("positional").map(|v| v.as_str()) {
writeln!(w, "positional present with value: {}", p).unwrap();
writeln!(w, "positional present with value: {p}").unwrap();
} else {
writeln!(w, "positional NOT present").unwrap();
}
@ -207,17 +207,17 @@ pub fn check_complex_output(args: &str, out: &str) {
writeln!(w, "flag NOT present").unwrap();
}
n => {
writeln!(w, "flag present {} times", n).unwrap();
writeln!(w, "flag present {n} times").unwrap();
}
}
if matches.contains_id("option") {
if let Some(v) = matches.get_one::<String>("option").map(|v| v.as_str()) {
writeln!(w, "scoption present with value: {}", v).unwrap();
writeln!(w, "scoption present with value: {v}").unwrap();
}
if let Some(ov) = matches.get_many::<String>("option") {
for o in ov {
writeln!(w, "An scoption: {}", o).unwrap();
writeln!(w, "An scoption: {o}").unwrap();
}
}
} else {
@ -228,7 +228,7 @@ pub fn check_complex_output(args: &str, out: &str) {
.get_one::<String>("scpositional")
.map(|v| v.as_str())
{
writeln!(w, "scpositional present with value: {}", p).unwrap();
writeln!(w, "scpositional present with value: {p}").unwrap();
}
}
} else {

View file

@ -28,7 +28,7 @@ pub fn assert_output(l: Command, args: &str, expected: &str, stderr: bool) {
let mut buf = Cursor::new(Vec::with_capacity(50));
let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
let err = res.unwrap_err();
write!(&mut buf, "{}", err).unwrap();
write!(&mut buf, "{err}").unwrap();
let actual = buf.into_inner();
let actual = String::from_utf8(actual).unwrap();
assert_eq!(

View file

@ -17,8 +17,8 @@ pub const FULL_TEMPLATE: &str = "\
pub fn get_help<T: CommandFactory>() -> String {
let output = <T as CommandFactory>::command().render_help().to_string();
eprintln!("\n%%% HELP %%%:=====\n{}\n=====\n", output);
eprintln!("\n%%% HELP (DEBUG) %%%:=====\n{:?}\n=====\n", output);
eprintln!("\n%%% HELP %%%:=====\n{output}\n=====\n");
eprintln!("\n%%% HELP (DEBUG) %%%:=====\n{output:?}\n=====\n");
output
}
@ -28,8 +28,8 @@ pub fn get_long_help<T: CommandFactory>() -> String {
.render_long_help()
.to_string();
eprintln!("\n%%% LONG_HELP %%%:=====\n{}\n=====\n", output);
eprintln!("\n%%% LONG_HELP (DEBUG) %%%:=====\n{:?}\n=====\n", output);
eprintln!("\n%%% LONG_HELP %%%:=====\n{output}\n=====\n");
eprintln!("\n%%% LONG_HELP (DEBUG) %%%:=====\n{output:?}\n=====\n");
output
}
@ -42,14 +42,8 @@ pub fn get_subcommand_long_help<T: CommandFactory>(subcmd: &str) -> String {
.render_long_help()
.to_string();
eprintln!(
"\n%%% SUBCOMMAND `{}` HELP %%%:=====\n{}\n=====\n",
subcmd, output
);
eprintln!(
"\n%%% SUBCOMMAND `{}` HELP (DEBUG) %%%:=====\n{:?}\n=====\n",
subcmd, output
);
eprintln!("\n%%% SUBCOMMAND `{subcmd}` HELP %%%:=====\n{output}\n=====\n",);
eprintln!("\n%%% SUBCOMMAND `{subcmd}` HELP (DEBUG) %%%:=====\n{output:?}\n=====\n",);
output
}