/
ArgumentParser.java
1504 lines (1371 loc) · 43.6 KB
/
ArgumentParser.java
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
package com.laytonsmith.PureUtilities;
import com.laytonsmith.PureUtilities.Common.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* An ArgumentParser allows for programmatic registration of arguments, which
* will be automatically parsed and validated. Additionally, automatically
* generated help text can be retrieved and displayed, perhaps if a --help
* argument is present.
*
*/
public final class ArgumentParser {
/**
* Eases the operation of building a single argument. A new instance of this should be constructed, and the IDE
* will guide you through selecting the rest of the elements. Using this builder, it is not possible to create
* an inconsistent Argument.
*/
public static class ArgumentBuilder {
enum Mode { SHORT, LONG, BOTH, DEFAULT }
Mode mode;
char shortArg;
String longArg;
String description;
String usageName;
boolean required;
Type argType = Type.ARRAY_OF_STRINGS;
String defaultValue;
/**
* Sets the description for this argument. For consistency, it is arbitrarily decided that these descriptions
* should end in a period.
* @param description The description
* @return The next step in the build process
*/
public ArgumentBuilderRequired2 setDescription(String description) {
Objects.requireNonNull(description, "description may not be null");
ArgumentBuilder.this.description = description;
return new ArgumentBuilderRequired2();
}
public class ArgumentBuilderRequired2 {
ArgumentBuilderRequired2(){}
/**
* Sets the usage name for this argument. Typically, this is just the name
* of the long argument, or a very short phrase describing the value,
* but is only used for informational purposes, so
* is ok to be repeated across arguments.
* <p>
* During help generation, this argument is surrounded by <> to indicate that it
* is an argument, so there is no need to wrap the value in symbols.
* <p>
* The documentation will look something like this, assuming the argument is named "path"
* and the usageName is "path to file":
* <pre>
* --path <path to file>
* </pre>
*
* If the type of the argument is an array, {@code , ...} is appended, and if it is numeric, {@code #}
* is prepended.
* <p>
* Flags do not have a parameter, so no usage name is needed.
*
* @param usageName The display name in the help docs.
* @return The next step in the build process.
*/
public ArgumentBuilderRequired3 setUsageName(String usageName) {
Objects.requireNonNull(usageName, "usageName may not be null");
ArgumentBuilder.this.usageName = usageName;
return new ArgumentBuilderRequired3();
}
/**
* Treat this argument as a flag. A flag is a value, which is true if present, and false if not present.
* It takes no arguments, and cannot be the default argument.
* @return The next step in the build process.
*/
public FlagBuilderMode asFlag() {
return new FlagBuilderMode();
}
}
public class ArgumentBuilderRequired3 {
ArgumentBuilderRequired3(){}
/**
* Configures this as a required argument. If the argument is missing, then a {@link ValidationException}
* will be thrown during parsing.
* @return The next step in the build process.
*/
public RequiredArgumentBuilderMode setRequired() {
ArgumentBuilder.this.required = true;
return new RequiredArgumentBuilderMode();
}
/**
* Shortcut to setting this argument as required and as the default argument.
* @return The final step in the build process.
*/
public RequiredArgumentBuilderOptional setRequiredAndDefault() {
return setRequired().asDefault();
}
/**
* Configures this as an optional argument. The build
* process will allow you to set a default value if
* desired.
* @return The next step in the build process.
*/
public OptionalArgumentBuilderMode setOptional() {
ArgumentBuilder.this.required = false;
return new OptionalArgumentBuilderMode();
}
/**
* Shortcut to setting this argument as optional and as the default argument.
* @return The final step in the build process.
*/
public OptionalArgumentBuilderOptional setOptionalAndDefault() {
return setOptional().asDefault();
}
}
public class FlagBuilderMode {
/**
* Creates an argument that is a flag. That is, this is a boolean true if present,
* boolean false if not. There are no arguments attached to this value.
* @param shortArg The short code for the argument
* @return The next step in the build process.
*/
public ArgumentBuilderFlag setName(char shortArg) {
new OptionalArgumentBuilderMode().setName(shortArg);
argType = Type.BOOLEAN;
return new ArgumentBuilderFlag();
}
/**
* Creates an argument that is a flag. That is, this is a boolean true if present,
* boolean false if not. There are no arguments attached to this value.
* @param longArg The long code for the argument
* @return The next step in the build process.
*/
public ArgumentBuilderFlag setName(String longArg) {
new OptionalArgumentBuilderMode().setName(longArg);
argType = Type.BOOLEAN;
return new ArgumentBuilderFlag();
}
/**
* Creates an argument that is a flag. That is, this is a boolean true if present,
* boolean false if not. There are no arguments attached to this value.
* @param shortArg The short code for the argument
* @param longArg The long code for the argument
* @return The next step in the build process.
*/
public ArgumentBuilderFlag setName(char shortArg, String longArg) {
new OptionalArgumentBuilderMode().setName(shortArg, longArg);
argType = Type.BOOLEAN;
return new ArgumentBuilderFlag();
}
}
public class OptionalArgumentBuilderMode {
/**
* Sets the name of this argument, using a short code. The value passed in must
* be a printable character (as defined by {@link Character#isAlphabetic(int)})
* and cannot be \0.
*
* In this mode, the argument may only be addressed via the short arg.
*
* This does not set a flag, this sets an argument with input.
* @param shortArg The short arg
* @return The next step in the build process
*/
public OptionalArgumentBuilderOptional setName(char shortArg) {
if(shortArg == '\0') {
throw new NullPointerException("shortArg may not be the null character");
}
if(!Character.isAlphabetic(shortArg)) {
throw new IllegalArgumentException("shortArg must be a alphabetical character");
}
mode = Mode.SHORT;
ArgumentBuilder.this.shortArg = shortArg;
return new OptionalArgumentBuilderOptional();
}
/**
* Sets the name of this argument, using a long code. The value passed in may not
* be null.
*
* In this mode, the argument may only be addressed via the long arg.
*
* This does not set a flag, this sets an argument with input.
*
* @param longArg The long arg
* @return The next step in the build process
*/
public OptionalArgumentBuilderOptional setName(String longArg) {
Objects.requireNonNull(longArg, "longArg must not be null");
mode = Mode.LONG;
ArgumentBuilder.this.longArg = longArg;
return new OptionalArgumentBuilderOptional();
}
/**
* Sets the name of this argument, using a short code and a long code.
* The short arg passed in must
* be a printable character (as defined by {@link Character#isAlphabetic(int)})
* and cannot be \0.
*
* The long arg passed in may not be null.
*
* @param shortArg The single character address for this argument
* @param longArg The long name of this argument
* @return The next step in the build process.
*/
public OptionalArgumentBuilderOptional setName(char shortArg, String longArg) {
setName(shortArg);
setName(longArg);
// reset mode
mode = Mode.BOTH;
return new OptionalArgumentBuilderOptional();
}
/**
* Considers this argument to be a default argument, that is, this contains
* the parameters passed in without parameter names. There may only be one of
* these arguments in the list. If a second one is attempted to be added, an
* exception is thrown.
* @return The next step in the build process.
*/
public OptionalArgumentBuilderOptional asDefault() {
mode = Mode.DEFAULT;
return new OptionalArgumentBuilderOptional();
}
}
public class RequiredArgumentBuilderMode {
/**
* Sets the name of this argument, using a short code. The value passed in must
* be a printable character (as defined by {@link Character#isAlphabetic(int)})
* and cannot be \0.
*
* In this mode, the argument may only be addressed via the short arg.
*
* This does not set a flag, this sets an argument with input.
* @param shortArg The short arg
* @return The next step in the build process
*/
public RequiredArgumentBuilderOptional setName(char shortArg) {
if(shortArg == '\0') {
throw new NullPointerException("shortArg may not be the null character");
}
if(!Character.isAlphabetic(shortArg)) {
throw new IllegalArgumentException("shortArg must be a alphabetical character");
}
mode = Mode.SHORT;
ArgumentBuilder.this.shortArg = shortArg;
return new RequiredArgumentBuilderOptional();
}
/**
* Sets the name of this argument, using a long code. The value passed in may not
* be null.
*
* In this mode, the argument may only be addressed via the long arg.
*
* This does not set a flag, this sets an argument with input.
*
* @param longArg The long arg
* @return The next step in the build process
*/
public RequiredArgumentBuilderOptional setName(String longArg) {
Objects.requireNonNull(longArg, "longArg must not be null");
mode = Mode.LONG;
ArgumentBuilder.this.longArg = longArg;
return new RequiredArgumentBuilderOptional();
}
/**
* Sets the name of this argument, using a short code and a long code.
* The short arg passed in must
* be a printable character (as defined by {@link Character#isAlphabetic(int)})
* and cannot be \0.
*
* The long arg passed in may not be null.
*
* @param shortArg The single character address for this argument
* @param longArg The long name of this argument
* @return The next step in the build process.
*/
public RequiredArgumentBuilderOptional setName(char shortArg, String longArg) {
setName(shortArg);
setName(longArg);
// reset mode
mode = Mode.BOTH;
return new RequiredArgumentBuilderOptional();
}
/**
* Considers this argument to be a default argument, that is, this contains
* the parameters passed in without parameter names. There may only be one of
* these arguments in the list. If a second one is attempted to be added, an
* exception is thrown.
* @return The next step in the build process.
*/
public RequiredArgumentBuilderOptional asDefault() {
mode = Mode.DEFAULT;
return new RequiredArgumentBuilderOptional();
}
}
/**
* A subset of types that are only valid for non-flag types.
*/
public static enum BuilderTypeNonFlag {
/**
* A single string value
*/
STRING(Type.STRING),
/**
* An array of strings
*/
ARRAY_OF_STRINGS(Type.ARRAY_OF_STRINGS),
/**
* A numeric value
*/
NUMBER(Type.NUMBER),
/**
* An array of numeric values
*/
ARRAY_OF_NUMBERS(Type.ARRAY_OF_NUMBERS);
Type type;
private BuilderTypeNonFlag(Type type) {
this.type = type;
}
Type getType() {
return this.type;
}
}
/**
* Represents an object that is ready to be built. While user code cannot
* build the Argument directly, this is used internally.
*/
public abstract class ArgumentBuilderFinal {
private ArgumentBuilderFinal(){}
/**
* After building, this Argument should be in a consistent state.
* @return
*/
abstract Argument build();
}
public final class ArgumentBuilderFlag extends ArgumentBuilderFinal {
private ArgumentBuilderFlag(){}
@Override
Argument build() {
return new OptionalArgumentBuilderOptional().build();
}
}
public final class OptionalArgumentBuilderOptional extends ArgumentBuilderFinal {
OptionalArgumentBuilderOptional(){}
/**
* Sets the argument type. By default, ARRAY_OF_STRINGS is assumed.
* @param argType The type of this argument
* @return {@code this}, which can be called repeatedly to set the
* optional arguments, or used as is.
*/
public OptionalArgumentBuilderOptional setArgType(BuilderTypeNonFlag argType) {
Objects.requireNonNull(argType, "argType cannot be null");
ArgumentBuilder.this.argType = argType.getType();
return this;
}
/**
* For arguments that are not required, the default value may be set.
* If this argument is required, calling this method is an error.
* @param defaultVal
* @return {@code this}, which can be called repeatedly to set the
* optional arguments, or used as is.
*/
public OptionalArgumentBuilderOptional setDefaultVal(String defaultVal) {
if(required) {
throw new IllegalArgumentException("Required arguments cannot have a default value provided");
}
ArgumentBuilder.this.defaultValue = defaultVal;
return this;
}
@Override
Argument build() {
return new Argument(shortArg == '\0' ? null : shortArg, longArg, argType, defaultValue, description,
usageName, required);
}
}
public final class RequiredArgumentBuilderOptional extends ArgumentBuilderFinal {
RequiredArgumentBuilderOptional(){}
/**
* Sets the argument type. By default, ARRAY_OF_STRINGS is assumed.
* @param argType The type of this argument
* @return {@code this}, which can be called repeatedly to set the
* optional arguments, or used as is.
*/
public RequiredArgumentBuilderOptional setArgType(BuilderTypeNonFlag argType) {
Objects.requireNonNull(argType, "argType cannot be null");
ArgumentBuilder.this.argType = argType.getType();
return this;
}
@Override
Argument build() {
return new OptionalArgumentBuilderOptional().build();
}
}
}
/**
* A description of the command itself.
*/
String description = "";
/**
* The model for the arguments
*/
List<Argument> argumentModel = new ArrayList<>();
/**
* Whether to throw an error if unrecognized arguments were provided
*/
boolean errorOnUnknown = true;
/**
* Returns the default argument, if it exists.
*
* @return
*/
private Argument getArgument() {
for(Argument a : argumentModel) {
if(a.shortArg == null && a.longArg == null) {
return a;
}
}
return null;
}
private Argument getArgument(Character c) {
for(Argument a : argumentModel) {
if(a.shortArg == null) {
continue;
}
if(a.shortArg.equals(c)) {
return a;
}
}
return null;
}
private Argument getArgument(String s) {
for(Argument a : argumentModel) {
if(a.longArg == null) {
continue;
}
if(a.longArg.equals(s)) {
return a;
}
}
return null;
}
private static final class Argument {
Character shortArg;
String longArg;
Type argType;
String defaultVal;
List<String> defaultList;
String description;
String usageName;
boolean required;
String singleVal;
List<String> arrayVal;
private Argument(Argument arg) {
if(arg == null) {
return;
}
this.shortArg = arg.shortArg;
this.longArg = arg.longArg;
this.argType = arg.argType;
this.defaultVal = arg.defaultVal;
this.description = arg.description;
this.usageName = arg.usageName;
this.required = arg.required;
}
private Argument(Character shortArg, String longArg, Type argType, String defaultVal, String description, String usageName, boolean required) {
this.shortArg = shortArg;
this.longArg = longArg;
this.argType = argType;
this.description = description;
this.defaultVal = defaultVal;
if(isArray() && defaultVal != null) {
defaultList = ArgumentParser.lex(defaultVal);
}
this.usageName = usageName;
this.required = required;
}
public final boolean isFlag() {
return argType == Type.BOOLEAN;
}
public final boolean isArray() {
return argType == Type.ARRAY_OF_NUMBERS || argType == Type.ARRAY_OF_STRINGS;
}
public final boolean isSingle() {
return argType == Type.NUMBER || argType == Type.STRING;
}
public final boolean isNumeric() {
return argType == Type.NUMBER || argType == Type.ARRAY_OF_NUMBERS;
}
private void setValue(String val) {
if(isArray()) {
arrayVal = ArgumentParser.lex(val);
} else {
singleVal = val;
}
}
private void setValue(List<String> val) {
arrayVal = new ArrayList<>(val);
}
public boolean modelEquals(Argument obj) {
if(this.shortArg != null) {
return this.shortArg.equals(obj.shortArg);
} else if(this.longArg != null) {
return this.longArg.equals(obj.longArg);
} else {
return obj.shortArg == null && obj.longArg == null;
}
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
if(longArg != null && shortArg != null) {
b.append("--").append(longArg).append("/").append("-").append(shortArg);
} else if(longArg != null) {
b.append("--").append(longArg);
} else if(shortArg != null) {
b.append("-").append(shortArg);
}
b.append(": ");
if(isSingle()) {
b.append(singleVal);
} else if(isArray()) {
boolean first = true;
b.append("[");
for(String s : arrayVal) {
if(!first) {
b.append(", ");
}
first = false;
b.append("\"").append(s.replaceAll("\"", "\\\"")).append("\"");
}
b.append("]");
}
b.append("\n");
return b.toString();
}
private String generateDescription(boolean shortCode) {
StringBuilder b = new StringBuilder();
b.append("\t");
if(shortArg == null && longArg == null) {
//Default argument
b.append("<").append(usageName).append(">: ").append(description).append("\n");
} else {
//If short code is false, we need to check to see if there is a short code, if so,
//this is an alias.
if(shortCode) {
b.append("-").append(shortArg);
} else {
b.append("--").append(longArg);
}
b.append(": ");
if(!shortCode && shortArg != null) {
//Alias
b.append("Alias to -").append(shortArg);
} else {
if(argType != Type.BOOLEAN) {
if(required) {
b.append("Required. ");
} else {
b.append("Optional. ");
}
}
if(argType == Type.NUMBER) {
b.append("A numeric value. ");
}
if(argType == Type.ARRAY_OF_NUMBERS) {
b.append("A list of numbers. ");
}
if(argType == Type.ARRAY_OF_STRINGS) {
b.append("A list. ");
}
b.append(description.replaceAll("\n", "\n\t\t"));
}
b.append("\n");
}
return b.toString();
}
}
private ArgumentParser() {
}
public static final class ValidationException extends Exception {
private ValidationException(String string) {
super(string);
}
}
public static class ResultUseException extends RuntimeException {
ResultUseException(String string) {
super(string);
}
}
public class ArgumentParserResults {
List<Argument> arguments = new ArrayList<>();
List<String> unclassified = new ArrayList<>();
private void updateArgument(Argument a) {
if(a == null) {
return;
}
List<Argument> toRemove = new ArrayList<>();
for(Argument arg : arguments) {
if(arg.modelEquals(a)) {
toRemove.add(arg);
}
}
for(Argument arg : toRemove) {
arguments.remove(arg);
}
arguments.add(a);
}
private void updateUnclassifiedArgument(String a) {
unclassified.add(a);
}
/**
* Returns true if the flag represented by this short code is set.
*
* @param flag
* @return
*/
public boolean isFlagSet(Character flag) {
return getArg(flag) != null;
}
/**
* Returns true is the flag represented by this long code is set.
*
* @param flag
* @return
*/
public boolean isFlagSet(String flag) {
return getArg(flag) != null;
}
/**
* Gets the unassociated arguments passed in as a String. For instance,
* if the arguments were <code>These are arguments</code>, then "These
* are arguments" will be returned. However, assuming -c is registered
* as a single string type, and the arguments are
* <code>-c These are arguments</code>, then only "are arguments" is
* returned. This will return an empty string if no arguments were set.
*
* @return
*/
public String getStringArgument() {
try {
Argument a = getArg();
if(a.arrayVal == null) {
return "";
}
StringBuilder b = new StringBuilder();
boolean first = true;
for(String val : a.arrayVal) {
if(!first) {
b.append(" ");
}
first = false;
b.append(val);
}
return b.toString();
} catch (ResultUseException e) {
return "";
}
}
/**
* Returns the string associated with the switch represented by this
* short code. If the switch wasn't set, null is returned.
*
* @param flag
* @return
* @throws ArgumentParser.ResultUseException
*/
public String getStringArgument(Character flag) throws ResultUseException {
return getStringArgument(getArg(flag));
}
/**
* Returns the string associated with the switch represented by this
* long code. If the switch wasn't set, null is returned.
*
* @param flag
* @return
* @throws ArgumentParser.ResultUseException
*/
public String getStringArgument(String flag) throws ResultUseException {
return getStringArgument(getArg(flag));
}
private String getStringArgument(Argument arg) {
if(arg == null) {
return null;
}
if(arg.argType != Type.STRING) {
throw new ClassCastException("Argument type not set to " + Type.STRING.name() + ". Cannot return a " + "string" + ".");
}
return arg.singleVal;
}
/**
* Returns the value associated with the switch represented by this
* short code, pre-parsed as a double. If the switch wasn't set, null is
* returned.
*
* @param flag
* @return
* @throws ResultUseException, NumberFormatException
*/
public Double getNumberArgument(Character flag) throws ResultUseException {
return getNumberArgument(getArg(flag));
}
/**
* Returns the value associated with the switch represented by this long
* code, pre-parsed as a double. If the switch wasn't set, null is
* returned.
*
* @param flag
* @return
* @throws ResultUseException, NumberFormatException
*/
public Double getNumberArgument(String flag) throws ResultUseException {
return getNumberArgument(getArg(flag));
}
private Double getNumberArgument(Argument arg) {
if(arg == null) {
return null;
}
if(arg.argType != Type.NUMBER) {
throw new ClassCastException("Argument type not set to " + Type.NUMBER.name() + ". Cannot return a " + "number" + ".");
}
return Double.parseDouble(arg.singleVal);
}
/**
* Gets the unassociated arguments passed in as a List of Strings. For
* instance, if the arguments were <code>These are arguments</code>,
* then ["These", "are", "arguments"] will be returned. However,
* assuming -c is registered as a single string type, and the arguments
* are <code>-c These are arguments</code>, then only ["are",
* "arguments"] is returned. This will return an empty array if no
* arguments were set.
*
* @return
*/
public List<String> getStringListArgument() {
try {
Argument a = getArg();
if(a.arrayVal == null) {
return new ArrayList<>();
}
return new ArrayList<>(a.arrayVal);
} catch (ResultUseException e) {
return new ArrayList<>();
}
}
/**
* Returns the list of values associated with the switch represented by
* this short code. If the switch wasn't set, null is returned.
*
* @param flag
* @return
* @throws ArgumentParser.ResultUseException
*/
public List<String> getStringListArgument(Character flag) throws ResultUseException {
return getStringListArgument(getArg(flag));
}
/**
* Returns the list of values associated with the switch represented by
* this long code. If the switch wasn't set, null is returned.
*
* @param flag
* @return
* @throws ArgumentParser.ResultUseException
*/
public List<String> getStringListArgument(String flag) throws ResultUseException {
return getStringListArgument(getArg(flag));
}
private List<String> getStringListArgument(Argument arg) {
if(arg == null) {
return null;
}
if(arg.argType != Type.ARRAY_OF_STRINGS) {
throw new ClassCastException("Argument type not set to " + Type.ARRAY_OF_STRINGS.name() + ". Cannot return a " + "string list" + ".");
}
return new ArrayList<>(arg.arrayVal);
}
/**
* Returns the list of values associated with the switch represented by
* this short code, pre-parsed into doubles. If the switch wasn't set,
* null is returned.
*
* @param flag
* @return
* @throws ArgumentParser.ResultUseException
*/
public List<Double> getNumberListArgument(Character flag) throws ResultUseException {
return getNumberListArgument(getArg(flag));
}
/**
* Returns the list of values associated with the switch represented by
* this long code, pre-parsed into doubles. If the switch wasn't set,
* null is returned.
*
* @param flag
* @return
* @throws ArgumentParser.ResultUseException
*/
public List<Double> getNumberListArgument(String flag) throws ResultUseException {
return getNumberListArgument(getArg(flag));
}
private List<Double> getNumberListArgument(Argument arg) {
if(arg == null) {
return null;
}
if(arg.argType != Type.ARRAY_OF_NUMBERS) {
throw new ClassCastException("Argument type not set to " + Type.ARRAY_OF_NUMBERS.name() + ". Cannot return a " + "number list" + ".");
}
List<Double> list = new ArrayList<>();
for(String s : arg.arrayVal) {
list.add(Double.parseDouble(s));
}
return list;
}
private Argument getArg() {
for(Argument a : arguments) {
if(a.shortArg == null && a.longArg == null) {
return a;
}
}
return new Argument(ArgumentParser.this.getArgument());
}
private Argument getArg(Character flag) throws ResultUseException {
for(Argument a : arguments) {
if(a.shortArg == null) {
continue;
}
if(a.shortArg.equals(flag)) {
return a;
}
}
return null;
}
private Argument getArg(String flag) throws ResultUseException {
for(Argument a : arguments) {
if(a.longArg == null) {
continue;
}
if(a.longArg.equals(flag)) {
return a;
}
}
return null;
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
for(Argument arg : arguments) {
if(arg.isFlag()) {
b.append("Flag ");
if(arg.longArg != null && arg.shortArg != null) {
b.append("--").append(arg.longArg).append("/").append("-").append(arg.shortArg);
} else if(arg.longArg != null) {
b.append("--").append(arg.longArg);
} else if(arg.shortArg != null) {
b.append("-").append(arg.shortArg);
}
b.append(" is set.\n");
} else {
b.append(arg.toString());
}
}
return b.toString();
}
}
public static ArgumentParser GetParser() {
return new ArgumentParser();
}
static enum Type {
STRING,
NUMBER,
ARRAY_OF_STRINGS,
ARRAY_OF_NUMBERS,
BOOLEAN
}
/**
* Adds the configured argument to the ArgumentParser.
* <p>
* To build an Argument, create a new instance of {@link ArgumentBuilder}.
* @param arg The argument to add to the list
* @return {@code this}, for continued chaining
*/
public ArgumentParser addArgument(ArgumentBuilder.ArgumentBuilderFinal arg) {
Argument arg0 = arg.build();
// Check to make sure this isn't a duplicate value
for(Argument a : argumentModel) {
// default arg
if(a.shortArg == null && a.longArg == null) {
// This is the default arg, ensure that arg is not the default
if(arg0.shortArg == null && arg0.longArg == null) {
throw new IllegalArgumentException("Only 1 default argument may be provided.");
}
}
if(a.shortArg != null && arg0.shortArg != null && a.shortArg.equals(arg0.shortArg)) {
throw new IllegalArgumentException("A new argument with short arg '" + arg0.shortArg + "' was provided,"
+ " but a previous argument with the same short arg was already provided.");
}
if(a.longArg != null && arg0.longArg != null && a.longArg.equals(arg0.longArg)) {