CaseIterable @Flag()'s and customs names

I'm guessing this undefined behaviour, but I'm interested to know how it should work.

Given this code:

enum AppType: String, ExpressibleByArgument, CaseIterable {
  case basic
  case utility
}

struct Example: ParsableCommand {
  
  @Option()
  var name: String

  @Flag(default: .basic, name: .customLong("app-type"), help: "The type of app to launch")
  var applicationType: AppType
}

The --help prints this:

OPTIONS:
  --application-name <application-name>
  --app-type/--app-type   The type of app to launch
                                   (default: basic)

Running the command with --basic or --utility gives an error. Specifying --app-type works and picks basic (the default).

Can this be turned in to a compile time error? Not easily I suspect, there is a chance this is/can be fixed with this PR.

Also, should it even be fixed?

This is solved in https://github.com/apple/swift-argument-parser/pull/118

My @Flag example did not have explicit tests but I have left the author a comment.

I'm working on a change to address the larger issue in this PR, which introduces an EnumerableFlag protocol with a customization hook for providing individual names for each case: https://github.com/apple/swift-argument-parser/pull/65

Terms of Service

Privacy Policy

Cookie Policy