Skip to content

Commit

Permalink
uncrecognizedArgs handling
Browse files Browse the repository at this point in the history
Signed-off-by: Václav Muzikář <[email protected]>
  • Loading branch information
vmuzikar committed Nov 19, 2024
1 parent 817d6f5 commit fff8ffe
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,11 @@ private static class IncludeOptions {
}

private ExecutionExceptionHandler errorHandler = new ExecutionExceptionHandler();
private List<String> uncrecognizedArgs = new ArrayList<>();

public void parseAndRun(List<String> cliArgs) {
// perform two passes over the cli args. First without option validation to determine the current command, then with option validation enabled
CommandLine cmd = createCommandLine(spec -> spec
.addUnmatchedArgsBinding(CommandLine.Model.UnmatchedArgsBinding.forStringArrayConsumer(new ISetter() {
@Override
public <T> T set(T value) throws Exception {
return null; // just ignore
}
})));
CommandLine cmd = createCommandLine(spec -> {}).setUnmatchedArgumentsAllowed(true);
String[] argArray = cliArgs.toArray(new String[0]);

try {
Expand Down Expand Up @@ -157,6 +152,16 @@ private CommandLine createCommandLineForCommand(List<String> cliArgs, List<Comma

currentSpec = subCommand.getCommandSpec();

currentSpec.addUnmatchedArgsBinding(CommandLine.Model.UnmatchedArgsBinding.forStringArrayConsumer(new ISetter() {
@Override
public <T> T set(T value) {
if (value != null) {
uncrecognizedArgs.addAll(Arrays.asList((String[]) value));
}
return null; // doesn't matter
}
}));

addHelp(currentSpec);
}

Expand Down Expand Up @@ -324,6 +329,10 @@ private static boolean wasBuildEverRun() {
* @param abstractCommand
*/
public void validateConfig(List<String> cliArgs, AbstractCommand abstractCommand) {
if (!uncrecognizedArgs.isEmpty()) {
throw new KcUnmatchedArgumentException(abstractCommand.getCommandLine().orElseThrow(), uncrecognizedArgs);
}

if (cliArgs.contains(OPTIMIZED_BUILD_OPTION_LONG) && !wasBuildEverRun()) {
throw new PropertyException(Messages.optimizedUsedForFirstStartup());
}
Expand Down

0 comments on commit fff8ffe

Please sign in to comment.