feat(clap_complete_fig): Add exclusiveOn and isRepeatable

This commit is contained in:
Federico Ciardi 2022-03-17 21:29:26 +01:00
parent f755198349
commit abc72a7d0b
11 changed files with 106 additions and 19 deletions

View file

@ -3,9 +3,11 @@ pub fn basic_command(name: &'static str) -> clap::Command<'static> {
.arg(clap::Arg::new("config").short('c').global(true)) .arg(clap::Arg::new("config").short('c').global(true))
.arg(clap::Arg::new("v").short('v').conflicts_with("config")) .arg(clap::Arg::new("v").short('v').conflicts_with("config"))
.subcommand( .subcommand(
clap::Command::new("test") clap::Command::new("test").about("Subcommand").arg(
.about("Subcommand") clap::Arg::new("debug")
.arg(clap::Arg::new("debug").short('d')), .short('d')
.multiple_occurrences(true),
),
) )
} }
@ -21,6 +23,7 @@ pub fn feature_sample_command(name: &'static str) -> clap::Command<'static> {
) )
.arg( .arg(
clap::Arg::new("config") clap::Arg::new("config")
.multiple_occurrences(true)
.help("some config file") .help("some config file")
.short('c') .short('c')
.visible_short_alias('C') .visible_short_alias('C')

View file

@ -30,7 +30,7 @@ _my-app() {
case $line[1] in case $line[1] in
(test) (test)
_arguments "${_arguments_options[@]}" / _arguments "${_arguments_options[@]}" /
'-d[]' / '*-d[]' /
'-h[Print help information]' / '-h[Print help information]' /
'--help[Print help information]' / '--help[Print help information]' /
'-c[]' / '-c[]' /

View file

@ -19,10 +19,10 @@ _my-app() {
'--help[Print help information]' / '--help[Print help information]' /
'-V[Print version information]' / '-V[Print version information]' /
'--version[Print version information]' / '--version[Print version information]' /
'-c[some config file]' / '*-c[some config file]' /
'-C[some config file]' / '*-C[some config file]' /
'--config[some config file]' / '*--config[some config file]' /
'--conf[some config file]' / '*--conf[some config file]' /
'::file -- some input file:_files' / '::file -- some input file:_files' /
'::choice:(first second)' / '::choice:(first second)' /
":: :_my-app_commands" / ":: :_my-app_commands" /

View file

@ -19,10 +19,10 @@ _my-app() {
'--help[Print help information]' / '--help[Print help information]' /
'-V[Print version information]' / '-V[Print version information]' /
'--version[Print version information]' / '--version[Print version information]' /
'-c[some config file]' / '*-c[some config file]' /
'-C[some config file]' / '*-C[some config file]' /
'--config[some config file]' / '*--config[some config file]' /
'--conf[some config file]' / '*--conf[some config file]' /
'::file -- some input file:_files' / '::file -- some input file:_files' /
'::choice:(first second)' / '::choice:(first second)' /
":: :_my-app_commands" / ":: :_my-app_commands" /

View file

@ -19,10 +19,10 @@ _my-app() {
'--help[Print help information]' / '--help[Print help information]' /
'-V[Print version information]' / '-V[Print version information]' /
'--version[Print version information]' / '--version[Print version information]' /
'-c[some config file]' / '*-c[some config file]' /
'-C[some config file]' / '*-C[some config file]' /
'--config[some config file]' / '*--config[some config file]' /
'--conf[some config file]' / '*--conf[some config file]' /
'::file -- some input file:_files' / '::file -- some input file:_files' /
'::choice:(first second)' / '::choice:(first second)' /
":: :_my-app_commands" / ":: :_my-app_commands" /

View file

@ -177,6 +177,35 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
)) ))
} }
let conflicts = arg_conflicts(cmd, option);
if !conflicts.is_empty() {
buffer.push_str(&format!(
"{:indent$}exclusiveOn: [\n",
"",
indent = indent + 4
));
for conflict in conflicts {
buffer.push_str(&format!(
"{:indent$}\"{}\",\n",
"",
conflict,
indent = indent + 6
));
}
buffer.push_str(&format!("{:indent$}],\n", "", indent = indent + 4));
}
if option.is_multiple_occurrences_set() {
buffer.push_str(&format!(
"{:indent$}isRepeatable: true,\n",
"",
indent = indent + 4
));
}
buffer.push_str(&format!("{:indent$}args: ", "", indent = indent + 4)); buffer.push_str(&format!("{:indent$}args: ", "", indent = indent + 4));
buffer.push_str(&gen_args(option, indent + 4)); buffer.push_str(&gen_args(option, indent + 4));
@ -227,6 +256,35 @@ fn gen_options(cmd: &Command, indent: usize) -> String {
)); ));
} }
let conflicts = arg_conflicts(cmd, &flag);
if !conflicts.is_empty() {
buffer.push_str(&format!(
"{:indent$}exclusiveOn: [\n",
"",
indent = indent + 4
));
for conflict in conflicts {
buffer.push_str(&format!(
"{:indent$}\"{}\",\n",
"",
conflict,
indent = indent + 6
));
}
buffer.push_str(&format!("{:indent$}],\n", "", indent = indent + 4));
}
if flag.is_multiple_occurrences_set() {
buffer.push_str(&format!(
"{:indent$}isRepeatable: true,\n",
"",
indent = indent + 4
));
}
buffer.push_str(&format!("{:indent$}}},\n", "", indent = indent + 2)); buffer.push_str(&format!("{:indent$}}},\n", "", indent = indent + 2));
} }
@ -326,3 +384,19 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
buffer buffer
} }
fn arg_conflicts(cmd: &Command, arg: &Arg) -> Vec<String> {
let mut res = vec![];
for conflict in cmd.get_arg_conflicts_with(arg) {
if let Some(s) = conflict.get_short() {
res.push(format!("-{}", s));
}
if let Some(l) = conflict.get_long() {
res.push(format!("--{}", l));
}
}
res
}

