Skip to content

Improve command aliasing in CommandRegistration.BaseBuilder #1170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 the original author or authors.
* Copyright 2022-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,7 @@
* Interface defining a command registration endpoint.
*
* @author Janne Valkealahti
* @author Andrey Litvitski
*/
public interface CommandRegistration {

Expand Down Expand Up @@ -764,6 +765,14 @@ public interface Builder {
*/
AliasSpec withAlias();

/**
* Define an alias what this command should execute
*
* @param commands the commands
* @return builder for chaining
*/
Builder withAlias(String... commands);

/**
* Define an exit code what this command should execute
*
Expand Down Expand Up @@ -1421,6 +1430,20 @@ public AliasSpec withAlias() {
return spec;
}

@Override
public Builder withAlias(String... commands) {
Assert.notNull(commands, "commands must be set");
DefaultAliasSpec spec = new DefaultAliasSpec(this);
spec.commands = Arrays.asList(commands).stream()
.flatMap(c -> Stream.of(c.split(" ")))
.filter(c -> StringUtils.hasText(c))
.map(c -> c.trim())
.collect(Collectors.toList())
.toArray(new String[0]);
this.aliasSpecs.add(spec);
return this;
}

@Override
public ExitCodeSpec withExitCode() {
DefaultExitCodeSpec spec = new DefaultExitCodeSpec(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,16 @@ public void testAliases() {
.command("alias2")
.group("Alias Group")
.and()
.withAlias("alias3")
.withTarget()
.function(function1)
.and()
.build();
assertThat(registration.getCommand()).isEqualTo("command1");
assertThat(registration.getGroup()).isEqualTo("Test Group");
assertThat(registration.getAliases()).hasSize(2);
assertThat(registration.getAliases()).hasSize(3);
assertThat(registration.getAliases().stream().map(CommandAlias::getCommand)).containsExactlyInAnyOrder("alias1",
"alias2");
"alias2", "alias3");
assertThat(registration.getAliases().get(0).getGroup()).isEqualTo("Alias Group");
assertThat(registration.getAliases().get(1).getGroup()).isEqualTo("Alias Group");
}
Expand Down