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

Ability for an input to work as both a @Flag and an @Option. #337

Open
daltonclaybrook opened this issue Jul 9, 2021 · 2 comments
Open

Comments

@daltonclaybrook
Copy link

daltonclaybrook commented Jul 9, 2021

I'd like the ability for an input to be able to work as both a Flag and an Option in my CLI. For example, I would want both of the following examples to be possible:

$ example --build
# and
$ example --build Foo Bar

The first case would parse to build: [] and the second case to build: ["Foo", "Bar"]. I tried making this work in two ways. First I tried making an @Option with a type of [String]? = nil and using a custom transform closure. My assumption was that passing --build without a value would pass an empty string to the transform closure. Instead, the CLI emits the following error when running:

Error: Missing value for '--build <build>'

The second strategy I tried was creating two separate fields, making one a @Flag and one an @Option, and giving them the same name. This time, I got an assertion failure:

ArgumentParser/ParsableArguments.swift:243: Fatal error: Validation failed for `Example`:
- Multiple (2) `Option` or `Flag` arguments are named "--build".

The first strategy—the one that uses only a single @Option field—seems the most ergonomic to me. I could also picture using a new initializer parameter to @Option e.g. allowsMissingValue: Bool, etc. My apologies if this is already possible and I just didn't dig hard enough.

@kylemacomber
Copy link
Contributor

It looks like this is the same issue as #307 and there's a PR up for it at #317, though I think @natecook1000 is still making up his mind about whether this feature is safe to add or not

@daltonclaybrook
Copy link
Author

@kylemacomber Ah, got it! Feel free to close this issue if you think it is a duplicate.

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