Skip to content

Commit

Permalink
feat(complete): Make '--shell' optional
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 9, 2024
1 parent efdcad1 commit fa4ccec
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions clap_complete/src/dynamic/shells/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl CompleteCommand {
pub struct CompleteArgs {
/// Specify shell to complete for
#[arg(long)]
shell: Shell,
shell: Option<Shell>,

/// Path to write completion-registration to
#[arg(long, required = true)]
Expand All @@ -217,15 +217,21 @@ impl CompleteArgs {
/// **Warning:** `stdout` should not be written to before or after this has run.
pub fn try_complete(&self, cmd: &mut clap::Command) -> clap::error::Result<()> {
debug!("CompleteCommand::try_complete: {self:?}");

let shell = self
.shell
.or_else(|| Shell::from_env())
.unwrap_or(Shell::Bash);

if let Some(out_path) = self.register.as_deref() {
let mut buf = Vec::new();
let name = cmd.get_name();
let bin = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
self.shell.write_registration(name, bin, bin, &mut buf)?;
shell.write_registration(name, bin, bin, &mut buf)?;
if out_path == std::path::Path::new("-") {
std::io::stdout().write_all(&buf)?;
} else if out_path.is_dir() {
let out_path = out_path.join(self.shell.file_name(name));
let out_path = out_path.join(shell.file_name(name));
std::fs::write(out_path, buf)?;
} else {
std::fs::write(out_path, buf)?;
Expand All @@ -234,7 +240,7 @@ impl CompleteArgs {
let current_dir = std::env::current_dir().ok();

let mut buf = Vec::new();
self.shell.write_complete(
shell.write_complete(
cmd,
self.comp_words.clone(),
current_dir.as_deref(),
Expand Down

0 comments on commit fa4ccec

Please sign in to comment.