Skip to content

Commit

Permalink
Get rid of #[clap(no_version)]
Browse files Browse the repository at this point in the history
  • Loading branch information
CreepySkeleton committed Feb 5, 2020
1 parent 509ac33 commit 9ac1329
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 46 deletions.
4 changes: 4 additions & 0 deletions clap_derive/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ How to use aliases
### [`true` or `false`](true_or_false.rs)

How to express "`"true"` or `"false"` argument.

### [Author, description, and version from `Cargo.toml`](version_from_crate.rs)

//! How to derive a author, description, and version from Cargo.toml
16 changes: 0 additions & 16 deletions clap_derive/examples/no_version.rs

This file was deleted.

15 changes: 15 additions & 0 deletions clap_derive/examples/version_from_crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! How to derive a author, description, and version from Cargo.toml

use clap::Clap;

#[derive(Clap, Debug)]
#[clap(author, about, version)]
// ^^^^^^ <- derive author from Cargo.toml
// ^^^^^ <- derive description from Cargo.toml
// ^^^^^^^ <- derive version from Cargo.toml
struct Opt {}

fn main() {
let opt = Opt::parse();
println!("{:?}", opt);
}
16 changes: 2 additions & 14 deletions clap_derive/src/derives/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub struct Attrs {
author: Option<Method>,
about: Option<Method>,
version: Option<Method>,
no_version: Option<Ident>,
verbatim_doc_comment: Option<Ident>,
has_custom_parser: bool,
kind: Sp<Kind>,
Expand Down Expand Up @@ -260,7 +259,6 @@ impl Attrs {
about: None,
author: None,
version: None,
no_version: None,
verbatim_doc_comment: None,

has_custom_parser: false,
Expand Down Expand Up @@ -307,8 +305,6 @@ impl Attrs {
self.set_kind(kind);
}

NoVersion(ident) => self.no_version = Some(ident),

VerbatimDocComment(ident) => self.verbatim_doc_comment = Some(ident),

DefaultValue(ident, lit) => {
Expand Down Expand Up @@ -351,7 +347,7 @@ impl Attrs {
}

Version(ident, version) => {
self.push_method(ident, version);
self.version = Method::from_lit_or_env(ident, version, "CARGO_PKG_VERSION");
}

NameLitStr(name, lit) => {
Expand Down Expand Up @@ -596,15 +592,7 @@ impl Attrs {
}

pub fn version(&self) -> TokenStream {
match (&self.no_version, &self.version) {
(None, Some(m)) => m.to_token_stream(),

(None, None) => std::env::var("CARGO_PKG_VERSION")
.map(|version| quote!( .version(#version) ))
.unwrap_or_default(),

_ => quote!(),
}
self.version.clone().map(|m| m.to_token_stream()).unwrap_or_default()
}

pub fn cased_name(&self) -> TokenStream {
Expand Down
15 changes: 4 additions & 11 deletions clap_derive/src/derives/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ pub enum ClapAttr {
Env(Ident),
Flatten(Ident),
Subcommand(Ident),
NoVersion(Ident),
VerbatimDocComment(Ident),

// ident [= "string literal"]
About(Ident, Option<LitStr>),
Author(Ident, Option<LitStr>),
Version(Ident, Option<LitStr>),
DefaultValue(Ident, Option<LitStr>),

// ident = "string literal"
Version(Ident, LitStr),
RenameAllEnv(Ident, LitStr),
RenameAll(Ident, LitStr),
NameLitStr(Ident, LitStr),
Expand Down Expand Up @@ -78,7 +77,7 @@ impl Parse for ClapAttr {

"version" => {
check_empty_lit("version");
Ok(Version(name, lit))
Ok(Version(name, Some(lit)))
}

"author" => {
Expand Down Expand Up @@ -170,22 +169,16 @@ impl Parse for ClapAttr {
"env" => Ok(Env(name)),
"flatten" => Ok(Flatten(name)),
"subcommand" => Ok(Subcommand(name)),
"no_version" => Ok(NoVersion(name)),

"verbatim_doc_comment" => Ok(VerbatimDocComment(name)),

"default_value" => Ok(DefaultValue(name, None)),
"about" => (Ok(About(name, None))),
"author" => (Ok(Author(name, None))),
"version" => Ok(Version(name, None)),

"skip" => Ok(Skip(name, None)),

"version" => abort!(
name.span(),
"#[clap(version)] is invalid attribute, \
clap_derive inherits version from Cargo.toml by default, \
no attribute needed"
),

_ => abort!(name.span(), "unexpected attribute: {}", name_str),
}
}
Expand Down
6 changes: 3 additions & 3 deletions clap_derive/tests/author_version_about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use utils::*;
#[test]
fn no_author_version_about() {
#[derive(Clap, PartialEq, Debug)]
#[clap(name = "foo", no_version)]
#[clap(name = "foo")]
struct Opt {}

let output = get_long_help::<Opt>();
Expand All @@ -30,7 +30,7 @@ fn no_author_version_about() {
#[test]
fn use_env() {
#[derive(Clap, PartialEq, Debug)]
#[clap(author, about)]
#[clap(author, about, version)]
struct Opt {}

let output = get_long_help::<Opt>();
Expand All @@ -40,7 +40,7 @@ fn use_env() {
}

#[test]
fn explicit_version_not_str() {
fn explicit_version_not_str_lit() {
const VERSION: &str = "custom version";

#[derive(Clap)]
Expand Down
2 changes: 1 addition & 1 deletion clap_derive/tests/doc-comments-help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn empty_line_in_doc_comment_is_double_linefeed() {
///
/// Bar
#[derive(Clap, PartialEq, Debug)]
#[clap(name = "lorem-ipsum", no_version)]
#[clap(name = "lorem-ipsum")]
struct LoremIpsum {}

let help = get_long_help::<LoremIpsum>();
Expand Down
2 changes: 1 addition & 1 deletion clap_derive/tests/raw_bool_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use clap::Clap;
#[test]
fn raw_bool_literal() {
#[derive(Clap, Debug, PartialEq)]
#[clap(no_version, name = "raw_bool")]
#[clap(name = "raw_bool")]
struct Opt {
#[clap(raw(false))]
a: String,
Expand Down

0 comments on commit 9ac1329

Please sign in to comment.