use of picocli.CommandLine.Model.CommandSpec in project keycloak by keycloak.
the class Picocli method addMappedOptionsToArgGroups.
private static void addMappedOptionsToArgGroups(CommandSpec cSpec, List<PropertyMapper> propertyMappers) {
for (ConfigCategory category : ConfigCategory.values()) {
List<PropertyMapper> mappersInCategory = propertyMappers.stream().filter(m -> category.equals(m.getCategory())).collect(Collectors.toList());
if (mappersInCategory.isEmpty()) {
// picocli raises an exception when an ArgGroup is empty, so ignore it when no mappings found for a category.
continue;
}
ArgGroupSpec.Builder argGroupBuilder = ArgGroupSpec.builder().heading(category.getHeading() + ":").order(category.getOrder()).validate(false);
for (PropertyMapper mapper : mappersInCategory) {
String name = mapper.getCliFormat();
String description = mapper.getDescription();
if (description == null || cSpec.optionsMap().containsKey(name) || name.endsWith(OPTION_PART_SEPARATOR)) {
// when key is already added or has no description, don't add.
continue;
}
String defaultValue = mapper.getDefaultValue();
Iterable<String> expectedValues = mapper.getExpectedValues();
argGroupBuilder.addArg(OptionSpec.builder(name).defaultValue(defaultValue).description(description).paramLabel(mapper.getParamLabel()).completionCandidates(expectedValues).parameterConsumer(PropertyMapperParameterConsumer.INSTANCE).type(String.class).hidden(mapper.isHidden()).build());
}
cSpec.addArgGroup(argGroupBuilder.build());
}
}
use of picocli.CommandLine.Model.CommandSpec in project keycloak by keycloak.
the class Picocli method createCommandLine.
public static CommandLine createCommandLine(List<String> cliArgs) {
CommandSpec spec = CommandSpec.forAnnotatedObject(new Main(), new DefaultFactory()).name(Environment.getCommand());
for (CommandLine subCommand : spec.subcommands().values()) {
CommandSpec subCommandSpec = subCommand.getCommandSpec();
// help option added to any subcommand
subCommandSpec.addOption(OptionSpec.builder(Help.OPTION_NAMES).usageHelp(true).description("This help message.").build());
}
addOption(spec, Start.NAME, hasAutoBuildOption(cliArgs), true);
addOption(spec, StartDev.NAME, true, true);
addOption(spec, Build.NAME, true, hasAutoBuildOption(cliArgs));
CommandLine cmd = new CommandLine(spec);
cmd.setExecutionExceptionHandler(new ExecutionExceptionHandler());
cmd.setParameterExceptionHandler(new ShortErrorMessageHandler());
cmd.setHelpFactory(new HelpFactory());
cmd.getHelpSectionMap().put(SECTION_KEY_COMMAND_LIST, new SubCommandListRenderer());
cmd.setErr(new PrintWriter(System.err, true));
return cmd;
}
use of picocli.CommandLine.Model.CommandSpec in project keycloak by keycloak.
the class Picocli method addOption.
private static void addOption(CommandSpec spec, String command, boolean includeBuildTime, boolean includeRuntime) {
CommandSpec commandSpec = spec.subcommands().get(command).getCommandSpec();
List<PropertyMapper> mappers = new ArrayList<>();
if (includeRuntime) {
mappers.addAll(PropertyMappers.getRuntimeMappers());
}
if (includeBuildTime) {
mappers.addAll(PropertyMappers.getBuildTimeMappers());
}
addMappedOptionsToArgGroups(commandSpec, mappers);
}
use of picocli.CommandLine.Model.CommandSpec in project keycloak by keycloak.
the class SubCommandListRenderer method render.
@Override
public String render(Help help) {
CommandSpec spec = help.commandSpec();
if (spec.subcommands().isEmpty()) {
return "";
}
Column commands = new Column(24, 2, Column.Overflow.SPAN);
Column descriptions = new Column(spec.usageMessage().width() - 24, 2, Column.Overflow.WRAP);
TextTable textTable = TextTable.forColumns(help.colorScheme(), commands, descriptions);
textTable.setAdjustLineBreaksForWideCJKCharacters(spec.usageMessage().adjustLineBreaksForWideCJKCharacters());
addHierarchy(spec.subcommands().values(), textTable, "");
return textTable.toString();
}
use of picocli.CommandLine.Model.CommandSpec in project aion by aionnetwork.
the class CliTest method testAccountCliParseAndRun.
@Parameters(method = "parametersFortestAccountCliParseAndRun")
@Test
public void testAccountCliParseAndRun(String[] options, ReturnType expected) {
CommandLine commandLine = new CommandLine(Arguments.class).addSubcommand(EditCli.class);
ParseResult result;
try {
result = commandLine.parseArgs(options);
} catch (Exception e) {
if (expected.equals(ERROR)) {
return;
} else {
throw e;
}
}
CommandSpec commandSpec = Cli.findCommandSpec(result, AccountCli.class);
// noinspection ConstantConditions
AccountCli spiedAccountCLI = spy((AccountCli) commandSpec.userObject());
doReturn(true).when(spiedAccountCLI).exportPrivateKey(anyString(), any());
doReturn(true).when(spiedAccountCLI).listAccounts();
doReturn(true).when(spiedAccountCLI).createAccount(any());
doReturn(true).when(spiedAccountCLI).importPrivateKey(anyString(), any());
doCallRealMethod().when(spiedAccountCLI).runCommand(any());
assertThat(spiedAccountCLI.runCommand(mockpr)).isEqualTo(expected);
if (spiedAccountCLI.getList()) {
verify(spiedAccountCLI, times(1)).listAccounts();
} else {
verify(spiedAccountCLI, times(0)).listAccounts();
}
if (!spiedAccountCLI.getGroup().getImportAcc().isEmpty()) {
verify(spiedAccountCLI, times(1)).importPrivateKey(anyString(), any());
} else {
verify(spiedAccountCLI, times(0)).importPrivateKey(anyString(), any());
}
if (!spiedAccountCLI.getGroup().getExport().isEmpty()) {
verify(spiedAccountCLI, times(1)).exportPrivateKey(anyString(), any());
} else {
verify(spiedAccountCLI, times(0)).exportPrivateKey(anyString(), any());
}
if (spiedAccountCLI.getGroup().getCreate()) {
verify(spiedAccountCLI, times(1)).createAccount(any());
} else {
verify(spiedAccountCLI, times(0)).createAccount(any());
}
Mockito.clearInvocations(spiedAccountCLI);
}
Aggregations