Kotlin / Kotlin/kotlinx-cli

`default` should be evaluated lazily(by passing a provider function, for example)

Open
#69 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
951
Forks
71
PR merge metrics
No merged PRs in 30d

Description

Reffering to
- kotlinx.cli.OptionsKt#default(kotlinx.cli.SingleNullableOption, T)
- kotlinx.cli.OptionsKt#default(kotlinx.cli.MultipleOption, java.util.Collection)

Example Use case - in order to resolve an option with a value that could be set from an enviroment variable, kotlinxcli will first need to get the already resolved value of the env var, and only then search the value inside args.
It means that:
- the env var should always be set and with a valid value(which is wrong if we think about multiple ways to set a single option),
- or that resolving against the existence of an env var should happen after the parse had finished.
```kotlin
Optional.ofNullable(maybeSomeOptionThatCouldBeFromEnvVar)
.or { resolveTheEnvVarAndReturnNullIfNotFoud() }
.orElseThrow { Exception("SomeOptionThatCouldBeFromEnvVar is required") }
```

If the default value would have been a provider function that only evaluates when the parser finds that the actual value is missing, it would be much easier to use and goes along side the approach we normaly see in kotlin(for example, a logger.info that accepts a function that returns a string).

There are a lot of cases where the desired behaviour is to compute the default value only if the user did not input anything specific and it seems pretty straight forward to implement inside this lib.

Hope that makes sense, cheers for all your hard and awesome work!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the kotlinx.cli.OptionsKt#default overloads named in the issue and trace how defaults are handled during parsing. Define the provider-function API and ensure it is evaluated only when no value was supplied, including the single and multiple option cases. Done means environment-dependent defaults can be resolved after checking command-line input.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.