clikt icon indicating copy to clipboard operation
clikt copied to clipboard

Enum default value does not use key lambda when using showDefaultValues

Open BoD opened this issue 11 months ago • 1 comments

I have this option:

  enum class LogLevel {
    DEBUG,
    INFO,
    WARN,
    NONE,
  }

  val logLevel: LogLevel by option(
    "-l",
    "--log-level",
    help = "Set the log level",
  )
    .enum<LogLevel> { it.name.lowercase() }
    .default(LogLevel.INFO)

When using MordantHelpFormatter(it, showDefaultValues = true), the help message is rendered as:

  -l, --log-level=(debug|info|warn|none)
                                 Set the log level (default: INFO)

Ideally, it should be:

  -l, --log-level=(debug|info|warn|none)
                                 Set the log level (default: info)

BoD avatar Mar 08 '25 17:03 BoD

You'll have to do .default(LogLevel.INFO, "info"). Not ideal, but the keys lambda you pass to enum isn't stored anywhere, so there's no way to call it with the default value. To implement that, we'd have to add some valueToString function that gets stored on Options.

ajalt avatar Mar 08 '25 20:03 ajalt