Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update usage format for optional arguments #83

Merged
merged 1 commit into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,6 @@ impl Options {
row.push_str(&short_name);
if long_name.width() > 0 {
row.push_str(", ");
} else {
// Only a single space here, so that any
// argument is printed in the correct spot.
row.push(' ');
}
}
// FIXME: refer issue #7.
Expand All @@ -567,16 +563,21 @@ impl Options {
_ => {
row.push_str(if self.long_only { "-" } else { "--" });
row.push_str(&long_name);
row.push(' ');
}
}

// arg
match hasarg {
No => {}
Yes => row.push_str(&hint),
Yes => {
row.push(' ');
row.push_str(&hint);
}
Maybe => {
row.push('[');
if !long_name.is_empty() {
row.push('=');
}
row.push_str(&hint);
row.push(']');
}
Expand Down Expand Up @@ -979,9 +980,13 @@ fn format_option(opt: &OptGroup) -> String {
}

if opt.hasarg != No {
line.push(' ');
if opt.hasarg == Maybe {
line.push('[');
if opt.short_name.is_empty() {
line.push('=');
}
} else {
line.push(' ');
}
line.push_str(&opt.hint);
if opt.hasarg == Maybe {
Expand Down
31 changes: 18 additions & 13 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,8 @@ fn test_usage() {
opts.optopt("a", "012345678901234567890123456789", "Desc", "VAL");
opts.optflag("k", "kiwi", "Desc");
opts.optflagopt("p", "", "Desc", "VAL");
opts.optflagopt("", "pea", "Desc", "VAL");
opts.optflagopt("c", "cherry", "Desc", "VAL");
opts.optmulti("l", "", "Desc", "VAL");
opts.optflag("", "starfruit", "Starfruit");

Expand All @@ -773,7 +775,9 @@ Options:
-a, --012345678901234567890123456789 VAL
Desc
-k, --kiwi Desc
-p [VAL] Desc
-p[VAL] Desc
--pea[=VAL] Desc
-c, --cherry[=VAL] Desc
-l VAL Desc
--starfruit Starfruit
";
Expand Down Expand Up @@ -820,7 +824,7 @@ Options:

debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
assert_eq!(usage, expected)
}

#[test]
Expand Down Expand Up @@ -851,7 +855,7 @@ Options:

debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
assert_eq!(usage, expected)
}

#[test]
Expand Down Expand Up @@ -882,7 +886,7 @@ Options:

debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
assert_eq!(usage, expected)
}

#[test]
Expand All @@ -909,7 +913,7 @@ Options:
-c, --brûlée brûlée quite long description
-k, --kiwi€ kiwi description
-o, --orange‹ orange description
-r, --raspberry-but-making-this-option-way-too-long\u{0020}
-r, --raspberry-but-making-this-option-way-too-long
raspberry description is also quite long indeed longer
than every other piece of text we might encounter here
and thus will be automatically broken up
Expand All @@ -919,7 +923,7 @@ Options:

debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
assert_eq!(usage, expected)
}

#[test]
Expand All @@ -934,13 +938,13 @@ fn test_usage_short_only() {
Options:
-k VAL Kiwi
-s Starfruit
-a [TYPE] Apple
-a[TYPE] Apple
";

let usage = opts.usage("Usage: fruits");
debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
assert_eq!(usage, expected)
}

#[test]
Expand All @@ -955,13 +959,13 @@ fn test_usage_long_only() {
Options:
--kiwi VAL Kiwi
--starfruit Starfruit
--apple [TYPE] Apple
--apple[=TYPE] Apple
";

let usage = opts.usage("Usage: fruits");
debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
assert_eq!(usage, expected)
}

#[test]
Expand All @@ -971,9 +975,10 @@ fn test_short_usage() {
opts.optopt("a", "012345678901234567890123456789", "Desc", "VAL");
opts.optflag("k", "kiwi", "Desc");
opts.optflagopt("p", "", "Desc", "VAL");
opts.optmulti("l", "", "Desc", "VAL");
opts.optflagopt("", "pea", "Desc", "VAL");
opts.optflagopt("c", "cherry", "Desc", "VAL");

let expected = "Usage: fruits -b VAL [-a VAL] [-k] [-p [VAL]] [-l VAL]..".to_string();
let expected = "Usage: fruits -b VAL [-a VAL] [-k] [-p[VAL]] [--pea[=VAL]] [-c[VAL]]".to_string();
let generated_usage = opts.short_usage("fruits");

debug!("expected: <<{}>>", expected);
Expand Down Expand Up @@ -1026,7 +1031,7 @@ Options:

debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
assert!(usage == expected)
assert_eq!(usage, expected)
}

#[test]
Expand Down