Skip to content

Commit 9319518

Browse files
committed
Update usage format for optional arguments
Fixes #67.
1 parent f457f9a commit 9319518

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,6 @@ impl Options {
551551
row.push_str(&short_name);
552552
if long_name.width() > 0 {
553553
row.push_str(", ");
554-
} else {
555-
// Only a single space here, so that any
556-
// argument is printed in the correct spot.
557-
row.push(' ');
558554
}
559555
}
560556
// FIXME: refer issue #7.
@@ -567,16 +563,21 @@ impl Options {
567563
_ => {
568564
row.push_str(if self.long_only { "-" } else { "--" });
569565
row.push_str(&long_name);
570-
row.push(' ');
571566
}
572567
}
573568

574569
// arg
575570
match hasarg {
576571
No => {}
577-
Yes => row.push_str(&hint),
572+
Yes => {
573+
row.push(' ');
574+
row.push_str(&hint);
575+
}
578576
Maybe => {
579577
row.push('[');
578+
if !long_name.is_empty() {
579+
row.push('=');
580+
}
580581
row.push_str(&hint);
581582
row.push(']');
582583
}
@@ -979,9 +980,13 @@ fn format_option(opt: &OptGroup) -> String {
979980
}
980981

981982
if opt.hasarg != No {
982-
line.push(' ');
983983
if opt.hasarg == Maybe {
984984
line.push('[');
985+
if opt.short_name.is_empty() {
986+
line.push('=');
987+
}
988+
} else {
989+
line.push(' ');
985990
}
986991
line.push_str(&opt.hint);
987992
if opt.hasarg == Maybe {

src/tests/mod.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,8 @@ fn test_usage() {
763763
opts.optopt("a", "012345678901234567890123456789", "Desc", "VAL");
764764
opts.optflag("k", "kiwi", "Desc");
765765
opts.optflagopt("p", "", "Desc", "VAL");
766+
opts.optflagopt("", "pea", "Desc", "VAL");
767+
opts.optflagopt("c", "cherry", "Desc", "VAL");
766768
opts.optmulti("l", "", "Desc", "VAL");
767769
opts.optflag("", "starfruit", "Starfruit");
768770

@@ -773,7 +775,9 @@ Options:
773775
-a, --012345678901234567890123456789 VAL
774776
Desc
775777
-k, --kiwi Desc
776-
-p [VAL] Desc
778+
-p[VAL] Desc
779+
--pea[=VAL] Desc
780+
-c, --cherry[=VAL] Desc
777781
-l VAL Desc
778782
--starfruit Starfruit
779783
";
@@ -820,7 +824,7 @@ Options:
820824

821825
debug!("expected: <<{}>>", expected);
822826
debug!("generated: <<{}>>", usage);
823-
assert!(usage == expected)
827+
assert_eq!(usage, expected)
824828
}
825829

826830
#[test]
@@ -851,7 +855,7 @@ Options:
851855

852856
debug!("expected: <<{}>>", expected);
853857
debug!("generated: <<{}>>", usage);
854-
assert!(usage == expected)
858+
assert_eq!(usage, expected)
855859
}
856860

857861
#[test]
@@ -882,7 +886,7 @@ Options:
882886

883887
debug!("expected: <<{}>>", expected);
884888
debug!("generated: <<{}>>", usage);
885-
assert!(usage == expected)
889+
assert_eq!(usage, expected)
886890
}
887891

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

920924
debug!("expected: <<{}>>", expected);
921925
debug!("generated: <<{}>>", usage);
922-
assert!(usage == expected)
926+
assert_eq!(usage, expected)
923927
}
924928

925929
#[test]
@@ -934,13 +938,13 @@ fn test_usage_short_only() {
934938
Options:
935939
-k VAL Kiwi
936940
-s Starfruit
937-
-a [TYPE] Apple
941+
-a[TYPE] Apple
938942
";
939943

940944
let usage = opts.usage("Usage: fruits");
941945
debug!("expected: <<{}>>", expected);
942946
debug!("generated: <<{}>>", usage);
943-
assert!(usage == expected)
947+
assert_eq!(usage, expected)
944948
}
945949

946950
#[test]
@@ -955,13 +959,13 @@ fn test_usage_long_only() {
955959
Options:
956960
--kiwi VAL Kiwi
957961
--starfruit Starfruit
958-
--apple [TYPE] Apple
962+
--apple[=TYPE] Apple
959963
";
960964

961965
let usage = opts.usage("Usage: fruits");
962966
debug!("expected: <<{}>>", expected);
963967
debug!("generated: <<{}>>", usage);
964-
assert!(usage == expected)
968+
assert_eq!(usage, expected)
965969
}
966970

967971
#[test]
@@ -971,9 +975,10 @@ fn test_short_usage() {
971975
opts.optopt("a", "012345678901234567890123456789", "Desc", "VAL");
972976
opts.optflag("k", "kiwi", "Desc");
973977
opts.optflagopt("p", "", "Desc", "VAL");
974-
opts.optmulti("l", "", "Desc", "VAL");
978+
opts.optflagopt("", "pea", "Desc", "VAL");
979+
opts.optflagopt("c", "cherry", "Desc", "VAL");
975980

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

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

10271032
debug!("expected: <<{}>>", expected);
10281033
debug!("generated: <<{}>>", usage);
1029-
assert!(usage == expected)
1034+
assert_eq!(usage, expected)
10301035
}
10311036

10321037
#[test]

0 commit comments

Comments
 (0)