Skip to content

Commit a4d978c

Browse files
committed
Make --version flags work as expected.
1 parent 9bee8d4 commit a4d978c

File tree

7 files changed

+44
-8
lines changed

7 files changed

+44
-8
lines changed

src/plumbing-cli.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![forbid(unsafe_code)]
22

33
mod plumbing;
4+
#[cfg(feature = "lean-cli")]
5+
mod shared;
46

57
use anyhow::Result;
68

src/plumbing/lean.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ mod options {
66
#[argh(name = "gio-plumbing")]
77
/// The lean git underworld
88
pub struct Args {
9+
#[argh(switch)]
10+
/// print the program version.
11+
pub version: bool,
12+
913
#[argh(subcommand)]
1014
pub subcommand: SubCommands,
1115
}
@@ -20,10 +24,10 @@ mod options {
2024
#[derive(FromArgs, PartialEq, Debug)]
2125
#[argh(subcommand, name = "verify-pack")]
2226
pub struct VerifyPack {
23-
/// if set, output statistical information about the pack
27+
/// output statistical information about the pack
2428
#[argh(switch, short = 's')]
2529
pub statistics: bool,
26-
/// if set, verbose progress messages are printed line by line
30+
/// verbose progress messages are printed line by line
2731
#[argh(switch, short = 'v')]
2832
pub verbose: bool,
2933
/// the '.pack' or '.idx' file whose checksum to validate.
@@ -39,7 +43,7 @@ use std::io::{stderr, stdout};
3943

4044
pub fn main() -> Result<()> {
4145
pub use options::*;
42-
let cli: Args = argh::from_env();
46+
let cli: Args = crate::shared::from_env();
4347
match cli.subcommand {
4448
SubCommands::VerifyPack(VerifyPack {
4549
path,

src/plumbing/pretty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod options {
2525
/// Verify the integrity of a pack or index file
2626
#[structopt(setting = AppSettings::ColoredHelp)]
2727
VerifyPack {
28-
/// if set, output statistical information about the pack
28+
/// output statistical information about the pack
2929
#[structopt(long, short = "s")]
3030
statistics: bool,
3131
/// Determine the format to use when outputting statistics.
@@ -37,15 +37,15 @@ mod options {
3737
)]
3838
format: core::OutputFormat,
3939

40-
/// if set, verbose progress messages are printed line by line
40+
/// verbose progress messages are printed line by line
4141
#[structopt(long, short = "v")]
4242
verbose: bool,
4343

44-
/// if set, bring up a terminal user interface displaying progress visually
44+
/// bring up a terminal user interface displaying progress visually
4545
#[structopt(long, conflicts_with("verbose"))]
4646
progress: bool,
4747

48-
/// if set, the progress TUI will stay up even though the work is already completed.
48+
/// the progress TUI will stay up even though the work is already completed.
4949
///
5050
/// Use this to be able to read progress messages or additional information visible in the TUI log pane.
5151
#[structopt(long, conflicts_with("verbose"), requires("progress"))]

src/porcelain-cli.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
mod porcelain;
44

5+
#[cfg(feature = "lean-cli")]
6+
mod shared;
7+
58
use anyhow::Result;
69

710
#[cfg(feature = "pretty-cli")]

src/porcelain/lean.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ mod options {
44
#[derive(FromArgs)]
55
/// The lean git
66
pub struct Args {
7+
#[argh(switch)]
8+
/// print the program version.
9+
pub version: bool,
10+
711
#[argh(subcommand)]
812
pub subcommand: SubCommands,
913
}
@@ -25,7 +29,8 @@ use gitoxide_core as core;
2529

2630
pub fn main() -> Result<()> {
2731
pub use options::*;
28-
let cli: Args = argh::from_env();
32+
let cli: Args = crate::shared::from_env();
33+
2934
match cli.subcommand {
3035
SubCommands::Init(_) => core::init(),
3136
}

src/shared.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub static VERSION: &str = concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION"));
2+
3+
pub fn from_env<T: argh::TopLevelCommand>() -> T {
4+
let strings: Vec<String> = std::env::args().collect();
5+
let strs: Vec<&str> = strings.iter().map(|s| s.as_str()).collect();
6+
T::from_args(&[strs[0]], &strs[1..]).unwrap_or_else(|early_exit| {
7+
// This allows us to make subcommands mandatory,
8+
// and trigger a helpful message unless --version is specified
9+
if let Some(arg) = std::env::args().skip(1).next() {
10+
if arg == "--version" {
11+
println!("{}", VERSION);
12+
std::process::exit(0);
13+
}
14+
}
15+
println!("{}", early_exit.output);
16+
std::process::exit(match early_exit.status {
17+
Ok(()) => 0,
18+
Err(()) => 1,
19+
})
20+
})
21+
}

tasks.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [x] pretty + fast
66
* [x] lean + fast
77
* [x] lean + small
8+
* [x] `--version|-v` option
89
* **progress**
910
* [ ] one-line progress indicator, maybe implemented in prodash (similar to what `git` does when receiving)
1011
* **initial release**

0 commit comments

Comments
 (0)