Picocli: Allow interactive options that echo to the console

Created on 5 Sep 2019  路  6Comments  路  Source: remkop/picocli

Hi, now interactive mode hide text digitized by user. There is a mode to use interactive mode which show text that user insert during write on console?

API enhancement

Most helpful comment

Note that you can work around this in the application, by calling Console.readLine:

public class Interactive implements Runnable {
    @Option(names = {"-u", "--user"}, interactive = true,
            description = "user id")
    private String user;

    public void run() {
        if (user == null) {
            user = new String(System.console().readLine("We really need a user id: "));
        }
        System.out.printf("Ok, we got: %s%n", user);
        // do stuff...
    }

    public static void main(String[] args) {
        int exitCode = new CommandLine(new Interactive()).execute(args);
    }
}

All 6 comments

Currently the picocli library does not provide this functionality, but I think it would be a good feature to have.

Perhaps we should add an echo attribute, that allows applications to control whether the user input is echo-ed to the console or not. I'm also thinking to add a prompt attribute so that applications can customize the text displayed to the end user for an interactive option. For example:

@Option(names = "-x", interactive = true, echo = true, prompt = "Enter your PIN: ")
String pin;

Note that you can work around this in the application, by calling Console.readLine:

public class Interactive implements Runnable {
    @Option(names = {"-u", "--user"}, interactive = true,
            description = "user id")
    private String user;

    public void run() {
        if (user == null) {
            user = new String(System.console().readLine("We really need a user id: "));
        }
        System.out.printf("Ok, we got: %s%n", user);
        // do stuff...
    }

    public static void main(String[] args) {
        int exitCode = new CommandLine(new Interactive()).execute(args);
    }
}

Note that you can work around this in the application, by calling Console.readLine:

public class Interactive implements Runnable {
    @Option(names = {"-u", "--user"}, interactive = true,
            description = "user id")
    private String user;

    public void run() {
        if (user == null) {
            user = new String(System.console().readLine("We really need a user id: "));
        }
        System.out.printf("Ok, we got: %s%n", user);
        // do stuff...
    }

    public static void main(String[] args) {
        int exitCode = new CommandLine(new Interactive()).execute(args);
    }
}

Then, the interactive flag is useless in this case and can be dropped, right?

@xinchao-zhang Yes, I think you are correct.

@remkop Hi, I'm interested in echo and prompt feature. Can I try to create a PR?

Yes, great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bdemers picture bdemers  路  5Comments

keturn picture keturn  路  3Comments

qw3ry picture qw3ry  路  7Comments

kalexmills picture kalexmills  路  8Comments

remkop picture remkop  路  7Comments