Repository: clap-rs/clap
Pull request #1676 (2020-02-05 11:17:04)

Get rid of `#[clap(no_version)]`
Do what I wanted to do for a long time - get rid of `no_version` and replace it with "no version by default, use `version` to do it explicitly".

CreepySkeleton: Argh! I guess I need a pre-commit hook with cargo-fmt.

Patch:
diff --git a/clap_derive/examples/README.md b/clap_derive/examples/README.md
index 496cf9fb..61264659 100644
--- a/clap_derive/examples/README.md
+++ b/clap_derive/examples/README.md
@@ -57,10 +57,6 @@ How to parse `key=value` pairs.
 
 How to add `no-thing` flag which is `true` by default and `false` if passed.
 
-### [No version](no_version.rs)
-
-How to completely remove version.
-
 ### [Rename all](rename_all.rs)
 
 How `#[clap(rename_all)]` works.
@@ -76,3 +72,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`](from_crate.rs)
+
+//! How to derive a author, description, and version from Cargo.toml
diff --git a/clap_derive/examples/from_crate.rs b/clap_derive/examples/from_crate.rs
new file mode 100644
index 00000000..73e3fc4d
--- /dev/null
+++ b/clap_derive/examples/from_crate.rs
@@ -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);
+}
diff --git a/clap_derive/examples/no_version.rs b/clap_derive/examples/no_version.rs
deleted file mode 100644
index 0b530813..00000000
--- a/clap_derive/examples/no_version.rs
+++ /dev/null
@@ -1,16 +0,0 @@
-//! How to completely remove version.
-
-use clap::{AppSettings, Clap};
-
-#[derive(Clap, Debug)]
-#[clap(
-    name = "no_version",
-    no_version,
-    global_setting = AppSettings::DisableVersion
-)]
-struct Opt {}
-
-fn main() {
-    let opt = Opt::parse();
-    println!("{:?}", opt);
-}
diff --git a/clap_derive/src/derives/attrs.rs b/clap_derive/src/derives/attrs.rs
index 6c6b7aea..4791d09a 100644
--- a/clap_derive/src/derives/attrs.rs
+++ b/clap_derive/src/derives/attrs.rs
@@ -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>,
@@ -260,7 +259,6 @@ impl Attrs {
             about: None,
             author: None,
             version: None,
-            no_version: None,
             verbatim_doc_comment: None,
 
             has_custom_parser: false,
@@ -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) => {
@@ -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) => {
@@ -596,15 +592,10 @@ 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 {
diff --git a/clap_derive/src/derives/parse.rs b/clap_derive/src/derives/parse.rs
index 62caeec8..aab83f34 100644
--- a/clap_derive/src/derives/parse.rs
+++ b/clap_derive/src/derives/parse.rs
@@ -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),
@@ -78,7 +77,7 @@ impl Parse for ClapAttr {
 
                     "version" => {
                         check_empty_lit("version");
-                        Ok(Version(name, lit))
+                        Ok(Version(name, Some(lit)))
                     }
 
                     "author" => {
@@ -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),
             }
         }


Test patch:
diff --git a/clap_derive/tests/author_version_about.rs b/clap_derive/tests/author_version_about.rs
index 1233bebf..051315d4 100644
--- a/clap_derive/tests/author_version_about.rs
+++ b/clap_derive/tests/author_version_about.rs
@@ -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>();
@@ -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>();
@@ -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)]
diff --git a/clap_derive/tests/doc-comments-help.rs b/clap_derive/tests/doc-comments-help.rs
index 93e9f0a6..e0c94473 100644
--- a/clap_derive/tests/doc-comments-help.rs
+++ b/clap_derive/tests/doc-comments-help.rs
@@ -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>();
diff --git a/clap_derive/tests/raw_bool_literal.rs b/clap_derive/tests/raw_bool_literal.rs
index 2daa35c7..7e52a084 100644
--- a/clap_derive/tests/raw_bool_literal.rs
+++ b/clap_derive/tests/raw_bool_literal.rs
@@ -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,