Kotlin / Kotlin/kotlinx-cli

Shared Options/Arguments with Subcommands

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

Description

Is it possible with the current state of the API to share options/arguments with subcommands?

Example:
I have two subcommands, which process either a) a single file or b) a set of files contained in multiple directories.

Naturally I created two subcommands:
```kotlin

class Single(private val runner: Runner) : Subcommand("single", "Process a single file") {
private val file by argument(
type = ArgType.String,
fullName = "file",
description = "Process the given file."
)

override fun execute() {
runner.single(File(file))
}
}

class Multiple(private val runner: Runner) : Subcommand("multiple", "Process all files in the given directories") {
private val directories by argument(
type = ArgType.String,
fullName = "directories",
description = "Process all files in the given directory or directories."
).vararg()

override fun execute() {
runner.multiple(directories.map(::File))
}
}
```

What I'd like to do is have both subcommands be able to access a shared `-o --output` option to specify the output directory.

```kotlin
private val output by option(
type = ArgType.String,
shortName = "o", fullName = "output",
description = "Specify the output directory"
).default(".")
```

At the moment it seems like I have to duplicate this in each subcommand for this to work.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the Subcommand API and the option/argument declarations shown in the issue. Determine how a shared -o/--output option could be exposed to both Single and Multiple without duplicating it. Done means both subcommands can access the option with its default value while retaining their existing file and directory arguments.

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.