diff --git a/clap_complete/tests/common.rs b/clap_complete/tests/common.rs index f7aa323e..54c8633d 100644 --- a/clap_complete/tests/common.rs +++ b/clap_complete/tests/common.rs @@ -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("v").short('v').conflicts_with("config")) .subcommand( - clap::Command::new("test") - .about("Subcommand") - .arg(clap::Arg::new("debug").short('d')), + clap::Command::new("test").about("Subcommand").arg( + clap::Arg::new("debug") + .short('d') + .multiple_occurrences(true), + ), ) } @@ -21,6 +23,7 @@ pub fn feature_sample_command(name: &'static str) -> clap::Command<'static> { ) .arg( clap::Arg::new("config") + .multiple_occurrences(true) .help("some config file") .short('c') .visible_short_alias('C') diff --git a/clap_complete/tests/snapshots/basic.zsh.log b/clap_complete/tests/snapshots/basic.zsh.log index c1dfe599..cd50968d 100644 --- a/clap_complete/tests/snapshots/basic.zsh.log +++ b/clap_complete/tests/snapshots/basic.zsh.log @@ -30,7 +30,7 @@ _my-app() { case $line[1] in (test) _arguments "${_arguments_options[@]}" / -'-d[]' / +'*-d[]' / '-h[Print help information]' / '--help[Print help information]' / '-c[]' / diff --git a/clap_complete/tests/snapshots/feature_sample.zsh.log b/clap_complete/tests/snapshots/feature_sample.zsh.log index fe943731..96b44ef4 100644 --- a/clap_complete/tests/snapshots/feature_sample.zsh.log +++ b/clap_complete/tests/snapshots/feature_sample.zsh.log @@ -19,10 +19,10 @@ _my-app() { '--help[Print help information]' / '-V[Print version information]' / '--version[Print version information]' / -'-c[some config file]' / -'-C[some config file]' / -'--config[some config file]' / -'--conf[some config file]' / +'*-c[some config file]' / +'*-C[some config file]' / +'*--config[some config file]' / +'*--conf[some config file]' / '::file -- some input file:_files' / '::choice:(first second)' / ":: :_my-app_commands" / diff --git a/clap_complete/tests/snapshots/special_commands.zsh.log b/clap_complete/tests/snapshots/special_commands.zsh.log index 5a94f850..07338475 100644 --- a/clap_complete/tests/snapshots/special_commands.zsh.log +++ b/clap_complete/tests/snapshots/special_commands.zsh.log @@ -19,10 +19,10 @@ _my-app() { '--help[Print help information]' / '-V[Print version information]' / '--version[Print version information]' / -'-c[some config file]' / -'-C[some config file]' / -'--config[some config file]' / -'--conf[some config file]' / +'*-c[some config file]' / +'*-C[some config file]' / +'*--config[some config file]' / +'*--conf[some config file]' / '::file -- some input file:_files' / '::choice:(first second)' / ":: :_my-app_commands" / diff --git a/clap_complete/tests/snapshots/sub_subcommands.zsh.log b/clap_complete/tests/snapshots/sub_subcommands.zsh.log index c14f152c..4b84a203 100644 --- a/clap_complete/tests/snapshots/sub_subcommands.zsh.log +++ b/clap_complete/tests/snapshots/sub_subcommands.zsh.log @@ -19,10 +19,10 @@ _my-app() { '--help[Print help information]' / '-V[Print version information]' / '--version[Print version information]' / -'-c[some config file]' / -'-C[some config file]' / -'--config[some config file]' / -'--conf[some config file]' / +'*-c[some config file]' / +'*-C[some config file]' / +'*--config[some config file]' / +'*--conf[some config file]' / '::file -- some input file:_files' / '::choice:(first second)' / ":: :_my-app_commands" / diff --git a/clap_complete_fig/src/fig.rs b/clap_complete_fig/src/fig.rs index ec4da93f..8993bd03 100644 --- a/clap_complete_fig/src/fig.rs +++ b/clap_complete_fig/src/fig.rs @@ -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(&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)); } @@ -326,3 +384,19 @@ fn gen_args(arg: &Arg, indent: usize) -> String { buffer } + +fn arg_conflicts(cmd: &Command, arg: &Arg) -> Vec { + 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 +} diff --git a/clap_complete_fig/tests/common.rs b/clap_complete_fig/tests/common.rs index f7aa323e..54c8633d 100644 --- a/clap_complete_fig/tests/common.rs +++ b/clap_complete_fig/tests/common.rs @@ -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("v").short('v').conflicts_with("config")) .subcommand( - clap::Command::new("test") - .about("Subcommand") - .arg(clap::Arg::new("debug").short('d')), + clap::Command::new("test").about("Subcommand").arg( + clap::Arg::new("debug") + .short('d') + .multiple_occurrences(true), + ), ) } @@ -21,6 +23,7 @@ pub fn feature_sample_command(name: &'static str) -> clap::Command<'static> { ) .arg( clap::Arg::new("config") + .multiple_occurrences(true) .help("some config file") .short('c') .visible_short_alias('C') diff --git a/clap_complete_fig/tests/snapshots/basic.fig.js b/clap_complete_fig/tests/snapshots/basic.fig.js index 4bcc271f..b951ee3d 100644 --- a/clap_complete_fig/tests/snapshots/basic.fig.js +++ b/clap_complete_fig/tests/snapshots/basic.fig.js @@ -8,6 +8,7 @@ const completion: Fig.Spec = { options: [ { name: "-d", + isRepeatable: true, }, { name: ["-h", "--help"], @@ -42,6 +43,9 @@ const completion: Fig.Spec = { }, { name: "-v", + exclusiveOn: [ + "-c", + ], }, ], }; diff --git a/clap_complete_fig/tests/snapshots/feature_sample.fig.js b/clap_complete_fig/tests/snapshots/feature_sample.fig.js index 5d69d01a..f3f3c527 100644 --- a/clap_complete_fig/tests/snapshots/feature_sample.fig.js +++ b/clap_complete_fig/tests/snapshots/feature_sample.fig.js @@ -47,6 +47,7 @@ const completion: Fig.Spec = { { name: ["-c", "-C", "--config", "--conf"], description: "some config file", + isRepeatable: true, }, ], args: [ diff --git a/clap_complete_fig/tests/snapshots/special_commands.fig.js b/clap_complete_fig/tests/snapshots/special_commands.fig.js index 22633808..8d74cd0f 100644 --- a/clap_complete_fig/tests/snapshots/special_commands.fig.js +++ b/clap_complete_fig/tests/snapshots/special_commands.fig.js @@ -102,6 +102,7 @@ const completion: Fig.Spec = { { name: ["-c", "-C", "--config", "--conf"], description: "some config file", + isRepeatable: true, }, ], args: [ diff --git a/clap_complete_fig/tests/snapshots/sub_subcommands.fig.js b/clap_complete_fig/tests/snapshots/sub_subcommands.fig.js index 4f3a8a98..fc93ca78 100644 --- a/clap_complete_fig/tests/snapshots/sub_subcommands.fig.js +++ b/clap_complete_fig/tests/snapshots/sub_subcommands.fig.js @@ -108,6 +108,7 @@ const completion: Fig.Spec = { { name: ["-c", "-C", "--config", "--conf"], description: "some config file", + isRepeatable: true, }, ], args: [