From cf407a0a5f6bfc2068948bc022bf8d1e3ae5715e Mon Sep 17 00:00:00 2001 From: CreepySkeleton Date: Sat, 21 Dec 2019 15:20:57 +0300 Subject: [PATCH] Pre release tweaks and changelog --- .travis.yml | 12 +++--- CHANGELOG.md | 70 +++++++++++++++++++++++++++++++++-- src/lib.rs | 17 ++++++++- structopt-derive/src/parse.rs | 40 +++++++++++++++++--- tests/issues.rs | 3 ++ tests/ui/raw.rs | 5 +++ tests/ui/raw.stderr | 12 +++++- 7 files changed, 143 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 57d0cd22..dff3050e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,6 @@ language: rust matrix: include: - - rust: 1.36.0 - - rust: stable - - rust: beta - - rust: nightly - - rust: stable name: check if `cargo fmt --all` is applied before_script: rustup component add rustfmt-preview @@ -17,6 +12,13 @@ matrix: install: npm install -g markdown-link-check script: - markdown-link-check -c link-check-headers.json README.md + - markdown-link-check -c link-check-headers.json CHANGELOG.md - markdown-link-check -c link-check-headers.json examples/README.md + + - rust: 1.36.0 + - rust: stable + - rust: beta + - rust: nightly + script: - cargo test $FEATURES diff --git a/CHANGELOG.md b/CHANGELOG.md index 5507e207..7288dcc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,74 @@ # [Upcoming] -* **Bugfix**: `structopt` used to treat `::path::to::type::Vec` as `Vec` +This is unusually big patch release. It contains a number of bugfixes and +new features, some of them may theoretically be considered breaking. We did our best +to avoid any problems on user's side but, if it wasn't good enough, please +[file an issue ASAP](https://github.com/TeXitoi/structopt/issues). + +### Bugfixes + +* `structopt` used to treat `::path::to::type::Vec` as `Vec` special type. [This was considered erroneous](https://github.com/TeXitoi/structopt/pull/287). (same for `Option` and `bool`). Now only exact `Vec` match is a special type. -* Top level `#structopt[...]` raw methods on inner atructs/enums now work as expected - ([#151](https://github.com/TeXitoi/structopt/issues/151)) ([#289](https://github.com/TeXitoi/structopt/issues/289)). + +* `#[structopt(version = expr)]` where `expr` is not a string literal used to get + overridden by auto generated `.version()` call, + [incorrectly](https://github.com/TeXitoi/structopt/issues/283). Now it doesn't. + +* Fixed bug with top-level `App::*` calls on multiple `struct`s, see + [#289](https://github.com/TeXitoi/structopt/issues/265). + +* Positional `bool` args with no explicit `#[structopt(parse(...))]` annotation are + now prohibited. This couldn't work well anyway, see + [this example](https://github.com/TeXitoi/structopt/blob/master/examples/true_or_false.rs) + for details. + +* Now we've instituted strict priority between doc comments, about, help, and the like. + See [the documentation](https://docs.rs/structopt/0.3/structopt/#help-messages). + + **HUGE THANKS to [`@ssokolow`](https://github.com/ssokolow)** for tidying up our documentation, + teaching me English and explaining why our doc used to suck. I promise I'll make the rest + of the doc up to your standards... sometime later! + +### New features + +* Implement `StructOpt` for `Box` so from now on you can use `Box` + with `flatten` and `subcommand` ([#304](https://github.com/TeXitoi/structopt/issues/304)). + + ```rust + enum Command { + #[structopt(name = "version")] + PrintVersion, + + #[structopt(name = "second")] + DoSomething { + #[structopt(flatten)] + config: Box, + }, + + #[structopt(name = "first")] + DoSomethingElse { + #[structopt(flatten)] + config: Box, + } + } + ``` + +* Introduced `#[structopt(verbatim_doc_comment)]` attribute that keeps line breaks in + doc comments, see + [the documentation](https://docs.rs/structopt/0.3/structopt/#doc-comment-preprocessing-and-structoptverbatim_doc_comment). + +* Introduced `#[structopt(rename_all_env)]` and `#[structopt(env)]` magical methods + so you can derive env var's name from field's name. See + [the documentation](https://docs.rs/structopt/0.3/structopt/#auto-deriving-environment-variables). + +### Improvements + +* Now we have nice README for our examples, + [check it out](https://github.com/TeXitoi/structopt/tree/master/examples)! + +* Some error messages were improved and clarified, thanks for all people involved! + # v0.3.5 (2019-11-22) diff --git a/src/lib.rs b/src/lib.rs index b8250d6a..8e6f6b1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -261,13 +261,13 @@ //! //! - [`rename_all`](#specifying-argument-types): [`rename_all = "kebab"/"snake"/"screaming-snake"/"camel"/"pascal"/"verbatim"]` //! -//! Usable only on top level +//! Usable both on top level and field level. //! //! - [`parse`](#custom-string-parsers): `parse(type [= path::to::parser::fn])` //! //! Usable only on field-level. //! -//! - [`skip`](#skipping-fields): `skip = [expr]` +//! - [`skip`](#skipping-fields): `skip [= expr]` //! //! Usable only on field-level. //! @@ -279,6 +279,19 @@ //! //! Usable only on field-level. //! +//! - [`env`](#environment-variable-fallback): `env [= str_literal]` +//! +//! Usable only on field-level. +//! +//! - [`rename_all_env`](##auto-deriving-environment-variables): [`rename_all_env = "kebab"/"snake"/"screaming-snake"/"camel"/"pascal"/"verbatim"]` +//! +//! Usable both on top level and field level. +//! +//! - [`verbatim_doc_comment`](#doc-comment-preprocessing-and-structoptverbatim_doc_comment): +//! `verbatim_doc_comment` +//! +//! Usable both on top level and field level. +//! //! ## Type magic //! //! One of major things that makes `structopt` so awesome is it's type magic. diff --git a/structopt-derive/src/parse.rs b/structopt-derive/src/parse.rs index 85123f17..a7047429 100644 --- a/structopt-derive/src/parse.rs +++ b/structopt-derive/src/parse.rs @@ -1,7 +1,7 @@ use std::iter::FromIterator; -use proc_macro2::TokenStream; use proc_macro_error::{abort, ResultExt}; +use quote::ToTokens; use syn::{ self, parenthesized, parse::{Parse, ParseBuffer, ParseStream}, @@ -229,19 +229,49 @@ impl Parse for ParserSpec { } } +struct CommaSeparated(Punctuated); + +impl Parse for CommaSeparated { + fn parse(input: ParseStream<'_>) -> syn::Result { + let res = Punctuated::parse_separated_nonempty(input)?; + Ok(CommaSeparated(res)) + } +} + fn raw_method_suggestion(ts: ParseBuffer) -> String { - let do_parse = move || -> Result<(Ident, TokenStream), syn::Error> { + let do_parse = move || -> Result<(Ident, CommaSeparated), syn::Error> { let name = ts.parse()?; let _eq: Token![=] = ts.parse()?; let val: LitStr = ts.parse()?; Ok((name, syn::parse_str(&val.value())?)) }; + + fn to_string(val: &T) -> String { + val.to_token_stream() + .to_string() + .replace(" ", "") + .replace(",", ", ") + } + if let Ok((name, val)) = do_parse() { - let val = val.to_string().replace(" ", "").replace(",", ", "); + let exprs = val.0; + let suggestion = if exprs.len() == 1 { + let val = to_string(&exprs[0]); + format!(" = {}", val) + } else { + let val = exprs + .into_iter() + .map(|expr| to_string(&expr)) + .collect::>() + .join(", "); + + format!("({})", val) + }; + format!( "if you need to call `clap::Arg/App::{}` method you \ - can do it like this: #[structopt({}({}))]", - name, name, val + can do it like this: #[structopt({}{})]", + name, name, suggestion ) } else { "if you need to call some method from `clap::Arg/App` \ diff --git a/tests/issues.rs b/tests/issues.rs index d97abb2c..4d250aea 100644 --- a/tests/issues.rs +++ b/tests/issues.rs @@ -30,6 +30,9 @@ fn issue_151() { assert!(Cli::clap() .get_matches_from_safe(&["test", "--zebra"]) .is_err()); + assert!(Cli::clap() + .get_matches_from_safe(&["test", "--foo", "--bar"]) + .is_ok()); } #[test] diff --git a/tests/ui/raw.rs b/tests/ui/raw.rs index a149a2bd..b94f783d 100644 --- a/tests/ui/raw.rs +++ b/tests/ui/raw.rs @@ -14,6 +14,11 @@ struct Opt { s: String, } +#[derive(StructOpt, Debug)] +struct Opt2 { + #[structopt(raw(requires_if = r#""one", "two""#))] + s: String, +} fn main() { let opt = Opt::from_args(); println!("{:?}", opt); diff --git a/tests/ui/raw.stderr b/tests/ui/raw.stderr index c3b17555..93b5e38d 100644 --- a/tests/ui/raw.stderr +++ b/tests/ui/raw.stderr @@ -1,9 +1,19 @@ error: `#[structopt(raw(...))` attributes are removed in structopt 0.3, they are replaced with raw methods = help: if you meant to call `clap::Arg::raw()` method you should use bool literal, like `raw(true)` or `raw(false)` - = note: if you need to call `clap::Arg/App::case_insensitive` method you can do it like this: #[structopt(case_insensitive(true))] + = note: if you need to call `clap::Arg/App::case_insensitive` method you can do it like this: #[structopt(case_insensitive = true)] --> $DIR/raw.rs:13:17 | 13 | #[structopt(raw(case_insensitive = "true"))] | ^^^ + +error: `#[structopt(raw(...))` attributes are removed in structopt 0.3, they are replaced with raw methods + + = help: if you meant to call `clap::Arg::raw()` method you should use bool literal, like `raw(true)` or `raw(false)` + = note: if you need to call `clap::Arg/App::requires_if` method you can do it like this: #[structopt(requires_if("one", "two"))] + + --> $DIR/raw.rs:19:17 + | +19 | #[structopt(raw(requires_if = r#""one", "two""#))] + | ^^^