3
3
import io .typecraft .command .i18n .MessageId ;
4
4
import io .vavr .*;
5
5
import io .vavr .control .Either ;
6
+ import io .vavr .control .Option ;
6
7
import lombok .Data ;
7
8
import lombok .With ;
8
9
import org .jetbrains .annotations .Nullable ;
@@ -42,36 +43,15 @@ public Optional<Command<A>> getFallback() {
42
43
}
43
44
}
44
45
45
- @ Data
46
- @ With
47
- class Present <A > implements Command <A > {
48
- private final A value ;
49
- private final MessageId descriptionId ;
50
-
51
- private Present (A value , MessageId descriptionId ) {
52
- this .value = value ;
53
- this .descriptionId = descriptionId ;
54
- }
55
-
56
- public Present <A > withDescription (String description ) {
57
- return withDescriptionId (MessageId .of ("" ).withMessage (description ));
58
- }
59
-
60
- @ Override
61
- public <B > Present <B > map (Function <? super A , ? extends B > f ) {
62
- return new Present <>(f .apply (value ), getDescriptionId ());
63
- }
64
- }
65
-
66
46
@ Data
67
47
@ With
68
48
class Parser <A > implements Command <A > {
69
- private final Function <List <String >, Tuple2 <Optional <A >, List <String >>> parser ;
49
+ private final Function <List <String >, Tuple2 <Option <A >, List <String >>> parser ;
70
50
private final List <Supplier <List <String >>> tabCompleters ;
71
51
private final List <MessageId > names ;
72
52
private final MessageId descriptionId ;
73
53
74
- private Parser (Function <List <String >, Tuple2 <Optional <A >, List <String >>> parser , List <Supplier <List <String >>> tabCompleters , List <MessageId > names , MessageId descriptionId ) {
54
+ private Parser (Function <List <String >, Tuple2 <Option <A >, List <String >>> parser , List <Supplier <List <String >>> tabCompleters , List <MessageId > names , MessageId descriptionId ) {
75
55
this .parser = parser ;
76
56
this .tabCompleters = tabCompleters ;
77
57
this .names = names ;
@@ -103,13 +83,22 @@ static <A> Mapping<A> mapping(Tuple2<String, Command<? extends A>>... entries) {
103
83
return new Mapping <>(map , null );
104
84
}
105
85
106
- static <A > Present <A > present (A value ) {
107
- return new Present <>(value , MessageId .of ("" ));
86
+ static <A > Parser <A > present (A value ) {
87
+ return argument (() -> value );
88
+ }
89
+
90
+ static <T > Parser <T > argument (Supplier <T > f ) {
91
+ return new Parser <>(
92
+ args -> new Tuple2 <>(Option .some (f .get ()), args ),
93
+ Collections .emptyList (),
94
+ Collections .singletonList (MessageId .of ("" )),
95
+ MessageId .ofEmpty ()
96
+ );
108
97
}
109
98
110
99
static <T , A > Parser <T > argument (Function <? super A , ? extends T > f , Argument <A > argument ) {
111
100
return new Parser <>(
112
- args -> argument .getParser ().apply (args ).map1 (aO -> aO .map (f )),
101
+ args -> argument .getParser ().apply (args ).map1 (aO -> Option . ofOptional ( aO ) .map (f )),
113
102
argument .getTabCompleters (),
114
103
argument .getIds (),
115
104
MessageId .of ("" )
@@ -167,15 +156,18 @@ static <A> Either<CommandFailure<A>, CommandSuccess<A>> parseWithIndex(int index
167
156
? parseWithIndex (index + 1 , args , subCommand )
168
157
: Either .left (new CommandFailure .UnknownSubCommand <>(args , index , mapCommand ));
169
158
}
170
- } else if (command instanceof Present ) {
171
- Present <A > present = (Present <A >) command ;
172
- return Either .right (new CommandSuccess <>(args , index , present .getValue ()));
173
159
} else if (command instanceof Parser ) {
174
160
Parser <A > parser = (Parser <A >) command ;
175
- List <String > list = new ArrayList <>(Arrays .asList (Arrays .copyOfRange (args , index , args .length )));
176
- A a = parser .getParser ().apply (list )._1 .orElse (null );
177
- return a != null
178
- ? Either .right (new CommandSuccess <>(new String [0 ], args .length , a ))
161
+ List <String > list = index <= args .length
162
+ ? new ArrayList <>(Arrays .asList (Arrays .copyOfRange (args , index , args .length )))
163
+ : Collections .emptyList ();
164
+ Tuple2 <Option <A >, List <String >> result = parser .getParser ().apply (list );
165
+ Option <A > aO = result ._1 ;
166
+ List <String > remainList = result ._2 ;
167
+ A a = aO .getOrNull ();
168
+ int currentIndex = index + list .size () - remainList .size ();
169
+ return aO .isDefined ()
170
+ ? Either .right (new CommandSuccess <>(args , currentIndex , a ))
179
171
: Either .left (new CommandFailure .ParsingFailure <>(parser .getNames (), parser ));
180
172
}
181
173
throw new UnsupportedOperationException ();
@@ -202,10 +194,6 @@ static <A> CommandTabResult<A> tabCompleteWithIndex(int index, String[] args, Co
202
194
? tabCompleteWithIndex (index + 1 , args , subCommand )
203
195
: CommandTabResult .suggestion (Collections .emptyList ());
204
196
}
205
- } else if (command instanceof Present ) {
206
- Present <A > present = (Present <A >) command ;
207
- String [] newArgs = Arrays .copyOfRange (args , index , args .length );
208
- return CommandTabResult .present (newArgs , present .getValue ());
209
197
} else if (command instanceof Parser ) {
210
198
Parser <A > parser = (Parser <A >) command ;
211
199
int pos = args .length - index - 1 ;
@@ -247,13 +235,7 @@ static <A> List<Map.Entry<List<String>, Command<A>>> getEntries(Command<A> cmd)
247
235
}
248
236
249
237
static <A > CommandSpec getSpec (Command <A > cmd ) {
250
- if (cmd instanceof Command .Present ) {
251
- Present <A > present = (Present <A >) cmd ;
252
- return CommandSpec .of (
253
- Collections .emptyList (),
254
- present .getDescriptionId ()
255
- );
256
- } else if (cmd instanceof Command .Parser ) {
238
+ if (cmd instanceof Command .Parser ) {
257
239
Parser <A > parser = (Parser <A >) cmd ;
258
240
return CommandSpec .of (
259
241
parser .getNames (),
0 commit comments