Repository: apache/kafka
File: tools/src/main/java/org/apache/kafka/tools/streams/StreamsGroupCommand.java (Java)

Code before:

    public static int execute(String[] args) {
        StreamsGroupCommandOptions opts = null;
        int exitCode = 0;
        try {
            opts = new StreamsGroupCommandOptions(args);
            opts.checkArgs();
            // should have exactly one action
            long numberOfActions = Stream.of(
                opts.listOpt,
                opts.describeOpt,
                opts.resetOffsetsOpt,
                opts.deleteOpt,
                opts.deleteOffsetsOpt
            ).filter(opts.options::has).count();
            if (numberOfActions != 1)
                throw new IllegalArgumentException("Command must include exactly one action: --list, --describe, --delete, --reset-offsets, or --delete-offsets.");

            run(opts);
        } catch (IllegalArgumentException | OptionException e) {
            System.err.println(e.getMessage());
            if (opts != null) {
                try {
                    opts.parser.printHelpOn(System.err);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
            exitCode = 1;
        } catch (Throwable e) {
            printError("Executing streams group command failed due to " + e.getMessage(), Optional.of(e));
            exitCode = 1;
        }

        return exitCode;
    }

    public static void run(StreamsGroupCommandOptions opts) throws ExecutionException, InterruptedException {
        try (StreamsGroupService streamsGroupService = new StreamsGroupService(opts, Map.of())) {
            if (opts.options.has(opts.listOpt)) {
                streamsGroupService.listGroups();
            } else if (opts.options.has(opts.describeOpt)) {
                streamsGroupService.describeGroups();
            } else if (opts.options.has(opts.resetOffsetsOpt)) {
                Map<String, Map<TopicPartition, OffsetAndMetadata>> offsetsToReset = streamsGroupService.resetOffsets();
                if (opts.options.has(opts.exportOpt)) {
                    String exported = streamsGroupService.exportOffsetsToCsv(offsetsToReset);
                    System.out.println(exported);
                } else
                    printOffsetsToReset(offsetsToReset);
            } else if (opts.options.has(opts.deleteOpt)) {

Review comment (suggestion):
I don't think we should re-throw here, but only print the stack-trace? If we re-throw here, we won't set `exitcode = 1`, and this exception is not called anywhere, but bubble out of `main()` and we don't call `Exit.exit(execute(args));` as intended.

Code after:

    public static int execute(String[] args) {
        StreamsGroupCommandOptions opts = null;
        int exitCode = 0;
        try {
            opts = new StreamsGroupCommandOptions(args);
            opts.checkArgs();
            // should have exactly one action
            long numberOfActions = Stream.of(
                opts.listOpt,
                opts.describeOpt,
                opts.resetOffsetsOpt,
                opts.deleteOpt,
                opts.deleteOffsetsOpt
            ).filter(opts.options::has).count();
            if (numberOfActions != 1)
                throw new IllegalArgumentException("Command must include exactly one action: --list, --describe, --delete, --reset-offsets, or --delete-offsets.");

            run(opts);
        } catch (IllegalArgumentException | OptionException e) {
            System.err.println(e.getMessage());
            if (opts != null) {
                try {
                    opts.parser.printHelpOn(System.err);
                } catch (IOException ex) {
                    printError(e.getMessage(), Optional.of(ex));
                }
            }
            exitCode = 1;
        } catch (Throwable e) {
            printError("Executing streams group command failed due to " + e.getMessage(), Optional.of(e));
            exitCode = 1;
        }

        return exitCode;
    }

    public static void run(StreamsGroupCommandOptions opts) throws ExecutionException, InterruptedException {
        try (StreamsGroupService streamsGroupService = new StreamsGroupService(opts, Map.of())) {
            if (opts.options.has(opts.listOpt)) {
                streamsGroupService.listGroups();
            } else if (opts.options.has(opts.describeOpt)) {
                streamsGroupService.describeGroups();
            } else if (opts.options.has(opts.resetOffsetsOpt)) {
                Map<String, Map<TopicPartition, OffsetAndMetadata>> offsetsToReset = streamsGroupService.resetOffsets();
                if (opts.options.has(opts.exportOpt)) {
                    String exported = streamsGroupService.exportOffsetsToCsv(offsetsToReset);
                    System.out.println(exported);
                } else
                    printOffsetsToReset(offsetsToReset);
            } else if (opts.options.has(opts.deleteOpt)) {