/
Samples.cs
1499 lines (1262 loc) · 30 KB
/
Samples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Reflection;
using CLAP;
using CLAP.Interception;
using CLAP.Validation;
namespace Tests
{
public interface IPrinter
{
void Print(String text);
void Reset();
}
public class Printer : IPrinter
{
public List<String> PrintedTexts = new List<String>();
public void Print(String text)
{
PrintedTexts.Add(text);
}
public void Reset()
{
PrintedTexts.Clear();
}
}
public class BaseSample
{
public IPrinter Printer { get; set; }
}
[TargetAlias("s02")]
public class Sample_02 : BaseSample
{
[Verb(IsDefault = true, Aliases = "p", Description = "Prints a message")]
public void Print(
[Aliases("c")]
Int32 count,
[Aliases("m,msg")]
[DefaultValue("hello")]
String message,
String prefix,
[Aliases("u")]
[DefaultValue(false)]
Boolean upper)
{
for (var i = 0; i < count; i++)
{
var text = prefix + message;
Printer.Print(upper ? text.ToUpper() : text);
}
}
}
[TargetAlias("s02")]
public class Sample_02_Default : BaseSample
{
[Verb(IsDefault = true)]
public void Print([DefaultValue("bar")]string foo)
{
Printer.Print(foo);
}
}
public class Sample_02_No_Default : BaseSample
{
[Verb(IsDefault = true)]
public void Print(string foo)
{
Printer.Print(foo);
}
}
public class ValidationSample_01 : BaseSample
{
[Verb]
public void MoreThan5([MoreThan(5)] Int32 n) { }
[Verb]
public void MoreOrEqualTo10([MoreOrEqualTo(10)] Int32 n) { }
[Verb]
public void LessThan20([LessThan(20)] Int32 n) { }
[Verb]
public void LessOrEqualTo30([LessOrEqualTo(30)] Int32 n) { }
[Verb]
public void RegexMatchesEmail([RegexMatches(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$")] String text) { }
[Verb]
public void FileExists([FileExists] string path) { }
[Verb]
public void UriFileExists([FileExists] Uri path) { }
[Verb]
public void DirectoryExists([DirectoryExists] string path) { }
[Verb]
public void UriDirectoryExists([DirectoryExists] Uri path) { }
[Verb]
public void PathExists([PathExists] string path) { }
[Verb]
public void UriPathExists([PathExists] Uri path) { }
}
[TargetAlias("s03")]
public class Sample_03 : BaseSample
{
[Verb(Aliases = "p", Description = "Prints a message")]
public void Print(
[Aliases("c")]
[MoreThan(5)]
[LessOrEqualTo(10)]
Int32 count,
[Aliases("m.msg"), DefaultValue("hello")]
String message,
String prefix)
{
for (var i = 0; i < count; i++)
{
Printer.Print(prefix + message);
}
}
}
public class Sample_04 : BaseSample
{
[Verb(IsDefault = true, Aliases = "p", Description = "Prints a message")]
public void Print(
Int32 count,
[Aliases("m,msg")]
[DefaultValue("hello")]
String message,
String prefix,
Case c)
{
for (var i = 0; i < count; i++)
{
var text = prefix + message;
switch (c)
{
case Case.Upper:
text = text.ToUpperInvariant();
break;
case Case.Lower:
text = text.ToLowerInvariant();
break;
case Case.Unchanged:
default:
break;
}
Printer.Print(text);
}
}
}
public class Sample_05 : BaseSample
{
[Verb(IsDefault = true, Aliases = "p", Description = "Prints a message")]
public void Print(
[Aliases("c")]
int count,
String c,
String x,
[Aliases("X")]
int number,
String y)
{
}
}
public class Sample_06 : BaseSample
{
[Verb(IsDefault = true, Aliases = "p", Description = "Prints a message")]
public void Print(
Int32 count,
[Aliases("m,msg")]
[DefaultValue("hello")]
String message,
String prefix,
[DefaultValue(Case.Lower)]
Case c)
{
for (var i = 0; i < count; i++)
{
var text = prefix + message;
switch (c)
{
case Case.Upper:
text = text.ToUpperInvariant();
break;
case Case.Lower:
text = text.ToLowerInvariant();
break;
case Case.Unchanged:
default:
break;
}
Printer.Print(text);
}
}
}
public class Sample_07 : BaseSample
{
[Verb]
public void Print(
[DefaultValue(1)]
Int32 count,
String message,
[Required]
String prefix,
Case c)
{
for (var i = 0; i < count; i++)
{
var text = prefix + message;
switch (c)
{
case Case.Upper:
text = text.ToUpperInvariant();
break;
case Case.Lower:
text = text.ToLowerInvariant();
break;
case Case.Unchanged:
default:
break;
}
Printer.Print(text);
}
}
}
public class Sample_08 : BaseSample
{
[Verb]
public void Print(String[] messages, String prefix)
{
if (messages == null)
{
return;
}
foreach (var msg in messages)
{
Printer.Print(prefix + msg);
}
}
[Verb]
public void PrintNumbers(Int32[] numbers, String prefix)
{
if (numbers == null)
{
return;
}
foreach (var msg in numbers)
{
Printer.Print(prefix + msg);
}
}
[Verb]
public void PrintEnums(Case[] enums, String prefix)
{
if (enums == null)
{
return;
}
foreach (var msg in enums)
{
Printer.Print(prefix + msg);
}
}
}
public class Sample_09 : BaseSample
{
[Empty]
public void Foo()
{
Printer.Print("a");
}
}
public class Sample_10 : BaseSample
{
[Verb(IsDefault = true, Description = "prints")]
public void Print()
{
}
[Verb]
[Validate("str like 'foo*'")]
public void Print2([Required, Description("string!")]string str)
{
}
[Global]
[Validate("x not like 'foo*'")]
public void Foo(string x)
{
Printer.Print(x);
}
[Global]
public void Bar()
{
Printer.Print("zoo");
}
[Global(Aliases = "m")]
public void Mish([LessThan(10)] int count)
{
for (int i = 0; i < count; i++)
{
Printer.Print("mesh");
}
}
[Global]
public void Abra1(BindingFlags f)
{
}
[Global]
public void Abra2(int[] nums)
{
}
[Global]
public void Abra3([Separator("-")] string[] strs)
{
}
[Help]
public void ShowHelp(string help)
{
Printer.Print("help");
}
[Global(Description = "cat!")]
public void Cat(Dictionary<string, Sample_10> x)
{
}
[Verb]
public void WithARequiredSwitch(string str, [Required] bool sw, int[] nums, [Separator("-")] string[] strs)
{
}
[Verb]
public void WIthDefaultProvider([DefaultProvider(typeof(MyDefaultProvider))] string blah)
{
}
public class MyDefaultProvider : DefaultProvider
{
public override object GetDefault(VerbExecutionContext context)
{
throw new NotImplementedException();
}
}
}
public class Sample_10_x : BaseSample
{
[Verb(IsDefault = true, Description = "prints")]
public void Print()
{
}
[Verb]
[Validate("str like 'foo*'")]
public void Print2([Required, Description("string!")]string str)
{
}
[Global]
[Validate("x not like 'foo*'")]
public void Foo(string x)
{
Printer.Print(x);
}
[Global]
public void Bar()
{
Printer.Print("zoo");
}
[Global(Aliases = "m")]
public void Mish([LessThan(10)] int count)
{
for (int i = 0; i < count; i++)
{
Printer.Print("mesh");
}
}
[Global]
public void Abra(BindingFlags f)
{
}
[Help]
public void ShowHelp(string help)
{
Printer.Print("help");
}
[Global(Description = "cat!")]
public void Cat(Dictionary<string, Sample_10> x)
{
}
[Verb]
public void WithARequiredSwitch(string str, [Required] bool sw)
{
}
[Verb]
public void WIthDefaultProvider([DefaultProvider(typeof(MyDefaultProvider))] string blah)
{
}
public class MyDefaultProvider : DefaultProvider
{
public override object GetDefault(VerbExecutionContext context)
{
throw new NotImplementedException();
}
}
}
public class Sample_11 : BaseSample
{
public void Print()
{
Printer.Print("x");
}
}
public class Sample_12 : BaseSample
{
[Verb]
public void Print()
{
}
[Global]
public void Foo(int x, int y)
{
}
}
public class Sample_13 : BaseSample
{
[Empty]
public void Foo()
{
}
[Empty]
public void Bar()
{
}
}
public class Sample_14 : BaseSample
{
[Empty]
public static void Foo()
{
}
}
public class Sample_15 : BaseSample
{
[Empty]
public void Foo()
{
}
}
public class Sample_16 : BaseSample
{
public static IPrinter StaticPrinter { get; set; }
[Empty]
public static void Foo()
{
StaticPrinter.Print("a");
}
}
public class Sample_17 : BaseSample
{
[Empty, Help]
public void Foo(string h)
{
Printer.Print("a");
}
}
public class Sample_18 : BaseSample
{
[Empty, Help]
public void Foo(int h)
{
Printer.Print("a");
}
}
public class Sample_19 : BaseSample
{
[Empty, Help]
public void Foo(string h, int n)
{
Printer.Print("a");
}
}
public class Sample_20 : BaseSample
{
[Empty]
public void Foo(string h)
{
}
}
public class Sample_21 : BaseSample
{
[Help(Aliases = "h,?")]
public void Help(string h)
{
Printer.Print("help");
}
}
public class Sample_22 : BaseSample
{
public static IPrinter StaticPrinter { get; set; }
[Help(Aliases = "h,?")]
public static void Help(string h)
{
StaticPrinter.Print("help");
}
}
public class Sample_23 : BaseSample
{
[Help(Aliases = "h,?")]
public void Help(string h)
{
Printer.Print("help");
}
}
public class Sample_24 : BaseSample
{
[Help(Aliases = "h,?")]
public void Help(string h, string boo)
{
Printer.Print("help");
}
}
public class Sample_25 : BaseSample
{
[Help(Aliases = "h,?")]
public void Help(int i)
{
}
[Verb]
public static void Foo()
{
}
}
public class Sample_27 : BaseSample
{
public static IPrinter StaticPrinter { get; set; }
[Verb]
public static void Foo(string x)
{
StaticPrinter.Print(x);
}
}
public class Sample_28 : BaseSample
{
[Verb]
public void Foo(string x, Guid g)
{
Printer.Print(x + g);
}
[Verb]
public void Zoo([DefaultValue("5")] int n)
{
Printer.Print(n.ToString());
}
[Verb]
public void Bar(
string x,
[DefaultValue("{2FBBAAAA-02AF-4F40-BADE-957F566B221E}")]
Guid g)
{
Printer.Print(x + g);
}
[Empty, Help]
public void Help(string help)
{
Printer.Print(help);
}
}
public class Sample_29 : BaseSample
{
[Verb]
public void Foo(string x, Uri u)
{
Printer.Print(x + u);
}
[Verb]
public void Bar(
string x,
[DefaultValue("http://www.com")]
Uri u)
{
Printer.Print(x + u);
}
}
public class Sample_30 : BaseSample
{
[Verb(IsDefault = true)]
public void Foo()
{
Printer.Print("works!");
}
}
public class Sample_31 : BaseSample
{
[Verb(IsDefault = true)]
public void Foo(string x, int y)
{
Printer.Print("works!");
}
}
public class Sample_32 : BaseSample
{
[Error]
public void Error1()
{
}
[Error]
public void Error2()
{
}
}
public class Sample_33 : BaseSample
{
public Exception Ex { get; set; }
[Error]
public void Error(ExceptionContext ex)
{
Ex = ex.Exception;
ex.ReThrow = true;
}
[Verb]
public void Foo()
{
throw new Exception("blah");
}
}
public class Sample_34 : BaseSample
{
public bool Handled { get; set; }
[Error]
public void Error(ExceptionContext ex)
{
Handled = true;
ex.ReThrow = true;
}
[Verb]
public void Foo()
{
throw new Exception("blah");
}
}
public class Sample_35 : BaseSample
{
[Error]
public void Error(int x)
{
}
}
public class Sample_36 : BaseSample
{
[Error]
public void Error(TargetInvocationException ex)
{
}
}
public class Sample_37 : BaseSample
{
[Error]
public void Error(Exception ex1, Exception ex2)
{
}
}
public class Sample_38 : BaseSample
{
[Verb(IsDefault = true)]
public void Foo1()
{
}
[Verb(IsDefault = true)]
public void Foo2()
{
}
}
public class Sample_39 : BaseSample
{
[Verb(IsDefault = true)]
public void Foo()
{
throw new Exception();
}
}
public class All_Validations_Sample
{
[Verb]
[Validate("")]
public static void Foo(
[MoreThan(1)]
[MoreOrEqualTo(1)]
[LessThan(1)]
[LessOrEqualTo(1)]
[RegexMatches("1")]
[FileExists]
[DirectoryExists]
[PathExists]
int x)
{
}
}
public class Sample_40 : BaseSample
{
[Verb]
[Validate("p1 > p2")]
public static void Foo1(int p1, int p2)
{
}
[Verb]
[Validate("p1 > p2")]
[Validate("p1 > 10")]
public static void Foo2(int p1, int p2)
{
}
[Verb]
[Validate("str in ('abc','def')")]
public static void Foo3(string str)
{
}
[Verb]
[Validate("str in ('abc','def')", CaseSensitive = true)]
public static void Foo5(string str)
{
}
[Verb]
[Validate("str like '*foo'")]
public static void Foo4(string str)
{
}
[Verb]
public static void Dummy()
{
}
[Global]
[Validate("num > 10")]
public static void Boing(int num)
{
}
}
public class Sample_41 : BaseSample
{
public Dictionary<string, object> Values { get; private set; }
[Verb(IsDefault = true)]
public void Foo(string str, int num, bool b, Case c, int[] numbers)
{
Values = new Dictionary<string, object>
{
{ "str", str },
{ "num", num},
{ "b", b },
{ "c", c },
{ "numbers", numbers },
};
}
}
public class Sample_42 : BaseSample
{
public MyType TheType { get; private set; }
public MyType TheType_Global { get; private set; }
public int[][] Array { get; private set; }
[Verb(IsDefault = true)]
public void Foo(MyType t)
{
TheType = t;
}
[Verb]
public void Bar(int[][] arr)
{
Array = arr;
}
[Global]
public void Glob(MyType t)
{
TheType_Global = t;
}
[Verb]
public void Val(MyValidatedType t)
{
}
[Verb]
public void ValCollection(MyValidatedType[] t)
{
}
[Verb]
public void Complex(MyComplexType t)
{
}
[Verb]
public void Props(MyComplexType_2 t)
{
}
[Verb]
public void Zoo(TypeWithProps t)
{
}
[Verb]
public void ZooCollection(TypeWithProps[] t)
{
}
}
public class Sample_43
{
public PreVerbExecutionContext PreContext;
public PostVerbExecutionContext PostContext;
public bool VerbExecuted;
[Verb]
public void Foo(string str, int num)
{
VerbExecuted = true;
}
[PreVerbExecution]
private void Pre(PreVerbExecutionContext context)
{
PreContext = context;
context.UserContext.Add("a", 54);
}
[PostVerbExecution]
private void Post(PostVerbExecutionContext context)
{
PostContext = context;
}
}
public class Sample_44
{
public PreVerbExecutionContext PreContext;
public PostVerbExecutionContext PostContext;
public bool VerbExecuted;
[Verb]
public void Foo(string str, int num)
{
VerbExecuted = true;
}
[PreVerbExecution]
private void Pre(PreVerbExecutionContext context)
{
PreContext = context;
context.Cancel = true;
}
[PostVerbExecution]
private void Post(PostVerbExecutionContext context)
{
PostContext = context;
}
}
public class Sample_45
{
[PreVerbExecution]
private void Pre1(PreVerbExecutionContext context)
{
}
[PreVerbExecution]
private void Pre2(PreVerbExecutionContext context)
{
}
}
public class Sample_46
{