clap_git_options/
lib.rs

1//! Provides the iconic `-C`, `--git-dir` & `--work-tree` git options
2
3use clap::ValueHint;
4
5/// The `clap::Args` struct
6#[derive(clap::Args, Default, Debug)]
7#[clap(next_help_heading = "Git Options")]
8pub struct GitOptions {
9    /// Run as if was started in <path>
10    #[clap(short = 'C', num_args = 1, value_hint=ValueHint::DirPath)]
11    pub change_dir: Option<String>,
12    /// Directory where the GIT_DIR is
13    #[clap(long, value_hint=ValueHint::DirPath)]
14    pub git_dir: Option<String>,
15    /// Directory where the GIT_WORK_TREE is
16    #[clap(long, value_hint=ValueHint::DirPath)]
17    pub work_tree: Option<String>,
18}
19
20#[cfg(feature = "git-wrapper")]
21impl TryFrom<&GitOptions> for git_wrapper::Repository {
22    type Error = git_wrapper::RepoError;
23    #[inline]
24    fn try_from(args: &GitOptions) -> Result<Self, Self::Error> {
25        Self::from_args(
26            args.change_dir.as_deref(),
27            args.git_dir.as_deref(),
28            args.work_tree.as_deref(),
29        )
30    }
31}