@@ -5,7 +5,14 @@ namespace RT.CommandLine.Tests;
55public sealed class CmdLineTests
66{
77#pragma warning disable 0649 // Field is never assigned to, and will always have its default value null
8- private class commandLine
8+ private class CommandLine1
9+ {
10+ [ IsPositional , IsMandatory ]
11+ public string Arg ;
12+ [ Option ( "-o" ) ]
13+ public string Option ;
14+ }
15+ private class CommandLine2
916 {
1017 [ Option ( "--stuff" ) ]
1118 public string Stuff ;
@@ -17,21 +24,57 @@ private class commandLine
1724 [ Fact ]
1825 public static void Test ( )
1926 {
20- var c = CommandLineParser . Parse < commandLine > ( "--stuff blah abc def" . Split ( ' ' ) ) ;
21- Assert . Equal ( "blah" , c . Stuff ) ;
22- Assert . True ( c . Args . SequenceEqual ( new [ ] { "abc" , "def" } ) ) ;
27+ try
28+ {
29+ CommandLineParser . Parse < CommandLine1 > ( [ ] ) ;
30+ Assert . Fail ( ) ;
31+ }
32+ catch ( CommandLineParseException e )
33+ {
34+ Assert . Equal ( "The parameter <Arg> is mandatory and must be specified." , e . Message ) ;
35+ }
36+ try
37+ {
38+ CommandLineParser . Parse < CommandLine1 > ( [ "-o" , "Option" ] ) ;
39+ Assert . Fail ( ) ;
40+ }
41+ catch ( CommandLineParseException e )
42+ {
43+ Assert . Equal ( "The parameter <Arg> is mandatory and must be specified." , e . Message ) ;
44+ }
45+
46+ var c1 = CommandLineParser . Parse < CommandLine1 > ( [ "Arg" ] ) ;
47+ Assert . Equal ( "Arg" , c1 . Arg ) ;
48+ Assert . Null ( c1 . Option ) ;
49+
50+ c1 = CommandLineParser . Parse < CommandLine1 > ( [ "-o" , "Arg1" , "Arg2" ] ) ;
51+ Assert . Equal ( "Arg1" , c1 . Option ) ;
52+ Assert . Equal ( "Arg2" , c1 . Arg ) ;
53+
54+ c1 = CommandLineParser . Parse < CommandLine1 > ( [ "Arg1" , "-o" , "Arg2" ] ) ;
55+ Assert . Equal ( "Arg1" , c1 . Arg ) ;
56+ Assert . Equal ( "Arg2" , c1 . Option ) ;
57+
58+ c1 = CommandLineParser . Parse < CommandLine1 > ( [ "--" , "-o" ] ) ;
59+ Assert . Equal ( "-o" , c1 . Arg ) ;
60+ Assert . Null ( c1 . Option ) ;
61+
62+
63+ var c2 = CommandLineParser . Parse < CommandLine2 > ( "--stuff blah abc def" . Split ( ' ' ) ) ;
64+ Assert . Equal ( "blah" , c2 . Stuff ) ;
65+ Assert . True ( c2 . Args . SequenceEqual ( [ "abc" , "def" ] ) ) ;
2366
24- c = CommandLineParser . Parse < commandLine > ( "def --stuff thingy abc" . Split ( ' ' ) ) ;
25- Assert . Equal ( "thingy" , c . Stuff ) ;
26- Assert . True ( c . Args . SequenceEqual ( new [ ] { "def" , "abc" } ) ) ;
67+ c2 = CommandLineParser . Parse < CommandLine2 > ( "def --stuff thingy abc" . Split ( ' ' ) ) ;
68+ Assert . Equal ( "thingy" , c2 . Stuff ) ;
69+ Assert . True ( c2 . Args . SequenceEqual ( [ "def" , "abc" ] ) ) ;
2770
28- c = CommandLineParser . Parse < commandLine > ( "--stuff stuff -- abc --stuff blah -- def" . Split ( ' ' ) ) ;
29- Assert . Equal ( "stuff" , c . Stuff ) ;
30- Assert . True ( c . Args . SequenceEqual ( new [ ] { "abc" , "--stuff" , "blah" , "--" , "def" } ) ) ;
71+ c2 = CommandLineParser . Parse < CommandLine2 > ( "--stuff stuff -- abc --stuff blah -- def" . Split ( ' ' ) ) ;
72+ Assert . Equal ( "stuff" , c2 . Stuff ) ;
73+ Assert . True ( c2 . Args . SequenceEqual ( [ "abc" , "--stuff" , "blah" , "--" , "def" ] ) ) ;
3174
3275 try
3376 {
34- CommandLineParser . Parse < commandLine > ( "--blah stuff" . Split ( ' ' ) ) ;
77+ CommandLineParser . Parse < CommandLine2 > ( "--blah stuff" . Split ( ' ' ) ) ;
3578 Assert . Fail ( ) ;
3679 }
3780 catch ( CommandLineParseException e )
0 commit comments