Skip to content

Commit 0fee5ec

Browse files
committed
Flag building cleanup
1 parent f067676 commit 0fee5ec

5 files changed

Lines changed: 84 additions & 51 deletions

File tree

cloud-annotations/src/main/java/org/incendo/cloud/annotations/assembler/FlagAssemblerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public FlagAssemblerImpl(final @NonNull CommandManager<?> commandManager) {
7171
permission = descriptor.permission();
7272
}
7373

74-
CommandFlag.Builder<Void> builder = this.commandManager
74+
CommandFlag.Builder<?, Void> builder = this.commandManager
7575
.flagBuilder(descriptor.name())
7676
.withDescription(description)
7777
.withAliases(descriptor.aliases())

cloud-core/src/main/java/org/incendo/cloud/Command.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2077,7 +2077,7 @@ private Builder(
20772077
* @param <T> flag value type
20782078
* @return new builder instance that uses the provided flag
20792079
*/
2080-
public @NonNull <T> Builder<C> flag(final CommandFlag.@NonNull Builder<T> builder) {
2080+
public @NonNull <T> Builder<C> flag(final CommandFlag.@NonNull Builder<C, T> builder) {
20812081
return this.flag(builder.build());
20822082
}
20832083

cloud-core/src/main/java/org/incendo/cloud/CommandManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ public void deleteRootCommand(final @NonNull String rootCommand) throws CloudCap
532532
* @param name Flag name
533533
* @return Flag builder
534534
*/
535-
public CommandFlag.@NonNull Builder<Void> flagBuilder(final @NonNull String name) {
535+
public CommandFlag.@NonNull Builder<C, Void> flagBuilder(final @NonNull String name) {
536536
return CommandFlag.builder(name);
537537
}
538538

cloud-core/src/main/java/org/incendo/cloud/parser/flag/CommandFlag.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ private CommandFlag(
7575
/**
7676
* Create a new {@link Builder}.
7777
*
78+
* @param <C> command sender type
7879
* @param name flag name
7980
* @return new {@link Builder}
8081
*/
8182
@API(status = API.Status.STABLE)
82-
public static @NonNull Builder<Void> builder(final @NonNull String name) {
83+
public static <C> @NonNull Builder<C, Void> builder(final @NonNull String name) {
8384
return new Builder<>(name);
8485
}
8586

@@ -165,21 +166,21 @@ public int hashCode() {
165166

166167

167168
@API(status = API.Status.STABLE)
168-
public static final class Builder<T> {
169+
public static final class Builder<C, T> {
169170

170171
private final String name;
171172
private final String[] aliases;
172173
private final Description description;
173174
private final Permission permission;
174-
private final TypedCommandComponent<?, T> commandComponent;
175+
private final TypedCommandComponent<C, T> commandComponent;
175176
private final FlagMode mode;
176177

177178
private Builder(
178179
final @NonNull String name,
179180
final @NonNull String[] aliases,
180181
final @NonNull Description description,
181182
final @NonNull Permission permission,
182-
final @Nullable TypedCommandComponent<?, T> commandComponent,
183+
final @Nullable TypedCommandComponent<C, T> commandComponent,
183184
final @NonNull FlagMode mode
184185
) {
185186
this.name = name;
@@ -201,7 +202,7 @@ private Builder(final @NonNull String name) {
201202
* @param aliases Flag aliases
202203
* @return New builder instance
203204
*/
204-
public @NonNull Builder<T> withAliases(final @NonNull String... aliases) {
205+
public @NonNull Builder<C, T> withAliases(final @NonNull String... aliases) {
205206
return this.withAliases(Arrays.asList(aliases));
206207
}
207208

@@ -213,7 +214,7 @@ private Builder(final @NonNull String name) {
213214
* @return New builder instance
214215
*/
215216
@API(status = API.Status.STABLE)
216-
public @NonNull Builder<T> withAliases(final @NonNull Collection<@NonNull String> aliases) {
217+
public @NonNull Builder<C, T> withAliases(final @NonNull Collection<@NonNull String> aliases) {
217218
final Set<String> filteredAliases = new HashSet<>();
218219
for (final String alias : aliases) {
219220
if (alias.isEmpty()) {
@@ -246,7 +247,7 @@ private Builder(final @NonNull String name) {
246247
* @return New builder instance
247248
*/
248249
@API(status = API.Status.STABLE)
249-
public @NonNull Builder<T> withDescription(final @NonNull Description description) {
250+
public @NonNull Builder<C, T> withDescription(final @NonNull Description description) {
250251
return new Builder<>(this.name, this.aliases, description, this.permission, this.commandComponent, this.mode);
251252
}
252253

@@ -257,7 +258,7 @@ private Builder(final @NonNull String name) {
257258
* @param <N> New component type
258259
* @return New builder instance
259260
*/
260-
public <N> @NonNull Builder<N> withComponent(final @NonNull TypedCommandComponent<?, N> component) {
261+
public <N> @NonNull Builder<C, N> withComponent(final @NonNull TypedCommandComponent<C, N> component) {
261262
return new Builder<>(this.name, this.aliases, this.description, this.permission, component, this.mode);
262263
}
263264

@@ -268,17 +269,8 @@ private Builder(final @NonNull String name) {
268269
* @param <N> new component type
269270
* @return New builder instance
270271
*/
271-
@SuppressWarnings({"rawtypes", "unchecked"})
272-
public <N> @NonNull Builder<N> withComponent(final @NonNull ParserDescriptor<?, N> parserDescriptor) {
273-
final CommandComponent.Builder builder = CommandComponent.builder();
274-
return new Builder<>(
275-
this.name,
276-
this.aliases,
277-
this.description,
278-
this.permission,
279-
builder.name(this.name).parser(parserDescriptor).build(),
280-
this.mode
281-
);
272+
public <N> @NonNull Builder<C, N> withComponent(final @NonNull ParserDescriptor<? super C, N> parserDescriptor) {
273+
return this.withComponent(CommandComponent.builder(this.name, parserDescriptor));
282274
}
283275

284276
/**
@@ -288,7 +280,7 @@ private Builder(final @NonNull String name) {
288280
* @param <N> New component type
289281
* @return New builder instance
290282
*/
291-
public <N> @NonNull Builder<N> withComponent(final CommandComponent.@NonNull Builder<?, N> builder) {
283+
public <N> @NonNull Builder<C, N> withComponent(final CommandComponent.@NonNull Builder<C, N> builder) {
292284
return this.withComponent(builder.build());
293285
}
294286

@@ -299,17 +291,28 @@ private Builder(final @NonNull String name) {
299291
* @return New builder instance
300292
*/
301293
@API(status = API.Status.STABLE)
302-
public @NonNull Builder<T> withPermission(final @NonNull Permission permission) {
294+
public @NonNull Builder<C, T> withPermission(final @NonNull Permission permission) {
303295
return new Builder<>(this.name, this.aliases, this.description, permission, this.commandComponent, this.mode);
304296
}
305297

298+
/**
299+
* Create a new builder instance using the given flag permission
300+
*
301+
* @param permissionString Flag permission
302+
* @return New builder instance
303+
*/
304+
@API(status = API.Status.STABLE)
305+
public @NonNull Builder<C, T> withPermission(final @NonNull String permissionString) {
306+
return this.withPermission(Permission.of(permissionString));
307+
}
308+
306309
/**
307310
* Marks the flag as {@link FlagMode#REPEATABLE}.
308311
*
309312
* @return new builder instance
310313
*/
311314
@API(status = API.Status.STABLE)
312-
public @NonNull Builder<T> asRepeatable() {
315+
public @NonNull Builder<C, T> asRepeatable() {
313316
return new Builder<>(
314317
this.name,
315318
this.aliases,

cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/org/incendo/cloud/kotlin/MutableCommandBuilder.kt

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ import org.incendo.cloud.key.CloudKey
3535
import org.incendo.cloud.kotlin.extension.command
3636
import org.incendo.cloud.kotlin.extension.senderType
3737
import org.incendo.cloud.parser.ParserDescriptor
38+
import org.incendo.cloud.parser.flag.CommandFlag
3839
import org.incendo.cloud.permission.Permission
3940
import kotlin.reflect.KClass
4041

4142
/**
4243
* A mutable [Command.Builder] wrapper, providing functions to assist in creating commands using the
4344
* Kotlin builder DSL style
4445
*
45-
* @property commandBuilder the command builder the mutate
46+
* @property commandBuilder the command builder to mutate
4647
* @property commandManager the command manager which will own this command
4748
* @constructor Create a new [MutableCommandBuilder]
4849
*/
@@ -361,6 +362,16 @@ public class MutableCommandBuilder<C : Any>(
361362
component: CommandComponent<C>
362363
): MutableCommandBuilder<C> = mutate { it.argument(component) }
363364

365+
/**
366+
* Adds a new component to this command
367+
*
368+
* @param component component to add
369+
* @return this mutable builder
370+
*/
371+
public fun <T> argument(
372+
component: CommandComponent.Builder<C, T>
373+
): MutableCommandBuilder<C> = mutate { it.argument(component) }
374+
364375
/**
365376
* Adds a new component to this command
366377
*
@@ -393,11 +404,7 @@ public class MutableCommandBuilder<C : Any>(
393404
name: String,
394405
parser: ParserDescriptor<C, T>,
395406
mutator: CommandComponent.Builder<C, T>.() -> Unit = {}
396-
): MutableCommandBuilder<C> = mutate {
397-
it.argument(
398-
CommandComponent.builder(name, parser).also(mutator)
399-
)
400-
}
407+
): MutableCommandBuilder<C> = argument(CommandComponent.builder(name, parser).also(mutator))
401408

402409
/**
403410
* Adds a new component to this command
@@ -411,11 +418,7 @@ public class MutableCommandBuilder<C : Any>(
411418
name: String,
412419
parser: ParserDescriptor<C, T>,
413420
mutator: CommandComponent.Builder<C, T>.() -> Unit = {}
414-
): MutableCommandBuilder<C> = mutate {
415-
it.argument(
416-
CommandComponent.builder(name, parser).optional().also(mutator)
417-
)
418-
}
421+
): MutableCommandBuilder<C> = argument(CommandComponent.builder(name, parser).optional().also(mutator))
419422

420423
/**
421424
* Adds a new component to this command
@@ -429,11 +432,7 @@ public class MutableCommandBuilder<C : Any>(
429432
name: CloudKey<T>,
430433
parser: ParserDescriptor<C, T>,
431434
mutator: CommandComponent.Builder<C, T>.() -> Unit = {}
432-
): MutableCommandBuilder<C> = mutate {
433-
it.argument(
434-
CommandComponent.builder(name, parser).also(mutator)
435-
)
436-
}
435+
): MutableCommandBuilder<C> = argument(CommandComponent.builder(name, parser).also(mutator))
437436

438437
/**
439438
* Adds a new component to this command
@@ -447,11 +446,7 @@ public class MutableCommandBuilder<C : Any>(
447446
name: CloudKey<T>,
448447
parser: ParserDescriptor<C, T>,
449448
mutator: CommandComponent.Builder<C, T>.() -> Unit = {}
450-
): MutableCommandBuilder<C> = mutate {
451-
it.argument(
452-
CommandComponent.builder(name, parser).optional().also(mutator)
453-
)
454-
}
449+
): MutableCommandBuilder<C> = argument(CommandComponent.builder(name, parser).optional().also(mutator))
455450

456451
/**
457452
* Add a new argument to this command
@@ -461,7 +456,7 @@ public class MutableCommandBuilder<C : Any>(
461456
*/
462457
public fun argument(
463458
componentSupplier: () -> CommandComponent<C>
464-
): MutableCommandBuilder<C> = mutate { it.argument(componentSupplier()) }
459+
): MutableCommandBuilder<C> = argument(componentSupplier())
465460

466461
/**
467462
* Add a new argument to this command
@@ -481,7 +476,7 @@ public class MutableCommandBuilder<C : Any>(
481476
*/
482477
public fun optional(
483478
componentSupplier: () -> CommandComponent.Builder<C, *>
484-
): MutableCommandBuilder<C> = mutate { it.optional(componentSupplier()) }
479+
): MutableCommandBuilder<C> = optional(componentSupplier())
485480

486481
/**
487482
* Add a new literal argument to this command
@@ -507,6 +502,15 @@ public class MutableCommandBuilder<C : Any>(
507502
it.handler(handler)
508503
}
509504

505+
/**
506+
* Set the [CommandExecutionHandler] for this builder
507+
*
508+
* @param handler command execution handler
509+
* @return this mutable builder
510+
*/
511+
public fun futureHandler(handler: CommandExecutionHandler.FutureCommandExecutionHandler<C>): MutableCommandBuilder<C> =
512+
mutate { it.futureHandler(handler) }
513+
510514
/**
511515
* Sets a new command execution handler that invokes the given {@code handler} before the current
512516
* {@link #handler() handler}.
@@ -535,22 +539,48 @@ public class MutableCommandBuilder<C : Any>(
535539
* @param name name of the flag
536540
* @param aliases flag aliases
537541
* @param description description of the flag
538-
* @param componentSupplier component supplier for the flag
542+
* @param parser parser for the flag
539543
* @param <T> the component value type
540544
* @return this mutable builder
541545
*/
542546
public fun <T> flag(
543547
name: String,
544548
aliases: Array<String> = emptyArray(),
545549
description: Description = Description.empty(),
546-
componentSupplier: () -> TypedCommandComponent<C, T>
550+
parser: ParserDescriptor<C, T>
551+
): MutableCommandBuilder<C> = mutate {
552+
it.flag(
553+
this.commandManager
554+
.flagBuilder(name)
555+
.withAliases(*aliases)
556+
.withDescription(description)
557+
.withComponent(parser)
558+
.build()
559+
)
560+
}
561+
562+
/**
563+
* Add a new flag component to this command
564+
*
565+
* @param name name of the flag
566+
* @param aliases flag aliases
567+
* @param description description of the flag
568+
* @param builderModifier flag builder modifier
569+
* @param T the component value type
570+
* @return this mutable builder
571+
*/
572+
public fun <T> flag(
573+
name: String,
574+
aliases: Array<String> = emptyArray(),
575+
description: Description = Description.empty(),
576+
builderModifier: CommandFlag.Builder<C, Void>.() -> CommandFlag.Builder<C, T>
547577
): MutableCommandBuilder<C> = mutate {
548578
it.flag(
549579
this.commandManager
550580
.flagBuilder(name)
551581
.withAliases(*aliases)
552582
.withDescription(description)
553-
.withComponent(componentSupplier())
583+
.builderModifier()
554584
.build()
555585
)
556586
}

0 commit comments

Comments
 (0)