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

Shared Options/Arguments with Subcommands #54

Open
AvlWx2014 opened this issue Feb 8, 2021 · 2 comments
Open

Shared Options/Arguments with Subcommands #54

AvlWx2014 opened this issue Feb 8, 2021 · 2 comments

Comments

@AvlWx2014
Copy link

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:

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.

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.

@LepilkinaElena
Copy link

@AvlWx2014
Copy link
Author

Awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants