Skip to content

Commit

Permalink
fix: make bash_completion optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 13, 2024
1 parent 225f074 commit 6705de4
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 13,911 deletions.
3,476 changes: 0 additions & 3,476 deletions cli/assets/completions/usage.bash

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions cli/src/cli/generate/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ pub struct Completion {
/// Defaults to "$bin --usage"
#[clap(long, required_unless_present = "file")]
usage_cmd: Option<String>,

/// Include https://github.com/scop/bash-completion
///
/// This is required for usage completions to work in bash, but the user may already provide it
#[clap(long, verbatim_doc_comment)]
include_bash_completion_lib: bool,
}

impl Completion {
Expand All @@ -44,6 +50,7 @@ impl Completion {
cache_key: self.cache_key.clone(),
spec,
usage_cmd: self.usage_cmd.clone(),
include_bash_completion_lib: self.include_bash_completion_lib,
};

println!("{}", usage::complete::complete(&opts).trim());
Expand Down
3 changes: 3 additions & 0 deletions cli/usage.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ cmd "generate" subcommand_required=true {
flag "--usage-cmd" help="A command which generates a usage spec e.g.: `mycli --usage` or `mycli completion usage` Defaults to \"$bin --usage\"" {
arg "<USAGE_CMD>"
}
flag "--include-bash-completion-lib" help="Include https://github.com/scop/bash-completion" {
long_help "Include https://github.com/scop/bash-completion\n\nThis is required for usage completions to work in bash, but the user may already provide it"
}
arg "<SHELL>" {
choices "bash" "fish" "zsh"
}
Expand Down
11 changes: 11 additions & 0 deletions docs/cli/reference/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@
"required": true,
"hide": false
}
},
{
"name": "include-bash-completion-lib",
"usage": "--include-bash-completion-lib",
"help": "Include https://github.com/scop/bash-completion",
"help_long": "Include https://github.com/scop/bash-completion\n\nThis is required for usage completions to work in bash, but the user may already provide it",
"help_first_line": "Include https://github.com/scop/bash-completion",
"short": [],
"long": ["include-bash-completion-lib"],
"hide": false,
"global": false
}
],
"mounts": [],
Expand Down
6 changes: 6 additions & 0 deletions docs/cli/reference/generate/completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ A .usage.kdl spec file to use for generating completions
### `--usage-cmd <USAGE_CMD>`

A command which generates a usage spec e.g.: `mycli --usage` or `mycli completion usage` Defaults to "$bin --usage"

### `--include-bash-completion-lib`

Include https://github.com/scop/bash-completion

This is required for usage completions to work in bash, but the user may already provide it
13 changes: 9 additions & 4 deletions lib/src/complete/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ pub fn complete_bash(opts: &CompleteOptions) -> String {
} else {
format!("_usage_spec_{bin_snake}")
};
let bash_completion = include_str!("../../bash-completion/bash_completion");
let mut out = vec![];
if opts.include_bash_completion_lib {
out.push(include_str!("../../bash-completion/bash_completion").to_string());
out.push("\n".to_string());
};
let mut out = vec![format!(
r#"{bash_completion}
_{bin_snake}() {{
r#"_{bin_snake}() {{
if ! command -v usage &> /dev/null; then
echo >&2
echo "Error: usage CLI not found. This is required for completions to work in {bin}." >&2
Expand Down Expand Up @@ -77,13 +79,15 @@ mod tests {
cache_key: None,
spec: None,
usage_cmd: Some("mycli complete --usage".to_string()),
include_bash_completion_lib: false,
}));
assert_snapshot!(complete_bash(&CompleteOptions {
shell: "bash".to_string(),
bin: "mycli".to_string(),
cache_key: Some("1.2.3".to_string()),
spec: None,
usage_cmd: Some("mycli complete --usage".to_string()),
include_bash_completion_lib: false,
}));

assert_snapshot!(complete_bash(&CompleteOptions {
Expand All @@ -92,6 +96,7 @@ mod tests {
cache_key: None,
spec: Some(SPEC_KITCHEN_SINK.clone()),
usage_cmd: None,
include_bash_completion_lib: false,
}));
}
}
3 changes: 3 additions & 0 deletions lib/src/complete/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,23 @@ mod tests {
cache_key: None,
spec: None,
usage_cmd: Some("mycli complete --usage".to_string()),
include_bash_completion_lib: false,
}));
assert_snapshot!(complete_fish(&CompleteOptions {
shell: "fish".to_string(),
bin: "mycli".to_string(),
cache_key: Some("1.2.3".to_string()),
spec: None,
usage_cmd: Some("mycli complete --usage".to_string()),
include_bash_completion_lib: false,
}));
assert_snapshot!(complete_fish(&CompleteOptions {
shell: "fish".to_string(),
bin: "mycli".to_string(),
cache_key: None,
spec: Some(SPEC_KITCHEN_SINK.clone()),
usage_cmd: None,
include_bash_completion_lib: false,
}));
}
}
1 change: 1 addition & 0 deletions lib/src/complete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct CompleteOptions {
pub cache_key: Option<String>,
pub spec: Option<Spec>,
pub usage_cmd: Option<String>,
pub include_bash_completion_lib: bool,
}

pub fn complete(options: &CompleteOptions) -> String {
Expand Down
Loading

0 comments on commit 6705de4

Please sign in to comment.