Skip to content

Commit cebeedf

Browse files
committed
Merge branch 'ein-completions'
2 parents 9b71440 + 4fd6ab8 commit cebeedf

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Diff for: src/porcelain/main.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::sync::{
33
Arc,
44
};
55

6-
use anyhow::Result;
7-
use clap::Parser;
6+
use anyhow::{anyhow, Result};
7+
use clap::{CommandFactory, Parser};
88
use gitoxide::shared::pretty::prepare_and_run;
99
use gitoxide_core as core;
1010

@@ -169,6 +169,21 @@ pub fn main() -> Result<()> {
169169
)
170170
}
171171
},
172+
Subcommands::Completions { shell, out_dir } => {
173+
let mut app = Args::command();
174+
175+
let shell = shell
176+
.or_else(clap_complete::Shell::from_env)
177+
.ok_or_else(|| anyhow!("The shell could not be derived from the environment"))?;
178+
179+
let bin_name = app.get_name().to_owned();
180+
if let Some(out_dir) = out_dir {
181+
clap_complete::generate_to(shell, &mut app, bin_name, &out_dir)?;
182+
} else {
183+
clap_complete::generate(shell, &mut app, bin_name, &mut std::io::stdout());
184+
}
185+
Ok(())
186+
}
172187
}?;
173188
Ok(())
174189
}

Diff for: src/porcelain/options.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use clap_complete::Shell;
12
use std::path::PathBuf;
23

34
#[derive(Debug, clap::Parser)]
4-
#[clap(about = "The rusty git", version = option_env!("GIX_VERSION"))]
5+
#[clap(name = "ein", about = "The rusty git", version = option_env!("GIX_VERSION"))]
56
#[clap(subcommand_required = true)]
67
pub struct Args {
78
/// Do not display verbose messages and progress information
@@ -39,6 +40,15 @@ pub enum Subcommands {
3940
/// A selection of useful tools
4041
#[clap(subcommand)]
4142
Tool(ToolCommands),
43+
/// Generate shell completions to stdout or a directory.
44+
#[clap(visible_alias = "generate-completions", visible_alias = "shell-completions")]
45+
Completions {
46+
/// The shell to generate completions for. Otherwise it's derived from the environment.
47+
#[clap(long, short)]
48+
shell: Option<Shell>,
49+
/// The output directory in case multiple files are generated. If not provided, will write to stdout.
50+
out_dir: Option<String>,
51+
},
4252
#[cfg(debug_assertions)]
4353
Panic,
4454
}

0 commit comments

Comments
 (0)