Context
I am developing a tool using ArgumentParser to fetch data from a public API. The API and Swift tool works like so:
command
Returns entire API dataset from api.example.comcommand --option1 val1
Returns a narrower dataset from api.example.com/val1command --option1 val1 --option2 val2
Returns an even narrower set of data from api.example.com/val1/val2command --option1 val1 --option2 val2 --option3 val3
Returns a single data item from api.example.com/val1/val2/val3
So, option2 requires option1, and option3 requires option2. Here is a generalized example of my code:
struct MyOptions: ParsableArguments {
@Option option1: Type? = nil
@Option option2: Type? = nil
@Option option3: Type? = nil
}
struct MyCommand: ParsableCommand {
@OptionGroup var options: MyOptions
mutating func run() throws {}
}
Questions
For handling checking for allowed option combinations, I was planning to have some logic in the run()
function to check when constructing the API's URL path. But it seems like this logic should be moved to a custom validate()
? Also, more of a general swift question, but is there a way to utilize the default validate() implementation in my own? That way I just handle the mutual dependencies and don’t need to implement the basic argument checking. It seems super
is only allowed in Swift classes.