Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into help-short-alias
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Nov 12, 2023
2 parents e87d197 + 7c4d1e6 commit 4e988af
Show file tree
Hide file tree
Showing 15 changed files with 1,968 additions and 185 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- uses: actions/checkout@v2

- name: Run clippy
run: cargo clippy --version && cargo clippy --tests --workspace -- -D warnings
run: cargo clippy --version && cargo clippy --tests --workspace -- -D warnings -A clippy::needless-raw-string-hashes

docs:
name: docs
Expand Down
10 changes: 7 additions & 3 deletions argh/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "argh"
version = "0.1.9"
version = "0.1.12"
authors = ["Taylor Cramer <[email protected]>", "Benjamin Brittain <[email protected]>", "Erick Tryzelaar <[email protected]>"]
edition = "2018"
keywords = ["args", "arguments", "derive", "cli"]
Expand All @@ -10,9 +10,13 @@ repository = "https://github.com/google/argh"
readme = "README.md"

[dependencies]
argh_shared = { version = "0.1.9", path = "../argh_shared" }
argh_derive = { version = "0.1.9", path = "../argh_derive" }
argh_shared = { version = "0.1.12", path = "../argh_shared" }
argh_derive = { version = "0.1.12", path = "../argh_derive" }

[dev-dependencies]
once_cell = "1.10.0"
trybuild = "1.0.63"

[features]
default = ["help"]
help = ["argh_derive/help"]
55 changes: 52 additions & 3 deletions argh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,30 @@

use std::str::FromStr;

pub use argh_derive::FromArgs;
pub use argh_derive::{ArgsInfo, FromArgs};

/// Information about a particular command used for output.
pub type CommandInfo = argh_shared::CommandInfo<'static>;

/// Information about the command including the options and arguments.
pub type CommandInfoWithArgs = argh_shared::CommandInfoWithArgs<'static>;

/// Information about a subcommand.
pub type SubCommandInfo = argh_shared::SubCommandInfo<'static>;

pub use argh_shared::{ErrorCodeInfo, FlagInfo, FlagInfoKind, Optionality, PositionalInfo};

/// Structured information about the command line arguments.
pub trait ArgsInfo {
/// Returns the argument info.
fn get_args_info() -> CommandInfoWithArgs;

/// Returns the list of subcommands
fn get_subcommands() -> Vec<SubCommandInfo> {
Self::get_args_info().commands
}
}

/// Types which can be constructed from a set of commandline arguments.
pub trait FromArgs: Sized {
/// Construct the type from an input set of arguments.
Expand Down Expand Up @@ -670,7 +689,19 @@ fn cmd<'a>(default: &'a str, path: &'a str) -> &'a str {
/// was unsuccessful or if information like `--help` was requested. Error messages will be printed
/// to stderr, and `--help` output to stdout.
pub fn from_env<T: TopLevelCommand>() -> T {
let strings: Vec<String> = std::env::args().collect();
let strings: Vec<String> = std::env::args_os()
.map(|s| s.into_string())
.collect::<Result<Vec<_>, _>>()
.unwrap_or_else(|arg| {
eprintln!("Invalid utf8: {}", arg.to_string_lossy());
std::process::exit(1)
});

if strings.is_empty() {
eprintln!("No program name, argv is empty");
std::process::exit(1)
}

let cmd = cmd(&strings[0], &strings[0]);
let strs: Vec<&str> = strings.iter().map(|s| s.as_str()).collect();
T::from_args(&[cmd], &strs[1..]).unwrap_or_else(|early_exit| {
Expand All @@ -690,7 +721,7 @@ pub fn from_env<T: TopLevelCommand>() -> T {
/// Create a `FromArgs` type from the current process's `env::args`.
///
/// This special cases usages where argh is being used in an environment where cargo is
/// driving the build. We skip the second env variable.
/// driving the build. We skip the second env argument.
///
/// This function will exit early from the current process if argument parsing
/// was unsuccessful or if information like `--help` was requested. Error messages will be printed
Expand Down Expand Up @@ -803,6 +834,14 @@ impl<T> ParseValueSlot for ParseValueSlotTy<Vec<T>, T> {
}
}

// `ParseValueSlotTy<Option<Vec<T>>, T>` is used as the slot for optional repeating arguments.
impl<T> ParseValueSlot for ParseValueSlotTy<Option<Vec<T>>, T> {
fn fill_slot(&mut self, arg: &str, value: &str) -> Result<(), String> {
self.slot.get_or_insert_with(Vec::new).push((self.parse_func)(arg, value)?);
Ok(())
}
}

/// A type which can be the receiver of a `Flag`.
pub trait Flag {
/// Creates a default instance of the flag value;
Expand All @@ -823,6 +862,16 @@ impl Flag for bool {
}
}

impl Flag for Option<bool> {
fn default() -> Self {
None
}

fn set_flag(&mut self) {
*self = Some(true);
}
}

macro_rules! impl_flag_for_integers {
($($ty:ty,)*) => {
$(
Expand Down
Loading

0 comments on commit 4e988af

Please sign in to comment.