question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to configure a prompt with a default that comes from another option?

See original GitHub issue

I’m trying to setup a command that takes multiple sets of credentials (for different things) as options. All of them can be prompted for, but I would like the first username to be shown and used as default value for other username prompts.

class MyCommand : CliktCommand() {

    val githubUser by option("--github-user").prompt("Your GitHub username")

    val bintrayUser by option("--bintray-user").prompt("Your Bintray username", default = githubUser)
}

This obviously doesn’t work because githubUser doesn’t have a value at this time. Is there a way to provide a default with a lambda?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ajaltcommented, Nov 17, 2020

Jake is right, you can use defaultLazy here. option().prompt() just calls TermUi.prompt under the hood, so you can do the same:

class MyCommand : CliktCommand() {
    val githubUser by option("--github-user").prompt("Your GitHub username")
    val bintrayUser by option("--bintray-user").defaultLazy {
        TermUi.prompt("Your Bintray username", default = githubUser) ?: githubUser   
    }
}

There’s a caveat that default values (including prompt()) are evaluated in the order they’re defined, so make sure you’re only referencing properties defined earlier in the command. If you don’t want to rely on the definition order, you can call TermUi.prompt manually from run.

0reactions
joffrey-bioncommented, Nov 18, 2020

That makes sense. I was somehow too focused on the prompt() shorthand. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set a default app to prompt for a choice?
I would like to set the default for a file type to prompt me to choose which app I want to use. (I...
Read more >
Set default prompt set value dynamically based on value from ...
I would like to find a way to dynamically set the default value of one field in a prompt set based on the...
Read more >
Changing the default behavior of the 'Search and Select' Prompt
Option (1) is the default choice. There is no prompt control property available to set which option is applied by default.
Read more >
Creating a Click.Option with prompt that shows only if default ...
This can be done by over riding the click.Option.get_default() and the click.Option.prompt_for_value() methods like: Custom Class:.
Read more >
How to set Default to in Dashboard Prompt from presentation ...
Leave the second dashboard page's prompt "default" or if you want a value, set it to "Male" or "Female." When a selection is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found