1676: Get rid of `#[clap(no_version)]` r=pksunkara a=CreepySkeleton

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".

Co-authored-by: CreepySkeleton <creepy-skeleton@yandex.ru>
This commit is contained in:
bors[bot] 2020-02-05 14:44:45 +00:00 committed by GitHub
commit f0929d3596
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 33 additions and 50 deletions

View file

@ -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

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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 {

View file

@ -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),
}
}

View file

@ -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)]

View file

@ -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>();

View file

@ -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,