Skip to content

Commit

Permalink
Pre release tweaks and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
CreepySkeleton committed Feb 4, 2020
1 parent 3b11f9a commit 5aafe9e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 9 deletions.
4 changes: 2 additions & 2 deletions clap_derive/src/derives/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
// MIT/Apache 2.0 license.

use super::{parse::*, spanned::Sp, ty::Ty, doc_comments::process_doc_comment};
use super::{doc_comments::process_doc_comment, parse::*, spanned::Sp, ty::Ty};

use std::env;

use heck::{CamelCase, KebabCase, MixedCase, ShoutySnakeCase, SnakeCase};
use proc_macro2::{self, Span, TokenStream};
use proc_macro_error::abort;
use quote::{quote, quote_spanned, ToTokens};
use syn::{self, ext::IdentExt, spanned::Spanned, *};
use syn::{self, ext::IdentExt, spanned::Spanned, Ident, LitStr, MetaNameValue};

/// Default casing style for generated arguments.
pub const DEFAULT_CASING: CasingStyle = CasingStyle::Kebab;
Expand Down
2 changes: 1 addition & 1 deletion clap_derive/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
pub mod arg_enum;
pub mod attrs;
mod clap;
mod doc_comments;
mod from_argmatches;
mod into_app;
mod doc_comments;
pub mod parse;
pub mod spanned;
pub mod ty;
Expand Down
40 changes: 35 additions & 5 deletions clap_derive/src/derives/parse.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -230,19 +230,49 @@ impl Parse for ParserSpec {
}
}

struct CommaSeparated<T>(Punctuated<T, Token![,]>);

impl<T: Parse> Parse for CommaSeparated<T> {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
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<Expr>), 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<T: ToTokens>(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::<Vec<_>>()
.join(", ");

format!("({})", val)
};

format!(
"if you need to call `clap::Arg/App::{}` method you \
can do it like this: #[clap({}({}))]",
name, name, val
can do it like this: #[clap({}{})]",
name, name, suggestion
)
} else {
"if you need to call some method from `clap::Arg/App` \
Expand Down
1 change: 1 addition & 0 deletions clap_derive/tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn issue_151() {
assert!(Cli::try_parse_from(&["test", "--foo"]).is_ok());
assert!(Cli::try_parse_from(&["test", "--bar"]).is_ok());
assert!(Cli::try_parse_from(&["test", "--zebra"]).is_err());
assert!(Cli::try_parse_from(&["test", "--foo", "--bar"]).is_ok());
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions clap_derive/tests/ui/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ struct Opt {
s: String,
}

#[derive(Clap, Debug)]
struct Opt2 {
#[clap(raw(requires_if = r#""one", "two""#))]
s: String,
}
fn main() {
let opt = Opt::parse();
println!("{:?}", opt);
Expand Down
18 changes: 17 additions & 1 deletion clap_derive/tests/ui/raw.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
error: `#[clap(raw(...))` attributes are removed, 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: #[clap(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:12
|
13 | #[clap(raw(case_insensitive = "true"))]
| ^^^

error: `#[clap(raw(...))` attributes are removed, 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:12
|
19 | #[clap(raw(requires_if = r#""one", "two""#))]
| ^^^

error[E0277]: the trait bound `Opt: std::convert::From<clap::parse::matches::arg_matches::ArgMatches>` is not satisfied
--> $DIR/raw.rs:11:10
|
11 | #[derive(Clap, Debug)]
| ^^^^ the trait `std::convert::From<clap::parse::matches::arg_matches::ArgMatches>` is not implemented for `Opt`

error[E0277]: the trait bound `Opt2: std::convert::From<clap::parse::matches::arg_matches::ArgMatches>` is not satisfied
--> $DIR/raw.rs:17:10
|
17 | #[derive(Clap, Debug)]
| ^^^^ the trait `std::convert::From<clap::parse::matches::arg_matches::ArgMatches>` is not implemented for `Opt2`

0 comments on commit 5aafe9e

Please sign in to comment.