Skip to content

Commit 24a9c77

Browse files
authored
Merge pull request #83 from jridgewell/usage_format
Update usage format for optional arguments
2 parents 4a7bb77 + 9319518 commit 24a9c77

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
@@ -540,10 +540,6 @@ impl Options {
540540
row.push_str(&short_name);
541541
if long_name.width() > 0 {
542542
row.push_str(", ");
543-
} else {
544-
// Only a single space here, so that any
545-
// argument is printed in the correct spot.
546-
row.push(' ');
547543
}
548544
}
549545
// FIXME: refer issue #7.
@@ -556,16 +552,21 @@ impl Options {
556552
_ => {
557553
row.push_str(if self.long_only { "-" } else { "--" });
558554
row.push_str(&long_name);
559-
row.push(' ');
560555
}
561556
}
562557

563558
// arg
564559
match hasarg {
565560
No => {}
566-
Yes => row.push_str(&hint),
561+
Yes => {
562+
row.push(' ');
563+
row.push_str(&hint);
564+
}
567565
Maybe => {
568566
row.push('[');
567+
if !long_name.is_empty() {
568+
row.push('=');
569+
}
569570
row.push_str(&hint);
570571
row.push(']');
571572
}
@@ -968,9 +969,13 @@ fn format_option(opt: &OptGroup) -> String {
968969
}
969970

970971
if opt.hasarg != No {
971-
line.push(' ');
972972
if opt.hasarg == Maybe {
973973
line.push('[');
974+
if opt.short_name.is_empty() {
975+
line.push('=');
976+
}
977+
} else {
978+
line.push(' ');
974979
}
975980
line.push_str(&opt.hint);
976981
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)