Allow flatten with doc comments

This commit is contained in:
CreepySkeleton 2020-08-25 10:53:54 +03:00
parent 317a816857
commit a951958748
5 changed files with 25 additions and 46 deletions

View file

@ -481,12 +481,15 @@ impl Attrs {
"parse attribute is not allowed for flattened entry" "parse attribute is not allowed for flattened entry"
); );
} }
if res.has_explicit_methods() || res.has_doc_methods() { if res.has_explicit_methods() {
abort!( abort!(
res.kind.span(), res.kind.span(),
"methods and doc comments are not allowed for flattened entry" "methods are not allowed for flattened entry"
); );
} }
// ignore doc comments
res.doc_comment = vec![];
} }
Kind::ExternalSubcommand => { Kind::ExternalSubcommand => {
@ -695,14 +698,6 @@ impl Attrs {
.iter() .iter()
.any(|m| m.name != "about" && m.name != "long_about") .any(|m| m.name != "about" && m.name != "long_about")
} }
pub fn has_doc_methods(&self) -> bool {
!self.doc_comment.is_empty()
|| self
.methods
.iter()
.any(|m| m.name == "about" || m.name == "long_about")
}
} }
/// replace all `:` with `, ` when not inside the `<>` /// replace all `:` with `, ` when not inside the `<>`

View file

@ -132,3 +132,22 @@ fn merge_subcommands_with_flatten() {
Opt::parse_from(&["test", "command2", "43"]) Opt::parse_from(&["test", "command2", "43"])
); );
} }
#[test]
fn flatten_with_doc_comment() {
#[derive(Clap, Debug)]
struct DaemonOpts {
#[clap(short)]
user: String,
#[clap(short)]
group: String,
}
#[derive(Clap, Debug)]
#[clap(name = "basic")]
struct Opt {
/// A very important doc comment I just can't leave out!
#[clap(flatten)]
opts: DaemonOpts,
}
}

View file

@ -1,30 +0,0 @@
// 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)]
struct DaemonOpts {
#[clap(short)]
user: String,
#[clap(short)]
group: String,
}
#[derive(Clap, Debug)]
#[clap(name = "basic")]
struct Opt {
/// Opts.
#[clap(flatten)]
opts: DaemonOpts,
}
fn main() {
let opt = Opt::parse();
println!("{:?}", opt);
}

View file

@ -1,5 +0,0 @@
error: methods and doc comments are not allowed for flattened entry
--> $DIR/flatten_and_doc.rs:23:12
|
23 | #[clap(flatten)]
| ^^^^^^^

View file

@ -1,4 +1,4 @@
error: methods and doc comments are not allowed for flattened entry error: methods are not allowed for flattened entry
--> $DIR/flatten_and_methods.rs:22:19 --> $DIR/flatten_and_methods.rs:22:19
| |
22 | #[clap(short, flatten)] 22 | #[clap(short, flatten)]