mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
parent
236cf584cf
commit
d0b22b53ff
3 changed files with 41 additions and 12 deletions
|
@ -340,27 +340,28 @@ impl Attrs {
|
||||||
VerbatimDocComment(ident) => self.verbatim_doc_comment = Some(ident),
|
VerbatimDocComment(ident) => self.verbatim_doc_comment = Some(ident),
|
||||||
|
|
||||||
DefaultValueT(ident, expr) => {
|
DefaultValueT(ident, expr) => {
|
||||||
|
let ty = if let Some(ty) = self.ty.as_ref() {
|
||||||
|
ty
|
||||||
|
} else {
|
||||||
|
abort!(
|
||||||
|
ident,
|
||||||
|
"#[clap(default_value_t)] (without an argument) can be used \
|
||||||
|
only on field level";
|
||||||
|
|
||||||
|
note = "see \
|
||||||
|
https://docs.rs/structopt/0.3.5/structopt/#magical-methods")
|
||||||
|
};
|
||||||
|
|
||||||
let val = if let Some(expr) = expr {
|
let val = if let Some(expr) = expr {
|
||||||
quote!(#expr)
|
quote!(#expr)
|
||||||
} else {
|
} else {
|
||||||
let ty = if let Some(ty) = self.ty.as_ref() {
|
|
||||||
ty
|
|
||||||
} else {
|
|
||||||
abort!(
|
|
||||||
ident,
|
|
||||||
"#[clap(default_value_t)] (without an argument) can be used \
|
|
||||||
only on field level";
|
|
||||||
|
|
||||||
note = "see \
|
|
||||||
https://docs.rs/structopt/0.3.5/structopt/#magical-methods")
|
|
||||||
};
|
|
||||||
quote!(<#ty as ::std::default::Default>::default())
|
quote!(<#ty as ::std::default::Default>::default())
|
||||||
};
|
};
|
||||||
|
|
||||||
let val = quote_spanned!(ident.span()=> {
|
let val = quote_spanned!(ident.span()=> {
|
||||||
clap::lazy_static::lazy_static! {
|
clap::lazy_static::lazy_static! {
|
||||||
static ref DEFAULT_VALUE: &'static str = {
|
static ref DEFAULT_VALUE: &'static str = {
|
||||||
let val = #val;
|
let val: #ty = #val;
|
||||||
let s = ::std::string::ToString::to_string(&val);
|
let s = ::std::string::ToString::to_string(&val);
|
||||||
::std::boxed::Box::leak(s.into_boxed_str())
|
::std::boxed::Box::leak(s.into_boxed_str())
|
||||||
};
|
};
|
||||||
|
|
21
clap_derive/tests/ui/default_value_t_invalid.rs
Normal file
21
clap_derive/tests/ui/default_value_t_invalid.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
use clap::Clap;
|
||||||
|
|
||||||
|
#[derive(Clap, Debug)]
|
||||||
|
#[clap(name = "basic")]
|
||||||
|
struct Opt {
|
||||||
|
#[clap(default_value_t = -10)]
|
||||||
|
value: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let opt = Opt::parse();
|
||||||
|
println!("{:?}", opt);
|
||||||
|
}
|
7
clap_derive/tests/ui/default_value_t_invalid.stderr
Normal file
7
clap_derive/tests/ui/default_value_t_invalid.stderr
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
error[E0600]: cannot apply unary operator `-` to type `u32`
|
||||||
|
--> $DIR/default_value_t_invalid.rs:14:30
|
||||||
|
|
|
||||||
|
14 | #[clap(default_value_t = -10)]
|
||||||
|
| ^^^ cannot apply unary operator `-`
|
||||||
|
|
|
||||||
|
= note: unsigned values cannot be negated
|
Loading…
Reference in a new issue