View file

@ -3,9 +3,11 @@ pub fn basic_command(name: &'static str) -> clap::Command<'static> {
.arg(clap::Arg::new("config").short('c').global(true)) .arg(clap::Arg::new("config").short('c').global(true))
.arg(clap::Arg::new("v").short('v').conflicts_with("config")) .arg(clap::Arg::new("v").short('v').conflicts_with("config"))
.subcommand( .subcommand(
clap::Command::new("test") clap::Command::new("test").about("Subcommand").arg(
.about("Subcommand") clap::Arg::new("debug")
.arg(clap::Arg::new("debug").short('d')), .short('d')
.multiple_occurrences(true),
),
) )
} }
@ -21,6 +23,7 @@ pub fn feature_sample_command(name: &'static str) -> clap::Command<'static> {
) )
.arg( .arg(
clap::Arg::new("config") clap::Arg::new("config")
.multiple_occurrences(true)
.help("some config file") .help("some config file")
.short('c') .short('c')
.visible_short_alias('C') .visible_short_alias('C')

View file

@ -8,6 +8,7 @@ const completion: Fig.Spec = {
options: [ options: [
{ {
name: "-d", name: "-d",
isRepeatable: true,
}, },
{ {
name: ["-h", "--help"], name: ["-h", "--help"],
@ -42,6 +43,9 @@ const completion: Fig.Spec = {
}, },
{ {
name: "-v", name: "-v",
exclusiveOn: [
"-c",
],
}, },
], ],
}; };

View file

@ -47,6 +47,7 @@ const completion: Fig.Spec = {
{ {
name: ["-c", "-C", "--config", "--conf"], name: ["-c", "-C", "--config", "--conf"],
description: "some config file", description: "some config file",
isRepeatable: true,
}, },
], ],
args: [ args: [

View file

@ -102,6 +102,7 @@ const completion: Fig.Spec = {
{ {
name: ["-c", "-C", "--config", "--conf"], name: ["-c", "-C", "--config", "--conf"],
description: "some config file", description: "some config file",
isRepeatable: true,
}, },
], ],
args: [ args: [

View file

@ -108,6 +108,7 @@ const completion: Fig.Spec = {
{ {
name: ["-c", "-C", "--config", "--conf"], name: ["-c", "-C", "--config", "--conf"],
description: "some config file", description: "some config file",
isRepeatable: true,
}, },
], ],
args: [ args: [