Skip to content

Commit

Permalink
feat(args): support tilde for options (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Sep 8, 2023
1 parent 890de00 commit 8698bc2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions git-cliff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pretty_env_logger = "0.5.0"
dirs-next = "2.0.0"
clap_complete = "4.4.1"
clap_mangen = "0.2.13"
shellexpand = "3.1.0"

[dependencies.git-cliff-core]
version = "1.3.0" # managed by release.sh
Expand Down
56 changes: 51 additions & 5 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,32 @@ pub struct Opt {
#[arg(short, long, action = ArgAction::Count, alias = "debug", help_heading = Some("FLAGS"))]
pub verbose: u8,
/// Sets the configuration file.
#[arg(short, long, env = "GIT_CLIFF_CONFIG", value_name = "PATH", default_value = DEFAULT_CONFIG)]
#[arg(
short,
long,
env = "GIT_CLIFF_CONFIG",
value_name = "PATH",
default_value = DEFAULT_CONFIG,
value_parser = Opt::parse_dir
)]
pub config: PathBuf,
/// Sets the working directory.
#[arg(short, long, env = "GIT_CLIFF_WORKDIR", value_name = "PATH")]
#[arg(
short,
long,
env = "GIT_CLIFF_WORKDIR",
value_name = "PATH",
value_parser = Opt::parse_dir
)]
pub workdir: Option<PathBuf>,
/// Sets the git repository.
#[arg(
short,
long,
env = "GIT_CLIFF_REPOSITORY",
value_name = "PATH",
num_args(1..)
num_args(1..),
value_parser = Opt::parse_dir
)]
pub repository: Option<Vec<PathBuf>>,
/// Sets the path to include related commits.
Expand Down Expand Up @@ -102,10 +116,22 @@ pub struct Opt {
)]
pub with_commit: Option<Vec<String>>,
/// Prepends entries to the given changelog file.
#[arg(short, long, env = "GIT_CLIFF_PREPEND", value_name = "PATH")]
#[arg(
short,
long,
env = "GIT_CLIFF_PREPEND",
value_name = "PATH",
value_parser = Opt::parse_dir
)]
pub prepend: Option<PathBuf>,
/// Writes output to the given file.
#[arg(short, long, env = "GIT_CLIFF_OUTPUT", value_name = "PATH")]
#[arg(
short,
long,
env = "GIT_CLIFF_OUTPUT",
value_name = "PATH",
value_parser = Opt::parse_dir
)]
pub output: Option<PathBuf>,
/// Sets the tag for the latest version.
#[arg(
Expand Down Expand Up @@ -158,6 +184,18 @@ pub struct Opt {
pub range: Option<String>,
}

impl Opt {
/// Custom string parser for directories.
///
/// Expands the tilde (`~`) character in the beginning of the
/// input string into contents of the path returned by [`home_dir`].
///
/// [`home_dir`]: dirs_next::home_dir
fn parse_dir(dir: &str) -> Result<PathBuf, String> {
Ok(PathBuf::from(shellexpand::tilde(dir).to_string()))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -167,4 +205,12 @@ mod tests {
fn verify_cli() {
Opt::command().debug_assert()
}

#[test]
fn path_tilde_expansion() {
let home_dir =
dirs_next::home_dir().expect("cannot retrieve home directory");
let dir = Opt::parse_dir("~/").expect("cannot expand tilde");
assert_eq!(home_dir, dir);
}
}

0 comments on commit 8698bc2

Please sign in to comment.