Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clap_complete_nushell): correctly handle commands whose short description is more than one line #5359

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions clap_complete/src/shells/elvish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ fn escape_string(string: &str) -> String {
string.replace('\'', "''")
}

fn get_tooltip<T: ToString>(help: Option<&StyledStr>, data: T) -> String {
fn escape_help<T: ToString>(help: Option<&StyledStr>, data: T) -> String {
match help {
Some(help) => escape_string(&help.to_string()),
Some(help) => escape_string(&help.to_string().replace('\n', " ")),
_ => data.to_string(),
}
}
Expand All @@ -78,15 +78,15 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String {

for option in p.get_opts() {
if let Some(shorts) = option.get_short_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help(), shorts[0]);
let tooltip = escape_help(option.get_help(), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(format!("-{short} '{tooltip}'").as_str());
}
}

if let Some(longs) = option.get_long_and_visible_aliases() {
let tooltip = get_tooltip(option.get_help(), longs[0]);
let tooltip = escape_help(option.get_help(), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(format!("--{long} '{tooltip}'").as_str());
Expand All @@ -96,15 +96,15 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String {

for flag in utils::flags(p) {
if let Some(shorts) = flag.get_short_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help(), shorts[0]);
let tooltip = escape_help(flag.get_help(), shorts[0]);
for short in shorts {
completions.push_str(&preamble);
completions.push_str(format!("-{short} '{tooltip}'").as_str());
}
}

if let Some(longs) = flag.get_long_and_visible_aliases() {
let tooltip = get_tooltip(flag.get_help(), longs[0]);
let tooltip = escape_help(flag.get_help(), longs[0]);
for long in longs {
completions.push_str(&preamble);
completions.push_str(format!("--{long} '{tooltip}'").as_str());
Expand All @@ -114,7 +114,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String {

for subcommand in p.get_subcommands() {
let data = &subcommand.get_name();
let tooltip = get_tooltip(subcommand.get_about(), data);
let tooltip = escape_help(subcommand.get_about(), data);

completions.push_str(&preamble);
completions.push_str(format!("{data} '{tooltip}'").as_str());
Expand Down
14 changes: 8 additions & 6 deletions clap_complete/src/shells/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ fn escape_string(string: &str, escape_comma: bool) -> String {
}
}

fn escape_help(help: &clap::builder::StyledStr) -> String {
escape_string(&help.to_string().replace('\n', " "), false)
}

fn gen_fish_inner(
root_command: &str,
parent_commands: &[&str],
Expand Down Expand Up @@ -98,8 +102,7 @@ fn gen_fish_inner(
}

if let Some(data) = option.get_help() {
template
.push_str(format!(" -d '{}'", escape_string(&data.to_string(), false)).as_str());
template.push_str(&format!(" -d '{}'", escape_help(data)));
}

template.push_str(value_completion(option).as_str());
Expand All @@ -124,8 +127,7 @@ fn gen_fish_inner(
}

if let Some(data) = flag.get_help() {
template
.push_str(format!(" -d '{}'", escape_string(&data.to_string(), false)).as_str());
template.push_str(&format!(" -d '{}'", escape_help(data)));
}

buffer.push_str(template.as_str());
Expand All @@ -139,7 +141,7 @@ fn gen_fish_inner(
template.push_str(format!(" -a \"{}\"", &subcommand.get_name()).as_str());

if let Some(data) = subcommand.get_about() {
template.push_str(format!(" -d '{}'", escape_string(&data.to_string(), false)).as_str())
template.push_str(format!(" -d '{}'", escape_help(data)).as_str())
}

buffer.push_str(template.as_str());
Expand Down Expand Up @@ -173,7 +175,7 @@ fn value_completion(option: &Arg) -> String {
Some(format!(
"{}\t'{}'",
escape_string(value.get_name(), true).as_str(),
escape_string(&value.get_help().unwrap_or_default().to_string(), false)
escape_help(value.get_help().unwrap_or_default())
))
})
.collect::<Vec<_>>()
Expand Down
10 changes: 5 additions & 5 deletions clap_complete/src/shells/powershell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ fn escape_string(string: &str) -> String {
string.replace('\'', "''")
}

fn get_tooltip<T: ToString>(help: Option<&StyledStr>, data: T) -> String {
fn escape_help<T: ToString>(help: Option<&StyledStr>, data: T) -> String {
match help {
Some(help) => escape_string(&help.to_string()),
Some(help) => escape_string(&help.to_string().replace('\n', " ")),
_ => data.to_string(),
}
}
Expand All @@ -91,7 +91,7 @@ fn generate_inner(p: &Command, previous_command_name: &str) -> String {

for subcommand in p.get_subcommands() {
let data = &subcommand.get_name();
let tooltip = get_tooltip(subcommand.get_about(), data);
let tooltip = escape_help(subcommand.get_about(), data);

completions.push_str(&preamble);
completions.push_str(
Expand Down Expand Up @@ -120,7 +120,7 @@ fn generate_aliases(completions: &mut String, preamble: &String, arg: &Arg) {
use std::fmt::Write as _;

if let Some(aliases) = arg.get_short_and_visible_aliases() {
let tooltip = get_tooltip(arg.get_help(), aliases[0]);
let tooltip = escape_help(arg.get_help(), aliases[0]);
for alias in aliases {
let _ = write!(
completions,
Expand All @@ -131,7 +131,7 @@ fn generate_aliases(completions: &mut String, preamble: &String, arg: &Arg) {
}
}
if let Some(aliases) = arg.get_long_and_visible_aliases() {
let tooltip = get_tooltip(arg.get_help(), aliases[0]);
let tooltip = escape_help(arg.get_help(), aliases[0]);
for alias in aliases {
let _ = write!(
completions,
Expand Down
1 change: 1 addition & 0 deletions clap_complete/src/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ fn escape_help(string: &str) -> String {
.replace(':', "\\:")
.replace('$', "\\$")
.replace('`', "\\`")
.replace('\n', " ")
}

/// Escape value string inside single quotes and parentheses
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/basic.elvish
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ set edit:completion:arg-completer[my-app] = {|@words|
cand -v 'v'
cand -h 'Print help'
cand --help 'Print help'
cand test 'Subcommand'
cand test 'Subcommand with a second line'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'my-app;test'= {
Expand All @@ -32,7 +32,7 @@ set edit:completion:arg-completer[my-app] = {|@words|
cand --help 'Print help'
}
&'my-app;help'= {
cand test 'Subcommand'
cand test 'Subcommand with a second line'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'my-app;help;test'= {
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/basic.fish
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
complete -c my-app -n "__fish_use_subcommand" -s c
complete -c my-app -n "__fish_use_subcommand" -s v
complete -c my-app -n "__fish_use_subcommand" -s h -l help -d 'Print help'
complete -c my-app -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand'
complete -c my-app -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand with a second line'
complete -c my-app -n "__fish_use_subcommand" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c my-app -n "__fish_seen_subcommand_from test" -s d
complete -c my-app -n "__fish_seen_subcommand_from test" -s c
complete -c my-app -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand with a second line'
complete -c my-app -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/basic.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock {
[CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand')
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
break
}
Expand All @@ -37,7 +37,7 @@ Register-ArgumentCompleter -Native -CommandName 'my-app' -ScriptBlock {
break
}
'my-app;help' {
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand')
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
break
}
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/basic.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ esac
(( $+functions[_my-app_commands] )) ||
_my-app_commands() {
local commands; commands=(
'test:Subcommand' \
'test:Subcommand with a second line' \
'help:Print this message or the help of the given subcommand(s)' \
)
_describe -t commands 'my-app commands' commands "$@"
}
(( $+functions[_my-app__help_commands] )) ||
_my-app__help_commands() {
local commands; commands=(
'test:Subcommand' \
'test:Subcommand with a second line' \
'help:Print this message or the help of the given subcommand(s)' \
)
_describe -t commands 'my-app help commands' commands "$@"
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/custom_bin_name.elvish
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ set edit:completion:arg-completer[bin-name] = {|@words|
cand -v 'v'
cand -h 'Print help'
cand --help 'Print help'
cand test 'Subcommand'
cand test 'Subcommand with a second line'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'bin-name;test'= {
Expand All @@ -32,7 +32,7 @@ set edit:completion:arg-completer[bin-name] = {|@words|
cand --help 'Print help'
}
&'bin-name;help'= {
cand test 'Subcommand'
cand test 'Subcommand with a second line'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'bin-name;help;test'= {
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/custom_bin_name.fish
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
complete -c bin-name -n "__fish_use_subcommand" -s c
complete -c bin-name -n "__fish_use_subcommand" -s v
complete -c bin-name -n "__fish_use_subcommand" -s h -l help -d 'Print help'
complete -c bin-name -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand'
complete -c bin-name -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand with a second line'
complete -c bin-name -n "__fish_use_subcommand" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c bin-name -n "__fish_seen_subcommand_from test" -s d
complete -c bin-name -n "__fish_seen_subcommand_from test" -s c
complete -c bin-name -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help'
complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand'
complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand with a second line'
complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/custom_bin_name.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Register-ArgumentCompleter -Native -CommandName 'bin-name' -ScriptBlock {
[CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand')
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
break
}
Expand All @@ -37,7 +37,7 @@ Register-ArgumentCompleter -Native -CommandName 'bin-name' -ScriptBlock {
break
}
'bin-name;help' {
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand')
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand with a second line')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)')
break
}
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/custom_bin_name.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ esac
(( $+functions[_bin-name_commands] )) ||
_bin-name_commands() {
local commands; commands=(
'test:Subcommand' \
'test:Subcommand with a second line' \
'help:Print this message or the help of the given subcommand(s)' \
)
_describe -t commands 'bin-name commands' commands "$@"
}
(( $+functions[_bin-name__help_commands] )) ||
_bin-name__help_commands() {
local commands; commands=(
'test:Subcommand' \
'test:Subcommand with a second line' \
'help:Print this message or the help of the given subcommand(s)' \
)
_describe -t commands 'bin-name help commands' commands "$@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ set edit:completion:arg-completer[exhaustive] = {|@words|
cand cmd-backslash 'Avoid ''\n'''
cand cmd-brackets 'List packages [filter]'
cand cmd-expansions 'Execute the shell command with $SHELL'
cand escape-help '\tab "''
New Line'
cand escape-help '\tab "'' New Line'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'exhaustive;quote;cmd-single-quotes'= {
Expand Down Expand Up @@ -126,8 +125,7 @@ New Line'
cand cmd-backslash 'Avoid ''\n'''
cand cmd-brackets 'List packages [filter]'
cand cmd-expansions 'Execute the shell command with $SHELL'
cand escape-help '\tab "''
New Line'
cand escape-help '\tab "'' New Line'
cand help 'Print this message or the help of the given subcommand(s)'
}
&'exhaustive;quote;help;cmd-single-quotes'= {
Expand Down Expand Up @@ -269,8 +267,7 @@ New Line'
cand cmd-backslash 'Avoid ''\n'''
cand cmd-brackets 'List packages [filter]'
cand cmd-expansions 'Execute the shell command with $SHELL'
cand escape-help '\tab "''
New Line'
cand escape-help '\tab "'' New Line'
}
&'exhaustive;help;quote;cmd-single-quotes'= {
}
Expand Down