mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
style: Make clippy happy
This commit is contained in:
parent
c7d9fec2f8
commit
64639aa81c
4 changed files with 145 additions and 95 deletions
|
@ -287,7 +287,7 @@ fn parser_of<'help, 'cmd>(
|
|||
) -> Option<&'cmd Command<'help>> {
|
||||
debug!("parser_of: p={}, bin_name={}", parent.get_name(), bin_name);
|
||||
|
||||
if bin_name == parent.get_bin_name().unwrap_or(&String::new()) {
|
||||
if bin_name == parent.get_bin_name().unwrap_or("") {
|
||||
return Some(parent);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
// Std
|
||||
use std::io::Write;
|
||||
#![allow(clippy::write_with_newline)]
|
||||
|
||||
use std::fmt::Write;
|
||||
|
||||
// Internal
|
||||
use clap::*;
|
||||
|
@ -13,23 +14,27 @@ impl Generator for Fig {
|
|||
format!("{}.ts", name)
|
||||
}
|
||||
|
||||
fn generate(&self, cmd: &Command, buf: &mut dyn Write) {
|
||||
fn generate(&self, cmd: &Command, buf: &mut dyn std::io::Write) {
|
||||
let command = cmd.get_bin_name().unwrap();
|
||||
let mut buffer = String::new();
|
||||
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"const completion: Fig.Spec = {{\n name: \"{}\",\n",
|
||||
command
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
" description: \"{}\",\n",
|
||||
cmd.get_about().unwrap_or_default()
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
gen_fig_inner(command, &[], 2, cmd, &mut buffer);
|
||||
|
||||
buffer.push_str("};\n\nexport default completion;\n");
|
||||
write!(&mut buffer, "}};\n\nexport default completion;\n").unwrap();
|
||||
|
||||
buf.write_all(buffer.as_bytes())
|
||||
.expect("Failed to write to generated file");
|
||||
|
@ -49,19 +54,21 @@ fn gen_fig_inner(
|
|||
buffer: &mut String,
|
||||
) {
|
||||
if cmd.has_subcommands() {
|
||||
buffer.push_str(&format!("{:indent$}subcommands: [\n", "", indent = indent));
|
||||
write!(buffer, "{:indent$}subcommands: [\n", "", indent = indent).unwrap();
|
||||
// generate subcommands
|
||||
for subcommand in cmd.get_subcommands() {
|
||||
let mut aliases: Vec<&str> = subcommand.get_all_aliases().collect();
|
||||
if !aliases.is_empty() {
|
||||
aliases.insert(0, subcommand.get_name());
|
||||
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
buffer,
|
||||
"{:indent$}{{\n{:indent$} name: [",
|
||||
"",
|
||||
"",
|
||||
indent = indent + 2
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
buffer.push_str(
|
||||
&aliases
|
||||
|
@ -71,32 +78,32 @@ fn gen_fig_inner(
|
|||
.join(", "),
|
||||
);
|
||||
|
||||
buffer.push_str("],\n");
|
||||
write!(buffer, "],\n").unwrap();
|
||||
} else {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
buffer,
|
||||
"{:indent$}{{\n{:indent$} name: \"{}\",\n",
|
||||
"",
|
||||
"",
|
||||
subcommand.get_name(),
|
||||
indent = indent + 2
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if let Some(data) = subcommand.get_about() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
buffer,
|
||||
"{:indent$}description: \"{}\",\n",
|
||||
"",
|
||||
escape_string(data),
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if subcommand.is_hide_set() {
|
||||
buffer.push_str(&format!(
|
||||
"{:indent$}hidden: true,\n",
|
||||
"",
|
||||
indent = indent + 4
|
||||
))
|
||||
write!(buffer, "{:indent$}hidden: true,\n", "", indent = indent + 4).unwrap();
|
||||
}
|
||||
|
||||
let mut parent_commands: Vec<_> = parent_commands.into();
|
||||
|
@ -109,9 +116,9 @@ fn gen_fig_inner(
|
|||
buffer,
|
||||
);
|
||||
|
||||
buffer.push_str(&format!("{:indent$}}},\n", "", indent = indent + 2));
|
||||
write!(buffer, "{:indent$}}},\n", "", indent = indent + 2).unwrap();
|
||||
}
|
||||
buffer.push_str(&format!("{:indent$}],\n", "", indent = indent));
|
||||
write!(buffer, "{:indent$}],\n", "", indent = indent).unwrap();
|
||||
}
|
||||
|
||||
buffer.push_str(&gen_options(cmd, indent));
|
||||
|
@ -121,17 +128,17 @@ fn gen_fig_inner(
|
|||
match args.len() {
|
||||
0 => {}
|
||||
1 => {
|
||||
buffer.push_str(&format!("{:indent$}args: ", "", indent = indent));
|
||||
write!(buffer, "{:indent$}args: ", "", indent = indent).unwrap();
|
||||
|
||||
buffer.push_str(&gen_args(args[0], indent));
|
||||
}
|
||||
_ => {
|
||||
buffer.push_str(&format!("{:indent$}args: [\n", "", indent = indent));
|
||||
write!(buffer, "{:indent$}args: [\n", "", indent = indent).unwrap();
|
||||
for arg in args {
|
||||
buffer.push_str(&format!("{:indent$}", "", indent = indent + 2));
|
||||
write!(buffer, "{:indent$}", "", indent = indent + 2).unwrap();
|
||||
buffer.push_str(&gen_args(arg, indent + 2));
|
||||
}
|
||||
buffer.push_str(&format!("{:indent$}]\n", "", indent = indent));
|
||||
write!(buffer, "{:indent$}]\n", "", indent = indent).unwrap();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -142,10 +149,10 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
|
|||
let flags = generator::utils::flags(cmd);
|
||||
|
||||
if cmd.get_opts().next().is_some() || !flags.is_empty() {
|
||||
buffer.push_str(&format!("{:indent$}options: [\n", "", indent = indent));
|
||||
write!(&mut buffer, "{:indent$}options: [\n", "", indent = indent).unwrap();
|
||||
|
||||
for option in cmd.get_opts() {
|
||||
buffer.push_str(&format!("{:indent$}{{\n", "", indent = indent + 2));
|
||||
write!(&mut buffer, "{:indent$}{{\n", "", indent = indent + 2).unwrap();
|
||||
|
||||
let mut names = vec![];
|
||||
|
||||
|
@ -158,7 +165,7 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
|
|||
}
|
||||
|
||||
if names.len() > 1 {
|
||||
buffer.push_str(&format!("{:indent$}name: [", "", indent = indent + 4));
|
||||
write!(&mut buffer, "{:indent$}name: [", "", indent = indent + 4).unwrap();
|
||||
|
||||
buffer.push_str(
|
||||
&names
|
||||
|
@ -170,77 +177,91 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
|
|||
|
||||
buffer.push_str("],\n");
|
||||
} else {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}name: \"{}\",\n",
|
||||
"",
|
||||
names[0],
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if let Some(data) = option.get_help() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}description: \"{}\",\n",
|
||||
"",
|
||||
escape_string(data),
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if option.is_hide_set() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}hidden: true,\n",
|
||||
"",
|
||||
indent = indent + 4
|
||||
))
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let conflicts = arg_conflicts(cmd, option);
|
||||
|
||||
if !conflicts.is_empty() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}exclusiveOn: [\n",
|
||||
"",
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for conflict in conflicts {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}\"{}\",\n",
|
||||
"",
|
||||
conflict,
|
||||
indent = indent + 6
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
buffer.push_str(&format!("{:indent$}],\n", "", indent = indent + 4));
|
||||
write!(&mut buffer, "{:indent$}],\n", "", indent = indent + 4).unwrap();
|
||||
}
|
||||
|
||||
if let ArgAction::Set | ArgAction::Append | ArgAction::Count = option.get_action() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}isRepeatable: true,\n",
|
||||
"",
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if option.is_require_equals_set() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}requiresEquals: true,\n",
|
||||
"",
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
buffer.push_str(&format!("{:indent$}args: ", "", indent = indent + 4));
|
||||
write!(&mut buffer, "{:indent$}args: ", "", indent = indent + 4).unwrap();
|
||||
|
||||
buffer.push_str(&gen_args(option, indent + 4));
|
||||
|
||||
buffer.push_str(&format!("{:indent$}}},\n", "", indent = indent + 2));
|
||||
write!(&mut buffer, "{:indent$}}},\n", "", indent = indent + 2).unwrap();
|
||||
}
|
||||
|
||||
for flag in generator::utils::flags(cmd) {
|
||||
buffer.push_str(&format!("{:indent$}{{\n", "", indent = indent + 2));
|
||||
write!(&mut buffer, "{:indent$}{{\n", "", indent = indent + 2).unwrap();
|
||||
|
||||
let mut flags = vec![];
|
||||
|
||||
|
@ -253,7 +274,7 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
|
|||
}
|
||||
|
||||
if flags.len() > 1 {
|
||||
buffer.push_str(&format!("{:indent$}name: [", "", indent = indent + 4));
|
||||
write!(&mut buffer, "{:indent$}name: [", "", indent = indent + 4).unwrap();
|
||||
|
||||
buffer.push_str(
|
||||
&flags
|
||||
|
@ -265,56 +286,66 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
|
|||
|
||||
buffer.push_str("],\n");
|
||||
} else {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}name: \"{}\",\n",
|
||||
"",
|
||||
flags[0],
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if let Some(data) = flag.get_help() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}description: \"{}\",\n",
|
||||
"",
|
||||
escape_string(data).as_str(),
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let conflicts = arg_conflicts(cmd, &flag);
|
||||
|
||||
if !conflicts.is_empty() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}exclusiveOn: [\n",
|
||||
"",
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for conflict in conflicts {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}\"{}\",\n",
|
||||
"",
|
||||
conflict,
|
||||
indent = indent + 6
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
buffer.push_str(&format!("{:indent$}],\n", "", indent = indent + 4));
|
||||
write!(&mut buffer, "{:indent$}],\n", "", indent = indent + 4).unwrap();
|
||||
}
|
||||
|
||||
if let ArgAction::Set | ArgAction::Append | ArgAction::Count = flag.get_action() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}isRepeatable: true,\n",
|
||||
"",
|
||||
indent = indent + 4
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
buffer.push_str(&format!("{:indent$}}},\n", "", indent = indent + 2));
|
||||
write!(&mut buffer, "{:indent$}}},\n", "", indent = indent + 2).unwrap();
|
||||
}
|
||||
|
||||
buffer.push_str(&format!("{:indent$}],\n", "", indent = indent));
|
||||
write!(&mut buffer, "{:indent$}],\n", "", indent = indent).unwrap();
|
||||
}
|
||||
|
||||
buffer
|
||||
|
@ -327,95 +358,115 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
|
|||
|
||||
let mut buffer = String::new();
|
||||
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{{\n{:indent$} name: \"{}\",\n",
|
||||
"",
|
||||
arg.get_id(),
|
||||
indent = indent
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let num_args = arg.get_num_args().expect("built");
|
||||
if num_args != builder::ValueRange::EMPTY && num_args != builder::ValueRange::SINGLE {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}isVariadic: true,\n",
|
||||
"",
|
||||
indent = indent + 2
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if !arg.is_required_set() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}isOptional: true,\n",
|
||||
"",
|
||||
indent = indent + 2
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if let Some(data) = generator::utils::possible_values(arg) {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}suggestions: [\n",
|
||||
"",
|
||||
indent = indent + 2
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for value in data {
|
||||
if let Some(help) = value.get_help() {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}{{\n{:indent$} name: \"{}\",\n",
|
||||
"",
|
||||
"",
|
||||
value.get_name(),
|
||||
indent = indent + 4,
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}description: \"{}\",\n",
|
||||
"",
|
||||
escape_string(help),
|
||||
indent = indent + 6
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
buffer.push_str(&format!("{:indent$}}},\n", "", indent = indent + 4));
|
||||
write!(&mut buffer, "{:indent$}}},\n", "", indent = indent + 4).unwrap();
|
||||
} else {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}\"{}\",\n",
|
||||
"",
|
||||
value.get_name(),
|
||||
indent = indent + 4,
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
buffer.push_str(&format!("{:indent$}],\n", "", indent = indent + 2));
|
||||
write!(&mut buffer, "{:indent$}],\n", "", indent = indent + 2).unwrap();
|
||||
} else {
|
||||
match arg.get_value_hint() {
|
||||
ValueHint::AnyPath | ValueHint::FilePath | ValueHint::ExecutablePath => {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}template: \"filepaths\",\n",
|
||||
"",
|
||||
indent = indent + 2
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
ValueHint::DirPath => {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}template: \"folders\",\n",
|
||||
"",
|
||||
indent = indent + 2
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
ValueHint::CommandString | ValueHint::CommandName | ValueHint::CommandWithArguments => {
|
||||
buffer.push_str(&format!(
|
||||
write!(
|
||||
&mut buffer,
|
||||
"{:indent$}isCommand: true,\n",
|
||||
"",
|
||||
indent = indent + 2
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
// Disable completion for others
|
||||
_ => (),
|
||||
};
|
||||
};
|
||||
|
||||
buffer.push_str(&format!("{:indent$}}},\n", "", indent = indent));
|
||||
write!(&mut buffer, "{:indent$}}},\n", "", indent = indent).unwrap();
|
||||
|
||||
buffer
|
||||
}
|
||||
|
|
|
@ -436,7 +436,8 @@ fn assert_app_flags(cmd: &Command) {
|
|||
|
||||
$(
|
||||
if !cmd.$b() {
|
||||
s.push_str(&format!(" AppSettings::{} is required when AppSettings::{} is set.\n", std::stringify!($b), std::stringify!($a)));
|
||||
use std::fmt::Write;
|
||||
write!(&mut s, " AppSettings::{} is required when AppSettings::{} is set.\n", std::stringify!($b), std::stringify!($a)).unwrap();
|
||||
}
|
||||
)+
|
||||
|
||||
|
@ -451,7 +452,8 @@ fn assert_app_flags(cmd: &Command) {
|
|||
|
||||
$(
|
||||
if cmd.$b() {
|
||||
s.push_str(&format!(" AppSettings::{} conflicts with AppSettings::{}.\n", std::stringify!($b), std::stringify!($a)));
|
||||
use std::fmt::Write;
|
||||
write!(&mut s, " AppSettings::{} conflicts with AppSettings::{}.\n", std::stringify!($b), std::stringify!($a)).unwrap();
|
||||
}
|
||||
)+
|
||||
|
||||
|
@ -762,7 +764,8 @@ fn assert_arg_flags(arg: &Arg) {
|
|||
|
||||
$(
|
||||
if !arg.$b() {
|
||||
s.push_str(&format!(" Arg::{} is required when Arg::{} is set.\n", std::stringify!($b), std::stringify!($a)));
|
||||
use std::fmt::Write;
|
||||
write!(&mut s, " Arg::{} is required when Arg::{} is set.\n", std::stringify!($b), std::stringify!($a)).unwrap();
|
||||
}
|
||||
)+
|
||||
|
||||
|
|
|
@ -231,9 +231,7 @@ impl ArgMatcher {
|
|||
debug_assert_eq!(pending.ident, ident, "{}", INTERNAL_ERROR_MSG);
|
||||
}
|
||||
if trailing_values {
|
||||
pending
|
||||
.trailing_idx
|
||||
.get_or_insert_with(|| pending.raw_vals.len());
|
||||
pending.trailing_idx.get_or_insert(pending.raw_vals.len());
|
||||
}
|
||||
&mut pending.raw_vals
|
||||
}
|
||||
|
@ -241,9 +239,7 @@ impl ArgMatcher {
|
|||
pub(crate) fn start_trailing(&mut self) {
|
||||
if let Some(pending) = &mut self.pending {
|
||||
// Allow asserting its started on subsequent calls
|
||||
pending
|
||||
.trailing_idx
|
||||
.get_or_insert_with(|| pending.raw_vals.len());
|
||||
pending.trailing_idx.get_or_insert(pending.raw_vals.len());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue