Picocli: Interactive parameter asks for value to be entered at once

Created on 18 Oct 2019  路  8Comments  路  Source: remkop/picocli

I have the following Main class with subcommands:

@Command(description = "MyTools for testing",
    name = "MyTools",
    mixinStandardHelpOptions = true,
    usageHelpWidth = 120,
    version = "MyTools 1.0",

    subcommands = {
        //some other commands
        BCryptPasswordCommand.class
    }
)
public class Main implements Callable<Void> {

    @CommandLine.Option(names = {"--verbose"}, description = "to produce more information, especially for error debugging", defaultValue = "false")
    private boolean verbose;

    @Override
    public Void call() {
        System.err.println("Missing a sub-command specification");
        new CommandLine(new Main()).usage(System.err);
        System.exit(1);
        return null;
    }

    public static void main(String[] args) {
        Main main = new Main();
        try {
            //CommandLine.call(main, args);
            new CommandLine(main).execute(args);
        } catch (Exception e) {
            // print exception
        }
    }
}

and the BCryptPasswordCommand:

@CommandLine.Command(
    description = "BCrypt the password",
    name = "encodePwd")
public class BCryptPasswordCommand implements Callable<Void> {
    @CommandLine.Parameters(index = "0", interactive = true, description = "Password to encrypt")
    private String password;

    @Override
    public Void call() throws Exception {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(password);
        CliUtil.printCliInfoMessage(encodedPassword);
        return null;
    }
}

WHAT I DO:
i run java -jar myTools.jar encodePwd
CURRENT BEHAVIOR:
i'm not offered to enter password, the code just runs with null password.
EXPECTED:
i'm offered to enter password

It's interesting that when i do java -jar myTools.jar encodePwd blabla, i'm offered to enter the password interactively, but then get: Unmatched argument at index 1: 'blabla', so it looks like it takes both arguments into account: "blabla" and the one entered interectively after.

VERSION: the same for 3.9.5 and 4.0.4

All 8 comments

Thank you for raising this!
I鈥檒l take a look when I get to my pc.

I suspect that it may be difficult to make positional parameters work well interactively.

I suggest changing the BCryptPasswordCommand command to remove the password positional parameter and call Console.readPassword directly in the call method directly.

So, something like this:

import picocli.CommandLine.Command;

@Command(name = "encodePwd",
        header = "Prompts for a password and BCrypts it",
        description = "Prompts for a password, BCrypts it, and prints the result, together with other CLI info")
public class BCryptPasswordCommand implements Callable<Void> {

    @Override
    public Void call() throws Exception {
        char[] password = System.console().readPassword("Password to encrypt");
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(password);
        CliUtil.printCliInfoMessage(encodedPassword);
        Arrays.fill(password, '*');
        return null;
    }
}

@remkop thank you very much for your reply.
Yes, i know that it can be achieved this way. But then the question is what is interactive option in @CommandLine.Parameters for? Plus the docs say "Picocli 3.5 introduced password support: for options and positional parameters marked as interactive, the user is prompted to enter a value on the console"

Yes, you are right. It was certainly my intention for this to work for both options and positional parameters, and I have some tests that verify this, but the current parser logic doesn鈥檛 cover your use case. It basically only works if there鈥檚 something following the interactive positional parameter on the command line. This limits the usefulness of this feature...

I added a cautionary note to the user manual: https://picocli.info/#_interactive_password_options

thank you for help!

If you like picocli, please star the project on GitHub and tell your friends. :-)

Was this page helpful?
0 / 5 - 0 ratings