Skip to content

Commit

Permalink
Add extra::getopts::short_usage
Browse files Browse the repository at this point in the history
This complements `usage` by auto-generating a short one-liner summary
of the options.
  • Loading branch information
yuriks committed Feb 2, 2014
1 parent 8d7bd49 commit 65a6c7c
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/libextra/getopts.rs
Expand Up @@ -675,6 +675,51 @@ pub mod groups {
::getopts::getopts(args, opts.map(|x| x.long_to_short()))
}

fn format_option(opt: &OptGroup) -> ~str {
let mut line = ~"";

if opt.occur != Req {
line.push_char('[');
}

// Use short_name is possible, but fallback to long_name.
if opt.short_name.len() > 0 {
line.push_char('-');
line.push_str(opt.short_name);
} else {
line.push_str("--");
line.push_str(opt.long_name);
}

if opt.hasarg != No {
line.push_char(' ');
if opt.hasarg == Maybe {
line.push_char('[');
}
line.push_str(opt.hint);
if opt.hasarg == Maybe {
line.push_char(']');
}
}

if opt.occur != Req {
line.push_char(']');
}
if opt.occur == Multi {
line.push_str("..");
}

line
}

/// Derive a short one-line usage summary from a set of long options.
pub fn short_usage(program_name: &str, opts: &[OptGroup]) -> ~str {
let mut line = ~"Usage: " + program_name + " ";
line.push_str(opts.iter().map(format_option).to_owned_vec().connect(" "));
line
}
/// Derive a usage message from a set of long options.
pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
Expand Down Expand Up @@ -1637,4 +1682,23 @@ Options:
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
}

#[test]
fn test_short_usage() {
let optgroups = ~[
groups::reqopt("b", "banana", "Desc", "VAL"),
groups::optopt("a", "012345678901234567890123456789",
"Desc", "VAL"),
groups::optflag("k", "kiwi", "Desc"),
groups::optflagopt("p", "", "Desc", "VAL"),
groups::optmulti("l", "", "Desc", "VAL"),
];

let expected = ~"Usage: fruits -b VAL [-a VAL] [-k] [-p [VAL]] [-l VAL]..";
let generated_usage = groups::short_usage("fruits", optgroups);

debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", generated_usage);
assert_eq!(generated_usage, expected);
}
}

9 comments on commit 65a6c7c

@bors
Copy link
Contributor

@bors bors commented on 65a6c7c Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at yuriks@65a6c7c

@bors
Copy link
Contributor

@bors bors commented on 65a6c7c Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging yuriks/rust/getopts-tweaks = 65a6c7c into auto

@bors
Copy link
Contributor

@bors bors commented on 65a6c7c Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yuriks/rust/getopts-tweaks = 65a6c7c merged ok, testing candidate = 57550ed

@bors
Copy link
Contributor

@bors bors commented on 65a6c7c Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 65a6c7c Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at yuriks@65a6c7c

@bors
Copy link
Contributor

@bors bors commented on 65a6c7c Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging yuriks/rust/getopts-tweaks = 65a6c7c into auto

@bors
Copy link
Contributor

@bors bors commented on 65a6c7c Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yuriks/rust/getopts-tweaks = 65a6c7c merged ok, testing candidate = d8c4e78

@bors
Copy link
Contributor

@bors bors commented on 65a6c7c Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 65a6c7c Feb 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d8c4e78

Please sign in to comment.