Skip to content

Commit

Permalink
Merge pull request #35 from alexmaco/multiline
Browse files Browse the repository at this point in the history
Allow multiline doc on Options struct
  • Loading branch information
murarth committed Jun 22, 2020
2 parents c4198f0 + 300eb6a commit ba4209b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 9 additions & 2 deletions gumdrop_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,10 @@ impl DefaultOpts {
if let Meta::NameValue(nv) = meta {
let doc = lit_str(&nv.lit)?;

if opts.doc.is_none() {
if let Some(text) = opts.doc.as_mut() {
text.push('\n');
text.push_str(doc.trim_start());
} else {
opts.doc = Some(doc.trim_start().to_owned());
}
}
Expand Down Expand Up @@ -1763,13 +1766,17 @@ fn make_usage(help: &Option<String>, free: &[FreeOpt], opts: &[Opt]) -> String {

if let Some(help) = help {
res.push_str(help);
res.push_str("\n\n");
res.push('\n');
}

let width = max_width(free, |opt| opt.width())
.max(max_width(opts, |opt| opt.width()));

if !free.is_empty() {
if !res.is_empty() {
res.push('\n');
}

res.push_str("Positional arguments:\n");

for opt in free {
Expand Down
20 changes: 20 additions & 0 deletions tests/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,26 @@ Optional arguments:
[1..]);
}

#[test]
fn test_doc_help_multiline() {
/// type-level help comment
/// second line of text
#[derive(Options)]
struct Opts {
/// help comment
foo: i32,
}

assert_eq!(Opts::usage(), &"
type-level help comment
second line of text
Optional arguments:
-f, --foo FOO help comment"
// Skip leading newline
[1..]);
}

#[test]
fn test_failed_parse_free() {
#[derive(Options)]
Expand Down

0 comments on commit ba4209b

Please sign in to comment.