How to use exclusivity with @Flag?

I'm trying to figure out how to use the @Flag's exclusivity property.

I have the following:

struct CharacterCount: ParsableCommand {
  enum CharSet: String, CaseIterable {
    case whitespace
    case numbers
    case vowels
  @Argument(help: "String to count the characters of") var string: String
  @Flag(default: CharSet.whitespace, exclusivity: .exclusive, help: "") var characterSets: CharSet
  func run() throws {

What I'd like to do is to be able to pass in something like this to my tool:

CharacterCount Alice --whitespace --number

And have the tool only grab one. I know I can do this with exclusivity, but I can't figure out how to get it work. If I make characterSets an array, it doesn't compile anymore:

Cannot convert value of type 'Flag<CharacterCount.CharSet>' to specified type '[CharacterCount.CharSet]'

If I don't mark it as an array and pass more than one case, the tool crashes with:

Error: Value at position InputOrigin(_elements: Set([ArgumentParser.InputOrigin.Element.argumentIndex(2)])) has already been set from value at position InputOrigin(_elements: Set([ArgumentParser.InputOrigin.Element.argumentIndex(1)])).
Usage: character-count <string> [--whitespace] [--numbers] [--vowels]

I wasn't able to find the answer to this in the docs.

Aside from .exclusive, there are .chooseFirst, and .chooseLast which might be what you want.

Terms of Service

Privacy Policy

Cookie Policy