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

allow option with a default the same as a subcommand name #1428

Closed
cbcmg opened this issue Sep 8, 2021 · 3 comments · Fixed by #1433
Closed

allow option with a default the same as a subcommand name #1428

cbcmg opened this issue Sep 8, 2021 · 3 comments · Fixed by #1433

Comments

@cbcmg
Copy link

cbcmg commented Sep 8, 2021

Hi, I would like to have an option available to all subcommands that happens to have a default value which is also the name of a subcommand. This currently fails due to the argSpec failing varargCanConsumeNextValue(). I'm not sure if this is by design or a limitation, or a bug. Here is a reproduction. Thanks

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.ParentCommand
import java.util.concurrent.Callable

@Command(
  name = "portal",
  subcommands = [AdminCmd::class],
  mixinStandardHelpOptions = true,
)
class PortalCmd : Callable<Int> {

  @Option(
    names = ["--username"],
    defaultValue = "admin",
    scope = CommandLine.ScopeType.INHERIT
  )
  var username: String? = null

  override fun call(): Int {
    return 0;
  }
}

@Command(
  name = "admin",
  mixinStandardHelpOptions = true,
)
class AdminCmd : Callable<Int> {
  @ParentCommand
  lateinit var portal: PortalCmd

  override fun call(): Int {
    println("username=${portal.username}")
    return 0;
  }
}

fun main(args: Array<String>) {
  CommandLine(PortalCmd()).execute("admin", "--username=test") // works!
  CommandLine(PortalCmd()).execute("admin", "--username", "test") // works!
  CommandLine(PortalCmd()).execute("admin", "--username=admin") // works!
  CommandLine(PortalCmd()).execute("admin", "--username", "admin") // works!
  CommandLine(PortalCmd()).execute("admin") // fails with "Expected parameter for option '--username' but found 'admin'"
}
@remkop
Copy link
Owner

remkop commented Sep 8, 2021

Hi @cbcmg, I believe this is a duplicate of #1125

Can you check if the workaround suggested in #1125 helps to alleviate the problem for you?
Basically, the idea is to give that option a parameterConsumer that takes care of assigning the value to the option.

Roughly, something like this:

class PortalCmd : Callable<Int> {

  @Option(
    names = ["--username"],
    parameterConsumer = MyParameterConsumer::class,
    defaultValue = "admin",
    scope = CommandLine.ScopeType.INHERIT
  )
  var username: String? = null
...
}

class MyParameterConsumer : IParameterConsumer {
    override fun consumeParameters(args: Stack<String>, argSpec: ArgSpec, commandSpec: CommandSpec) {
        if (args.isEmpty()) { // this check may be unnecessary
            throw ParameterException(commandSpec.commandLine(),
                    "Missing required parameter for option " + ((OptionSpec) argSpec).longestName());
        }
        argSpec.setValue(args.pop());
    }
}

@cbcmg
Copy link
Author

cbcmg commented Sep 8, 2021

Yep, that did the trick. Sorry for my terrible search skills. And thanks for a terrific library!!

@remkop
Copy link
Owner

remkop commented Sep 8, 2021

No worries.

Maybe this should be added to the documentation; I don’t see myself working on that issue in the near future…

Do you feel like contributing a PR to the documentation?

cbcmg pushed a commit to cbcmg/picocli that referenced this issue Sep 14, 2021
@remkop remkop closed this as completed Sep 28, 2021
MarkoMackic pushed a commit to MarkoMackic/picocli that referenced this issue Oct 17, 2021
MarkoMackic pushed a commit to MarkoMackic/picocli that referenced this issue Oct 17, 2021
MarkoMackic added a commit to MarkoMackic/picocli that referenced this issue Oct 17, 2021
…on subcommands and default values"

This reverts commit 9f37fb3.
MarkoMackic added a commit to MarkoMackic/picocli that referenced this issue Oct 17, 2021
…e name as a default value"

This reverts commit 1449834.
MarkoMackic added a commit to MarkoMackic/picocli that referenced this issue Oct 17, 2021
…on subcommands and default values"

This reverts commit 9f37fb3.
MarkoMackic added a commit to MarkoMackic/picocli that referenced this issue Oct 17, 2021
…e name as a default value"

This reverts commit 1449834.
remkop added a commit that referenced this issue Nov 4, 2021
…mary": causes javascript to hang in browser while rendering the page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants