Skip to content

Commit 7573a13

Browse files
committed
feat(cli): Enable dynamic completions
1 parent 4bedc4d commit 7573a13

File tree

4 files changed

+85
-7
lines changed

4 files changed

+85
-7
lines changed

Cargo.lock

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

Cargo.toml

+11-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ required-features = ["cli"]
1818

1919
[features]
2020
default = ["cli", "bash", "elvish", "fish", "manpage", "powershell", "zsh"]
21-
completions = ["cli", "clap_complete"]
21+
completions = ["cli", "dep:clap_complete"]
2222
cli = ["clap"]
2323
bash = ["completions"]
2424
elvish = ["completions"]
@@ -40,20 +40,26 @@ snafu = "0.8"
4040
optional = true
4141
features = [ "derive", "wrap_help" ]
4242

43+
[dependencies.clap_complete]
44+
version = "4.5"
45+
optional = true
46+
features = [ "unstable-dynamic" ]
47+
4348
[dependencies.git2]
4449
version = "0.19"
4550
default-features = false
4651

4752
[build-dependencies]
4853

49-
[build-dependencies.clap_complete]
50-
version = "4.5"
51-
optional = true
52-
5354
[build-dependencies.clap_mangen]
5455
version = "0.2"
5556
optional = true
5657

58+
[build-dependencies.clap_complete]
59+
version = "4.5"
60+
optional = true
61+
features = [ "unstable-dynamic" ]
62+
5763
[build-dependencies.clap]
5864
version = "4.5"
5965
optional = true

src/bin/git-warp-time.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,20 @@ type Result<T, E = Error> = std::result::Result<T, E>;
4040

4141
fn main() -> Result<()> {
4242
let version = option_env!("VERGEN_GIT_DESCRIBE").unwrap_or_else(|| env!("CARGO_PKG_VERSION"));
43-
let app = Cli::command().version(version);
44-
let matches = app.get_matches();
43+
let mut app = Cli::command().version(version);
44+
let matches = app.clone().get_matches();
4545
let positionals = matches.get_many::<String>("paths");
4646
let repo = get_repo().context(NoRepositorySnafu)?;
4747
let mut opts = git_warp_time::Options::new()
4848
.dirty(matches.get_flag("dirty"))
4949
.ignored(matches.get_flag("ignored"))
5050
.ignore_older(matches.get_flag("ignore_older"))
5151
.verbose(!matches.get_flag("quiet"));
52+
if let Some(completions) =
53+
matches.get_one::<clap_complete::dynamic::CompleteCommand>("complete")
54+
{
55+
completions.complete(&mut app);
56+
}
5257
if matches.contains_id("paths") {
5358
let mut paths: FileSet = FileSet::new();
5459
for path in positionals.context(UnableToFormPathSnafu)? {

src/cli.rs

+13
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
// SPDX-License-Identifier: GPL-3.0-only
33

44
use clap::Parser;
5+
use clap_complete::dynamic::{ArgValueCompleter, CompleteCommand, CompletionCandidate};
56

67
/// CLI utility that resets the timestamps of files in a Git repository working directory
78
/// to the exact timestamp of the last commit which modified each file.
89
#[derive(Parser, Debug)]
910
#[clap(author, bin_name = "git-warp-time")]
1011
pub struct Cli {
12+
/// generate completions
13+
#[command(subcommand)]
14+
complete: Option<CompleteCommand>,
15+
1116
/// Include files tracked by Git but modifications in the working tee
1217
#[clap(short, long)]
1318
pub dirty: bool,
@@ -24,6 +29,14 @@ pub struct Cli {
2429
#[clap(short, long)]
2530
pub quiet: bool,
2631

32+
/// Test
33+
#[clap(add = ArgValueCompleter::new(|| { vec![
34+
CompletionCandidate::new("foo"),
35+
CompletionCandidate::new("bar"),
36+
CompletionCandidate::new("baz")] }))]
37+
pub bag: String,
38+
2739
/// Optional list of paths to operate on instead of default which is all files tracked by Git
40+
#[clap(value_hint = clap::ValueHint::FilePath)]
2841
pub paths: Vec<String>,
2942
}

0 commit comments

Comments
 (0)