Skip to content

Feature request: ability to specify required() after check() #620

@pgarrett-twc

Description

@pgarrett-twc

Hi, thanks for a great library.

In our app we have a library of options, where we specify types, help text, and validation rules for objects in our domain, like this contrived example:

class Options(private val command: ParameterHolder) {
  val widgetId get() =
      command.option("--widget-id", help="Widget ID as integer")
          .int()
          .check("Widget ID must be positive") { it > 0 }
}

And then in each command, we include the options that are relevant to it, like:

class UpdateWidgetCommand : CliktCommand() {
  private val options = Options(this)
  val widgetId by options.widgetId
  ...
}

The problem I have is that in some commands widgetId is required but in others it is optional. So I'd like to be able to do something like this:

  val widgetId by options.widgetId             // this is an Int?
  // or
  val widgetId by options.widgetId.required()  // this would be an Int. Doesn't compile.

The required() method is only available on NullableOption, not OptionDelegate<Int?>, so this doesn't work currently.

Thanks for considering this request. And of course, I'd be happy to know if there's already a way to do it that I missed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions