Running picocli from Windows command-line, not presenting subcommand output

54 Views Asked by At

I have a Java application I've wrapped with picocli. Works fine from the IDE (Eclipse) When running it from the Windows command-line (java -cp . . .) The sub command runs successfully but no output appears. All prints are with System.out.println()

Because I have 2 commands, I've split it to two different classes. So I have Main class that in it's Main method parse the args and then runs the execute method of the relevant command class. The subcommand classes implement the void run() method. No code in the Main method. Total of 3 classes.

I've found picocli issues in 'stackoverflow' they are not my issue. Any ideas? Thanks.

Tried to change to System.err.println() with no output. And of course searched in the Documentation. ChatGPT also didn't help

Editing: Here is an example of my code: BTW I just noticed that the help of the subcommand is the only output that is been printed, also in the command line.

Main class:

package picocli;

import picocli.CommandLine.Command;
import picocli.CommandLine.ParameterException;

@Command(name = "vault-cli", mixinStandardHelpOptions = true, version = "1.0", subcommands = { TestCommand.class,
        ConfigCommand.class })
public class VaultCli implements Runnable {

    public static void main(String[] args) {
        CommandLine commandLine = new CommandLine(new VaultCli());
        int exitCode = 0;
        try {
            commandLine.parseArgs(args);
            if (commandLine.getParseResult().subcommands().isEmpty() && args.length == 0) {
                // Show usage help if no subcommands or options are provided
                commandLine.usage(System.out);
                exitCode = -1;

            } else {
                // Execute the command with the provided arguments
                exitCode = commandLine.execute(args);
            }
            

        } catch (ParameterException e) {
            // Show the usage message for the subcommand that caused the exception
            CommandLine subcommandLine = e.getCommandLine();
            if (subcommandLine != null) {
                subcommandLine.usage(System.out);
            } else {
                // If no specific subcommand caused the exception, show the main usage
                System.out.println(e.getMessage());
                commandLine.usage(System.out);
                System.exit(exitCode);
            }
        } finally {
            System.err.println("exit code: " + exitCode);
            System.exit(exitCode);
        }
    }

    @Override
    public void run() {
    }
}

Subcommands:

package picocli;

import java.util.concurrent.Callable;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "test", mixinStandardHelpOptions = true, version = "checksum 4.0",
description = "Tests URL with GET method")
class TestCommand implements Callable<Integer> {
    
    @Option(names = {"-u", "--url"}, description = "https://<hostname>/url")
    private String url = "";

    @CommandLine.Spec
    CommandLine.Model.CommandSpec spec;
    
    @Override
    public Integer call() throws Exception {
        
        // Show TestCommand help
        if (spec.commandLine().isUsageHelpRequested()) {
            spec.commandLine().usage(System.out);
            return 0;
        }
        
        System.out.println("url : " + url);
        System.out.println("running");
        CloseableHttpClient client = HttpClientBuilder.create().build();
        int statusCode = -1;
        try {
            HttpGet get = new HttpGet(url);
            CloseableHttpResponse response = client.execute(get);
            statusCode = response.getStatusLine().getStatusCode();
            System.err.println(statusCode);
            System.err.println(response.getStatusLine().getReasonPhrase());
            if (response != null && response.getEntity() != null) {
                String responseAsString = EntityUtils.toString(response.getEntity());
                System.out.println(responseAsString);
                return 100;
            } else {
                return statusCode;
            }
        } catch (Exception e) {
            
        }
        System.out.println("End");
        return 22;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new TestCommand()).execute(args);
        System.exit(exitCode);
    }
}
package picocli;

import java.util.concurrent.Callable;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "config", mixinStandardHelpOptions = true, version = "vault-cli 1.0",
description = "config is built for running and testing communication with the Vault Provider")
        
class ConfigCommand implements Callable<Integer> {

    @Option(names = {"-action"}, description = "display|update|delete")
    private String action = "";
    @Option(names = {"-parameter"}, description = "<parameter>")
    private String parameter = "";
    
    @CommandLine.Spec
    CommandLine.Model.CommandSpec spec;

    
    // Show ConfigCommand help
    @Override
    public Integer call() throws Exception {
        if (spec.commandLine().isUsageHelpRequested()) {
            spec.commandLine().usage(System.out);
            return 0;
        }
        String getenv = System.getenv(parameter);
        if (getenv != null) {
            System.out.println(getenv);
            System.out.println(0);
            return 0;
        } else {
            System.out.println("No such var " + parameter);
            System.out.println(-1);
            return -1;
        }
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new ConfigCommand()).execute(args);
        System.exit(exitCode);
    }
}
0

There are 0 best solutions below