Reusing an existing `ParsableArguments` as `@OptionGroup`, but changing required repeating argument to optional

I have a tool with many subcommands. Many of them reuse the same struct type that conforms to ParsableArguments as an @OptionGroup property. That ParsableArguments has multiple optional flags, plus a required repeating positional argument of type [String].

My tool has one command, however, that needs all the flags & the repeating positional argument from the ParsableArguments subtype, but the repeating positional argument is optional instead of required (i.e. it is zero-or-more instead of one-or-more).

Is there any way to easily reuse/extend where appropriate the same ParsableArguments for both circumstances?

Thanks.

Not exactly... but since option groups nest, you could instead set it up like this:

struct OptionsAndFlags: ParsableArguments {
    // ...
}

struct RequiredArgs: ParsableArguments {
    @OptionGroup var options: OptionsAndFlags
    @Argument var required: [String]
}

struct OptionalArgs: ParsableArguments {
    @OptionGroup var options: OptionsAndFlags
    @Argument var optional: [String] = []
}
1 Like

Thanks for the answer.