diff --git a/src/lib.rs b/src/lib.rs index 9a9d5d20..c39403ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. @@ -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(']'); } @@ -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 { diff --git a/src/tests/mod.rs b/src/tests/mod.rs index c61beaa5..85f86194 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -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"); @@ -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 "; @@ -820,7 +824,7 @@ Options: debug!("expected: <<{}>>", expected); debug!("generated: <<{}>>", usage); - assert!(usage == expected) + assert_eq!(usage, expected) } #[test] @@ -851,7 +855,7 @@ Options: debug!("expected: <<{}>>", expected); debug!("generated: <<{}>>", usage); - assert!(usage == expected) + assert_eq!(usage, expected) } #[test] @@ -882,7 +886,7 @@ Options: debug!("expected: <<{}>>", expected); debug!("generated: <<{}>>", usage); - assert!(usage == expected) + assert_eq!(usage, expected) } #[test] @@ -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 @@ -919,7 +923,7 @@ Options: debug!("expected: <<{}>>", expected); debug!("generated: <<{}>>", usage); - assert!(usage == expected) + assert_eq!(usage, expected) } #[test] @@ -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] @@ -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] @@ -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); @@ -1026,7 +1031,7 @@ Options: debug!("expected: <<{}>>", expected); debug!("generated: <<{}>>", usage); - assert!(usage == expected) + assert_eq!(usage, expected) } #[test]