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
Parsing strategy .optional #307
Comments
Hi, I need something similar! I wonder maybe there is another way to get such behavior? |
After giving it some thought I think that rather than flag and option a minimum and maximum number of arguments is more flexible. A flag is then 0…0, an option is 1…1, what I want is 0…1. |
Sorry, I'm not sure that I understand you. But I need the third state of Command | Swift
---------------------------------------------------------------------------------
rugby | let focus: [String]? = nil
rugby --focus | let focus: [String]? = []
rugby --focus Alamofire SnapKit | let focus: [String]? = ["Alamofire", "SnapKit"] |
Not 0 or 1 but 0 to 1, it is a closed range of the number of arguments allowed, a flag has none or 0…0, an option 1…1 or 1… depending on your parsing strategy in the current setup. |
I found that there is swift-argument-parser/Sources/ArgumentParser/Parsable Properties/Option.swift Lines 496 to 509 in 992a745
|
Also, as a workaround I can use |
This isn't currently supported, mostly because there isn't a great way to model it cleanly. It might make sense as a double Optional, but those are awkward to work with. Maybe we could add an Optional-esque type: struct Example: ParsableCommand {
@Option var kind: OptionValue<String>?
func run() {
print(kind as Any)
}
}
Still strikes me as a bit gross… Any other ideas? |
Hey, @natecook1000 why we can use it like this: @Option var kind: [String]? Like it already works with non-collection types. I can try to make a draft if it suits. |
I found another way to do it |
@msalmonse Mind sharing your solution? :) |
I wrote my own parser with a min and max count for parameters. I fixed the negative numbers problem as well and threw in environmental variables. SAP is very good but… I remember a comparison of Windows with Linux: in Windows the easy is easy and the difficult is impossible while in Linux it's all just hard. |
@msalmonse Thank you for the reply! |
I wanted to add a
—draft
option to my command but I wanted to be able to optionally specify the draft text i.e.—draft
or—draft Entwurf
. Unfortunately I couldn't think of a way to handle it.I think that there is a case for having optional arrays although I think there that specifying a minimum and maximum number of elements would be better.
I don't think that there is a good way of handling this with standard types so maybe a custom type should be a requirement, one that could take an optional value as an argument.
The text was updated successfully, but these errors were encountered: