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

Parsing strategy .optional #307

Closed
msalmonse opened this issue May 12, 2021 · 12 comments
Closed

Parsing strategy .optional #307

msalmonse opened this issue May 12, 2021 · 12 comments

Comments

@msalmonse
Copy link

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.

@swiftyfinch
Copy link

Hi, I need something similar!
I use the option --focus and sometimes want to add some strings. Like --focus Alamofire.
But I can't use [String]? in the Option property wrapper.

I wonder maybe there is another way to get such behavior?

@msalmonse
Copy link
Author

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.

@swiftyfinch
Copy link

Sorry, I'm not sure that I understand you.
You can use Flag like 0 or 1 and Option like 0 or 1 as well. But also, Option can be an array. So it will be a collection with zero or more elements.

But I need the third state of Option:

Command                         | Swift
---------------------------------------------------------------------------------
rugby				| let focus: [String]? = nil
rugby --focus			| let focus: [String]? = []
rugby --focus Alamofire SnapKit | let focus: [String]? = ["Alamofire", "SnapKit"]

@msalmonse
Copy link
Author

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.

@swiftyfinch
Copy link

I found that there is init with initial: nil. But when I run my command its demands this argument.

public init<Element>(
name: NameSpecification = .long,
parsing parsingStrategy: ArrayParsingStrategy = .singleValue,
help: ArgumentHelp? = nil,
completion: CompletionKind? = nil
) where Element: ExpressibleByArgument, Value == Array<Element> {
self.init(
initial: nil,
name: name,
parsingStrategy: parsingStrategy,
help: help,
completion: completion
)
}

@swiftyfinch
Copy link

Also, as a workaround I can use rugby --focus none.
Then parse none and consider that focus is nil.

@natecook1000
Copy link
Member

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)
    }
}
$ example
nil
$ example --kind
.flagOnly
$ example --kind foo
.value(foo)

Still strikes me as a bit gross… Any other ideas?

@swiftyfinch
Copy link

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 see that need to change the array parsing strategy, but maybe there are some pitfalls.

@swiftyfinch swiftyfinch mentioned this issue May 20, 2021
4 tasks
@msalmonse
Copy link
Author

I found another way to do it

@Jomy10
Copy link

Jomy10 commented Mar 23, 2022

@msalmonse Mind sharing your solution? :)

@msalmonse
Copy link
Author

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.

@Jomy10
Copy link

Jomy10 commented Mar 23, 2022

@msalmonse Thank you for the reply!

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

4 participants