fix: Gracefully handle empty authors

This commit is contained in:
Ed Page 2021-12-06 11:29:07 -06:00
parent f517c0ede1
commit b2836c07a7
9 changed files with 47 additions and 86 deletions

View file

@ -267,11 +267,13 @@ On top of the clap 2 changes
- `IgnoreCase` is now unicode aware (requires `unicode` feature flag) - `IgnoreCase` is now unicode aware (requires `unicode` feature flag)
- Always respect `ColorChoice::Never`, even if that means we skip colors in some cases - Always respect `ColorChoice::Never`, even if that means we skip colors in some cases
- `ArgMatches` panics on unknown arguments - `ArgMatches` panics on unknown arguments
- Gracefully handle empty `authors` field in `Cargo.toml` with `app_from_crate`
**From structopt 0.3.25** **From structopt 0.3.25**
- Support `SubcommandsNegateReqs` by allowing required `Option<_>`s ([clap-rs/clap#2255](https://github.com/clap-rs/clap/issues/2255)) - Support `SubcommandsNegateReqs` by allowing required `Option<_>`s ([clap-rs/clap#2255](https://github.com/clap-rs/clap/issues/2255))
- Infer `AllowInvalidUtf8` based on parser ([clap-rs/clap#751](https://github.com/clap-rs/clap/issues/2255)) - Infer `AllowInvalidUtf8` based on parser ([clap-rs/clap#751](https://github.com/clap-rs/clap/issues/2255))
- Gracefully handle empty `authors` field in `Cargo.toml`
On top of the clap 2 changes On top of the clap 2 changes

View file

@ -483,17 +483,19 @@ impl Attrs {
} }
About(ident, about) => { About(ident, about) => {
let method = Method::from_lit_or_env(ident, about, "CARGO_PKG_DESCRIPTION"); if let Some(method) =
self.methods.push(method); Method::from_lit_or_env(ident, about, "CARGO_PKG_DESCRIPTION")
{
self.methods.push(method);
}
} }
Author(ident, author) => { Author(ident, author) => {
self.author = Some(Method::from_lit_or_env(ident, author, "CARGO_PKG_AUTHORS")); self.author = Method::from_lit_or_env(ident, author, "CARGO_PKG_AUTHORS");
} }
Version(ident, version) => { Version(ident, version) => {
self.version = self.version = Method::from_lit_or_env(ident, version, "CARGO_PKG_VERSION");
Some(Method::from_lit_or_env(ident, version, "CARGO_PKG_VERSION"));
} }
NameLitStr(name, lit) => { NameLitStr(name, lit) => {
@ -675,12 +677,17 @@ impl Method {
Method { name, args } Method { name, args }
} }
fn from_lit_or_env(ident: Ident, lit: Option<LitStr>, env_var: &str) -> Self { fn from_lit_or_env(ident: Ident, lit: Option<LitStr>, env_var: &str) -> Option<Self> {
let mut lit = match lit { let mut lit = match lit {
Some(lit) => lit, Some(lit) => lit,
None => match env::var(env_var) { None => match env::var(env_var) {
Ok(val) => LitStr::new(&val, ident.span()), Ok(val) => {
if val.is_empty() {
return None;
}
LitStr::new(&val, ident.span())
}
Err(_) => { Err(_) => {
abort!(ident, abort!(ident,
"cannot derive `{}` from Cargo.toml", ident; "cannot derive `{}` from Cargo.toml", ident;
@ -696,7 +703,7 @@ impl Method {
lit = LitStr::new(&edited, lit.span()); lit = LitStr::new(&edited, lit.span());
} }
Method::new(ident, quote!(#lit)) Some(Method::new(ident, quote!(#lit)))
} }
} }

View file

@ -5,8 +5,6 @@ Used to validate README.md's content
$ demo --help $ demo --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:

View file

@ -7,8 +7,6 @@ Let's see what this looks like in the help:
$ escaped_positional --help $ escaped_positional --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:

View file

@ -7,8 +7,6 @@ Let's see what this looks like in the help:
$ escaped_positional_derive --help $ escaped_positional_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:

View file

@ -26,8 +26,6 @@ You can create an application with several arguments using usage strings.
$ 01_quick --help $ 01_quick --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -93,8 +91,6 @@ file. **This requires the `cargo` feature flag.**
$ 02_crate --help $ 02_crate --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -118,8 +114,6 @@ all subcommands (`app.global_setting()`).
$ 02_app_settings --help $ 02_app_settings --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -146,8 +140,6 @@ Flags are switches that can be on/off:
$ 03_01_flag_bool --help $ 03_01_flag_bool --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -178,8 +170,6 @@ Or counted.
$ 03_01_flag_count --help $ 03_01_flag_count --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -206,8 +196,6 @@ Flags can also accept a value.
$ 03_02_option --help $ 03_02_option --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -240,8 +228,6 @@ Or you can have users specify values by their position on the command-line:
$ 03_03_positional --help $ 03_03_positional --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -271,8 +257,6 @@ $ 03_04_subcommands
? failed ? failed
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -288,8 +272,6 @@ SUBCOMMANDS:
$ 03_04_subcommands help $ 03_04_subcommands help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -339,8 +321,6 @@ set `Arg::default_value`.
$ 03_05_default_values --help $ 03_05_default_values --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -374,8 +354,6 @@ of the mistake, and what the possible valid values are
$ 04_01_possible --help $ 04_01_possible --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -409,8 +387,6 @@ When enabling the `derive` feature, you can use `ArgEnum` to take care of the bo
$ 04_01_enum --help $ 04_01_enum --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -446,8 +422,6 @@ More generally, you can validate and parse into any data type.
$ 04_02_validate --help $ 04_02_validate --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -485,8 +459,6 @@ each other.
$ 04_03_relations --help $ 04_03_relations --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -546,8 +518,6 @@ As a last resort, you can create custom errors with the basics of clap's formatt
$ 04_04_custom --help $ 04_04_custom --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:

View file

@ -27,8 +27,6 @@ attributes. **This requires enabling the `derive` feature flag.**
$ 01_quick_derive --help $ 01_quick_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -93,8 +91,6 @@ You can use `app_from_crate!()` to fill these fields in from your `Cargo.toml` f
$ 02_crate_derive --help $ 02_crate_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -118,8 +114,6 @@ all subcommands (`app.global_setting()`).
$ 02_app_settings_derive --help $ 02_app_settings_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -146,8 +140,6 @@ Flags are switches that can be on/off:
$ 03_01_flag_bool_derive --help $ 03_01_flag_bool_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -178,8 +170,6 @@ Or counted.
$ 03_01_flag_count_derive --help $ 03_01_flag_count_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -206,8 +196,6 @@ Flags can also accept a value.
$ 03_02_option_derive --help $ 03_02_option_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -240,8 +228,6 @@ Or you can have users specify values by their position on the command-line:
$ 03_03_positional_derive --help $ 03_03_positional_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -271,8 +257,6 @@ $ 03_04_subcommands_derive
? failed ? failed
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -288,8 +272,6 @@ SUBCOMMANDS:
$ 03_04_subcommands_derive help $ 03_04_subcommands_derive help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -339,8 +321,6 @@ set `Arg::default_value`.
$ 03_05_default_values_derive --help $ 03_05_default_values_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -374,8 +354,6 @@ of the mistake, and what the possible valid values are
$ 04_01_enum_derive --help $ 04_01_enum_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -411,8 +389,6 @@ More generally, you can validate and parse into any data type.
$ 04_02_validate_derive --help $ 04_02_validate_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -450,8 +426,6 @@ each other.
$ 04_03_relations_derive --help $ 04_03_relations_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:
@ -511,8 +485,6 @@ As a last resort, you can create custom errors with the basics of clap's formatt
$ 04_04_custom_derive --help $ 04_04_custom_derive --help
clap [..] clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE:

View file

@ -301,18 +301,36 @@ macro_rules! crate_name {
#[cfg(feature = "cargo")] #[cfg(feature = "cargo")]
#[macro_export] #[macro_export]
macro_rules! app_from_crate { macro_rules! app_from_crate {
() => { () => {{
$crate::App::new($crate::crate_name!()) let mut app = $crate::App::new($crate::crate_name!()).version($crate::crate_version!());
.version($crate::crate_version!())
.author($crate::crate_authors!(", ")) let author = $crate::crate_authors!(", ");
.about($crate::crate_description!()) if !author.is_empty() {
}; app = app.author(author)
($sep:expr) => { }
$crate::App::new($crate::crate_name!())
.version($crate::crate_version!()) let about = $crate::crate_description!();
.author($crate::crate_authors!($sep)) if !about.is_empty() {
.about($crate::crate_description!()) app = app.about(about)
}; }
app
}};
($sep:expr) => {{
let mut app = $crate::App::new($crate::crate_name!()).version($crate::crate_version!());
let author = $crate::crate_authors!($sep);
if !author.is_empty() {
app = app.author(author)
}
let about = $crate::crate_description!();
if !about.is_empty() {
app = app.about(about)
}
app
}};
} }
#[doc(hidden)] #[doc(hidden)]

View file

@ -4,8 +4,6 @@ use clap::{app_from_crate, ErrorKind};
static EVERYTHING: &str = "clap {{version}} static EVERYTHING: &str = "clap {{version}}
A simple to use, efficient, and full-featured Command Line Argument Parser A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE: USAGE: