mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
refactor(complete): Remove low-value w macro
This commit is contained in:
parent
17d6d24232
commit
6842ed96da
6 changed files with 36 additions and 48 deletions
|
@ -1,12 +1,3 @@
|
|||
macro_rules! w {
|
||||
($buf:expr, $to_w:expr) => {
|
||||
match $buf.write_all($to_w) {
|
||||
Ok(..) => (),
|
||||
Err(..) => panic!("Failed to write to generated file"),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
macro_rules! debug {
|
||||
($($arg:tt)*) => {
|
||||
|
|
|
@ -20,9 +20,8 @@ impl Generator for Bash {
|
|||
|
||||
let fn_name = bin_name.replace('-', "__");
|
||||
|
||||
w!(
|
||||
write!(
|
||||
buf,
|
||||
format!(
|
||||
"_{name}() {{
|
||||
local i cur prev opts cmd
|
||||
COMPREPLY=()
|
||||
|
@ -72,9 +71,7 @@ fi
|
|||
name_opts_details = option_details_for_path(cmd, bin_name),
|
||||
subcmds = all_subcommands(cmd, &fn_name),
|
||||
subcmd_details = subcommand_details(cmd)
|
||||
)
|
||||
.as_bytes()
|
||||
);
|
||||
).expect("failed to write completion file");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -274,22 +271,23 @@ 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} ").expect("writing to String is infallible");
|
||||
}
|
||||
for long in utils::longs_and_visible_aliases(p) {
|
||||
write!(&mut opts, "--{long} ").unwrap();
|
||||
write!(&mut opts, "--{long} ").expect("writing to String is infallible");
|
||||
}
|
||||
for pos in p.get_positionals() {
|
||||
if let Some(vals) = utils::possible_values(pos) {
|
||||
for value in vals {
|
||||
write!(&mut opts, "{} ", value.get_name()).unwrap();
|
||||
write!(&mut opts, "{} ", value.get_name())
|
||||
.expect("writing to String is infallible");
|
||||
}
|
||||
} else {
|
||||
write!(&mut opts, "{pos} ").unwrap();
|
||||
write!(&mut opts, "{pos} ").expect("writing to String is infallible");
|
||||
}
|
||||
}
|
||||
for (sc, _) in utils::subcommands(p) {
|
||||
write!(&mut opts, "{sc} ").unwrap();
|
||||
write!(&mut opts, "{sc} ").expect("writing to String is infallible");
|
||||
}
|
||||
opts.pop();
|
||||
|
||||
|
|
|
@ -22,7 +22,8 @@ impl Generator for Elvish {
|
|||
|
||||
let subcommands_cases = generate_inner(cmd, "");
|
||||
|
||||
let result = format!(
|
||||
write!(
|
||||
buf,
|
||||
r#"
|
||||
use builtin;
|
||||
use str;
|
||||
|
@ -46,9 +47,8 @@ set edit:completion:arg-completer[{bin_name}] = {{|@words|
|
|||
$completions[$command]
|
||||
}}
|
||||
"#,
|
||||
);
|
||||
|
||||
w!(buf, result.as_bytes());
|
||||
)
|
||||
.expect("failed to write completion file");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ impl Generator for Fish {
|
|||
needs_fn_name,
|
||||
using_fn_name,
|
||||
);
|
||||
w!(buf, buffer.as_bytes());
|
||||
write!(buf, "{buffer}").expect("failed to write completion file");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -240,7 +240,9 @@ fn gen_subcommand_helpers(
|
|||
}
|
||||
}
|
||||
let optspecs_fn_name = format!("__fish_{bin_name}_global_optspecs");
|
||||
let template = format!("\
|
||||
write!(
|
||||
buf,
|
||||
"\
|
||||
# Print an optspec for argparse to handle cmd's options that are independent of any subcommand.\n\
|
||||
function {optspecs_fn_name}\n\
|
||||
\tstring join \\n{optspecs}\n\
|
||||
|
@ -264,8 +266,7 @@ fn gen_subcommand_helpers(
|
|||
\tand return 1\n\
|
||||
\tcontains -- $cmd[1] $argv\n\
|
||||
end\n\n\
|
||||
");
|
||||
w!(buf, template.as_bytes());
|
||||
").expect("failed to write completion file");
|
||||
}
|
||||
|
||||
fn value_completion(option: &Arg) -> String {
|
||||
|
|
|
@ -22,7 +22,8 @@ impl Generator for PowerShell {
|
|||
|
||||
let subcommands_cases = generate_inner(cmd, "");
|
||||
|
||||
let result = format!(
|
||||
write!(
|
||||
buf,
|
||||
r#"
|
||||
using namespace System.Management.Automation
|
||||
using namespace System.Management.Automation.Language
|
||||
|
@ -51,9 +52,8 @@ Register-ArgumentCompleter -Native -CommandName '{bin_name}' -ScriptBlock {{
|
|||
Sort-Object -Property ListItemText
|
||||
}}
|
||||
"#
|
||||
);
|
||||
|
||||
w!(buf, result.as_bytes());
|
||||
)
|
||||
.expect("failed to write completion file");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,8 @@ impl Generator for Zsh {
|
|||
.get_bin_name()
|
||||
.expect("crate::generate should have set the bin_name");
|
||||
|
||||
w!(
|
||||
write!(
|
||||
buf,
|
||||
format!(
|
||||
"#compdef {name}
|
||||
|
||||
autoload -U is-at-least
|
||||
|
@ -54,8 +53,7 @@ fi
|
|||
subcommands = get_subcommands_of(cmd),
|
||||
subcommand_details = subcommand_details(cmd)
|
||||
)
|
||||
.as_bytes()
|
||||
);
|
||||
.expect("failed to write completion file");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue