Skip to content

Commit

Permalink
Make --version flags work as expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 3, 2020
1 parent 9bee8d4 commit a4d978c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/plumbing-cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![forbid(unsafe_code)]

mod plumbing;
#[cfg(feature = "lean-cli")]
mod shared;

use anyhow::Result;

Expand Down
10 changes: 7 additions & 3 deletions src/plumbing/lean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ mod options {
#[argh(name = "gio-plumbing")]
/// The lean git underworld
pub struct Args {
#[argh(switch)]
/// print the program version.
pub version: bool,

#[argh(subcommand)]
pub subcommand: SubCommands,
}
Expand All @@ -20,10 +24,10 @@ mod options {
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "verify-pack")]
pub struct VerifyPack {
/// if set, output statistical information about the pack
/// output statistical information about the pack
#[argh(switch, short = 's')]
pub statistics: bool,
/// if set, verbose progress messages are printed line by line
/// verbose progress messages are printed line by line
#[argh(switch, short = 'v')]
pub verbose: bool,
/// the '.pack' or '.idx' file whose checksum to validate.
Expand All @@ -39,7 +43,7 @@ use std::io::{stderr, stdout};

pub fn main() -> Result<()> {
pub use options::*;
let cli: Args = argh::from_env();
let cli: Args = crate::shared::from_env();
match cli.subcommand {
SubCommands::VerifyPack(VerifyPack {
path,
Expand Down
8 changes: 4 additions & 4 deletions src/plumbing/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod options {
/// Verify the integrity of a pack or index file
#[structopt(setting = AppSettings::ColoredHelp)]
VerifyPack {
/// if set, output statistical information about the pack
/// output statistical information about the pack
#[structopt(long, short = "s")]
statistics: bool,
/// Determine the format to use when outputting statistics.
Expand All @@ -37,15 +37,15 @@ mod options {
)]
format: core::OutputFormat,

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

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

/// if set, the progress TUI will stay up even though the work is already completed.
/// the progress TUI will stay up even though the work is already completed.
///
/// Use this to be able to read progress messages or additional information visible in the TUI log pane.
#[structopt(long, conflicts_with("verbose"), requires("progress"))]
Expand Down
3 changes: 3 additions & 0 deletions src/porcelain-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

mod porcelain;

#[cfg(feature = "lean-cli")]
mod shared;

use anyhow::Result;

#[cfg(feature = "pretty-cli")]
Expand Down
7 changes: 6 additions & 1 deletion src/porcelain/lean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ mod options {
#[derive(FromArgs)]
/// The lean git
pub struct Args {
#[argh(switch)]
/// print the program version.
pub version: bool,

#[argh(subcommand)]
pub subcommand: SubCommands,
}
Expand All @@ -25,7 +29,8 @@ use gitoxide_core as core;

pub fn main() -> Result<()> {
pub use options::*;
let cli: Args = argh::from_env();
let cli: Args = crate::shared::from_env();

match cli.subcommand {
SubCommands::Init(_) => core::init(),
}
Expand Down
21 changes: 21 additions & 0 deletions src/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pub static VERSION: &str = concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION"));

pub fn from_env<T: argh::TopLevelCommand>() -> T {
let strings: Vec<String> = std::env::args().collect();
let strs: Vec<&str> = strings.iter().map(|s| s.as_str()).collect();
T::from_args(&[strs[0]], &strs[1..]).unwrap_or_else(|early_exit| {
// This allows us to make subcommands mandatory,
// and trigger a helpful message unless --version is specified
if let Some(arg) = std::env::args().skip(1).next() {
if arg == "--version" {
println!("{}", VERSION);
std::process::exit(0);
}
}
println!("{}", early_exit.output);
std::process::exit(match early_exit.status {
Ok(()) => 0,
Err(()) => 1,
})
})
}
1 change: 1 addition & 0 deletions tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [x] pretty + fast
* [x] lean + fast
* [x] lean + small
* [x] `--version|-v` option
* **progress**
* [ ] one-line progress indicator, maybe implemented in prodash (similar to what `git` does when receiving)
* **initial release**
Expand Down

0 comments on commit a4d978c

Please sign in to comment.