File tree Expand file tree Collapse file tree
src/main/java/net/marcellperger/mathexpr/cli/minicli Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010import java .util .HashMap ;
1111import java .util .List ;
1212import java .util .Map ;
13+ import java .util .function .Function ;
1314
1415public class MiniCLI {
1516 Map <String , CLIOption <?>> options = new HashMap <>();
@@ -21,14 +22,24 @@ public MiniCLI() {
2122
2223 @ Contract ("_ -> new" )
2324 public CLIOption <String > addStringOption (String @ NotNull ... names ) {
25+ return addOptionFromFactory (names , StringCLIOption ::new );
26+ }
27+ @ Contract ("_ -> new" )
28+ public CLIOption <Boolean > addBooleanOption (String @ NotNull ... names ) {
29+ return addOptionFromFactory (names , BooleanCLIOption ::new );
30+ }
31+
32+ protected <T > CLIOption <T > addOptionFromFactory (
33+ String @ NotNull [] names ,
34+ Function <? super List <String >, ? extends CLIOption <T >> optionFactory ) {
2435 // First validate all of them so we don't mutate unless all are valid
2536 for (String name : names ) {
2637 if (!name .startsWith ("-" )) throw new IllegalArgumentException ("Option name must start with '-'" );
2738 if (options .containsKey (name )) {
2839 throw new IllegalStateException ("Argument '%s' has already been registered" .formatted (name ));
2940 }
3041 }
31- CLIOption <String > opt = new StringCLIOption (List .of (names ));
42+ CLIOption <T > opt = optionFactory . apply (List .of (names ));
3243 for (String name : names ) {
3344 options .put (name , opt );
3445 }
You can’t perform that action at this time.
0 commit comments