Skip to content

Commit 1a1e35a

Browse files
committed
feat: add command for shell completions
1 parent 6035692 commit 1a1e35a

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ globset = "0.4.15"
3131
git2 = { version = "0.20.0" }
3232
regex = "1.11.1"
3333
clap = { version = "4.5.27", features = ["derive", "env"] }
34+
clap_complete = "4.5.46"
3435

3536
[dev-dependencies]
3637
rstest = "0.24.0"

src/commands/completion.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::io;
2+
3+
use clap::Command;
4+
use clap_complete::{generate, Shell};
5+
6+
/// Generate shell completions
7+
pub fn completion(shell: Shell, app: &mut Command) -> anyhow::Result<()> {
8+
generate(shell, app, app.get_name().to_string(), &mut io::stdout());
9+
Ok(())
10+
}

src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod add_provider;
22
pub mod archive;
3+
pub mod completion;
34
pub mod fetch;
45
pub mod list;
56
pub mod lock;
@@ -9,6 +10,7 @@ pub mod update;
910

1011
pub use add_provider::add_provider_to_config;
1112
pub use archive::archive;
13+
pub use completion::completion;
1214
pub use fetch::fetch;
1315
pub use list::list;
1416
pub use lock::lock;

src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use clap::Parser;
1+
use clap::{CommandFactory, Parser, ValueHint};
22
use git_workspace::commands::{
3-
add_provider_to_config, archive, execute_cmd, fetch, list, lock, pull_all_repositories, update,
3+
add_provider_to_config, archive, completion, execute_cmd, fetch, list, lock,
4+
pull_all_repositories, update,
45
};
56
use git_workspace::config::ProviderSource;
67
use git_workspace::utils::{ensure_workspace_dir_exists, expand_workspace_path};
@@ -67,6 +68,11 @@ enum Command {
6768
#[command(subcommand)]
6869
command: ProviderSource,
6970
},
71+
/// Generate shell completions
72+
Completion {
73+
/// The shell to generate the completion script for
74+
shell: clap_complete::Shell,
75+
},
7076
}
7177

7278
fn main() -> anyhow::Result<()> {
@@ -98,6 +104,7 @@ fn handle_main(args: Args) -> anyhow::Result<()> {
98104
args,
99105
} => execute_cmd(&workspace_path, threads, command, args)?,
100106
Command::SwitchAndPull { threads } => pull_all_repositories(&workspace_path, threads)?,
107+
Command::Completion { shell } => completion(shell, &mut Args::command())?,
101108
};
102109
Ok(())
103110
}

0 commit comments

Comments
 (0)