Skip to content

Commit 681c607

Browse files
committed
Merge branch 'feat_completions'
2 parents a899f74 + fdd2df8 commit 681c607

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

Diff for: Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ gix = { version = "^0.54.1", path = "gix", default-features = false }
168168
time = "0.3.23"
169169

170170
clap = { version = "4.1.1", features = ["derive", "cargo"] }
171+
clap_complete = "4.4.3"
171172
prodash = { workspace = true, optional = true }
172173
is-terminal = { version = "0.4.0", optional = true }
173174
env_logger = { version = "0.10.0", default-features = false }

Diff for: src/plumbing/main.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::{
77
},
88
};
99

10-
use anyhow::{Context, Result};
11-
use clap::Parser;
10+
use anyhow::{anyhow, Context, Result};
11+
use clap::{CommandFactory, Parser};
1212
use gitoxide_core as core;
1313
use gitoxide_core::{pack::verify, repository::PathsOrPatterns};
1414
use gix::bstr::{io::BufReadExt, BString};
@@ -1219,6 +1219,18 @@ pub fn main() -> Result<()> {
12191219
},
12201220
),
12211221
},
1222+
Subcommands::Completions { shell, out_dir } => {
1223+
let mut app = Args::command();
1224+
let shell = shell
1225+
.or_else(clap_complete::Shell::from_env)
1226+
.ok_or_else(|| anyhow!("The shell could not be derived from the environment"))?;
1227+
if let Some(out_dir) = out_dir {
1228+
clap_complete::generate_to(shell, &mut app, env!("CARGO_PKG_NAME"), &out_dir)?;
1229+
} else {
1230+
clap_complete::generate(shell, &mut app, env!("CARGO_PKG_NAME"), &mut std::io::stdout());
1231+
}
1232+
Ok(())
1233+
}
12221234
}?;
12231235
Ok(())
12241236
}

Diff for: src/plumbing/options/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::path::PathBuf;
22

3+
use clap_complete::Shell;
34
use gitoxide_core as core;
45
use gix::bstr::BString;
56

@@ -134,6 +135,15 @@ pub enum Subcommands {
134135
/// Subcommands that need no git repository to run.
135136
#[clap(subcommand)]
136137
Free(free::Subcommands),
138+
/// Generate shell completions to stdout or a directory.
139+
#[clap(visible_alias = "generate-completions", visible_alias = "shell-completions")]
140+
Completions {
141+
/// The shell to generate completions for. Otherwise it's derived from the environment.
142+
#[clap(long, short)]
143+
shell: Option<Shell>,
144+
/// The output directory in case multiple files are generated. If not provided, will write to stdout.
145+
out_dir: Option<String>,
146+
},
137147
}
138148

139149
#[cfg(feature = "gitoxide-core-tools-archive")]

0 commit comments

Comments
 (0)