Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs for interactive options (was:Input on interactive flag not working on Windows cmd) #1140

Closed
smalirizvi opened this issue Aug 5, 2020 · 4 comments
Labels
theme: integration An issue or change related to integration with other frameworks, shells or operating systems theme: shell An issue or change related to interactive (JLine) applications type: doc 📘
Milestone

Comments

@smalirizvi
Copy link

Input is not being taken in on interactive flag set to true when entering input on windows cmd.

@remkop
Copy link
Owner

remkop commented Aug 6, 2020

Are you using jline2?
Is this issue perhaps similar to this one: #767

@remkop remkop added theme: integration An issue or change related to integration with other frameworks, shells or operating systems theme: shell An issue or change related to interactive (JLine) applications labels Aug 6, 2020
@remkop
Copy link
Owner

remkop commented Aug 11, 2020

@smalirizvi, are you using JLine 2?

If so, there is a workaround: use a custom parameter consumer that uses the JLine 2 API to read a password interactively from the console.

The picocli-shell-jline2 README has an example: see the InteractiveParameterConsumer class in the README.

@remkop
Copy link
Owner

remkop commented Sep 15, 2020

@smalirizvi Can you provide more information on how to reproduce this issue?

@remkop
Copy link
Owner

remkop commented Feb 21, 2022

I am going to assume that you expected picocli to prompt the user for input when the interactive option is not specified when the program is invoked.
Instead, picocli only prompts the user when the interactive option is specified without parameter:

$ myprogram                             # option not specified: no prompting
You provided value 'null'

$ myprogram --interactive-option=abc    # option specified with parameter: no prompting
You provided value 'abc'

$ myprogram --interactive-option        # option specified WITHOUT parameter: prompt for input
Enter value for --interactive-option (...):    #  <--- type xxx and hit Enter
You provided value 'xxx'

Applications that also need the user to be prompted when the option is not specified, need to do this in the business logic.
For example:

@Command
public class Main implements Runnable {
    @Option(names = "--interactive", interactive = true)
    String value;

    public void run() {
        if (value == null) {
            // alternatively, use Console::readPassword
            value = System.console().readLine("Enter value for --interactive: ");
        }
        System.out.println("You provided value '" + value + "'");
    }

    public static void main(String[] args) {
        new CommandLine(new Main()).execute(args);
    }
}

I believe this question has come up before, so I will add this example to the user manual.

@remkop remkop added this to the 4.7 milestone Feb 21, 2022
@remkop remkop changed the title Input on interactive flag not working on Windows cmd. Improve docs for interactive options (was:Input on interactive flag not working on Windows cmd) Feb 21, 2022
@remkop remkop closed this as completed in 5f04e37 Feb 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: integration An issue or change related to integration with other frameworks, shells or operating systems theme: shell An issue or change related to interactive (JLine) applications type: doc 📘
Projects
None yet
Development

No branches or pull requests

2 participants