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)
- Always respect `ColorChoice::Never`, even if that means we skip colors in some cases
- `ArgMatches` panics on unknown arguments
- Gracefully handle empty `authors` field in `Cargo.toml` with `app_from_crate`
**From structopt 0.3.25**
- 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))
- Gracefully handle empty `authors` field in `Cargo.toml`
On top of the clap 2 changes

View file

@ -483,17 +483,19 @@ impl Attrs {
}
About(ident, about) => {
let method = Method::from_lit_or_env(ident, about, "CARGO_PKG_DESCRIPTION");
self.methods.push(method);
if let Some(method) =
Method::from_lit_or_env(ident, about, "CARGO_PKG_DESCRIPTION")
{
self.methods.push(method);
}
}
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) => {
self.version =
Some(Method::from_lit_or_env(ident, version, "CARGO_PKG_VERSION"));
self.version = Method::from_lit_or_env(ident, version, "CARGO_PKG_VERSION");
}
NameLitStr(name, lit) => {
@ -675,12 +677,17 @@ impl Method {
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 {
Some(lit) => lit,
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(_) => {
abort!(ident,
"cannot derive `{}` from Cargo.toml", ident;
@ -696,7 +703,7 @@ impl Method {
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
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:

View file

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

View file

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

View file

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

View file

@ -27,8 +27,6 @@ attributes. **This requires enabling the `derive` feature flag.**
$ 01_quick_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
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
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -118,8 +114,6 @@ all subcommands (`app.global_setting()`).
$ 02_app_settings_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -146,8 +140,6 @@ Flags are switches that can be on/off:
$ 03_01_flag_bool_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -178,8 +170,6 @@ Or counted.
$ 03_01_flag_count_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -206,8 +196,6 @@ Flags can also accept a value.
$ 03_02_option_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -240,8 +228,6 @@ Or you can have users specify values by their position on the command-line:
$ 03_03_positional_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -271,8 +257,6 @@ $ 03_04_subcommands_derive
? failed
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -288,8 +272,6 @@ SUBCOMMANDS:
$ 03_04_subcommands_derive help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -339,8 +321,6 @@ set `Arg::default_value`.
$ 03_05_default_values_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -374,8 +354,6 @@ of the mistake, and what the possible valid values are
$ 04_01_enum_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -411,8 +389,6 @@ More generally, you can validate and parse into any data type.
$ 04_02_validate_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:
@ -450,8 +426,6 @@ each other.
$ 04_03_relations_derive --help
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
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
clap [..]
A simple to use, efficient, and full-featured Command Line Argument Parser
USAGE:

View file

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

View file

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