swift-argument-parser
swift-argument-parser copied to clipboard
Add a .implicit name specification, for @Option initializers
Properties wrapped by @Option with a .implicit name specification would be handled like those with @Argument and would allow command line tools like this one (inspired from sed).
import ArgumentParser
struct MySed: ParsableCommand {
// my-sed 's/hello/world/' input.txt
// my-sed -e 's/hello/world/' input.txt
// my-sed --expression='s/hello/world/' input.txt
@Option(name: [.implicit, .short, .long])
var expression: [String]
@Argument()
var input: [String]
func run() throws {
print("expression: \(expression)")
print("input: \(input)")
}
}
MySed.main()
Help would show properties wrapped by @Option with an .implicit name specification into brackets
[[--expression] <expression>]
They would, like @Argument, be order dependent and face identical limitations when used on array properties.
Only one .implicit would be allowed by property (even on array properties)... in order to let simple in/out command line tools like
inout [-i] <inputFile> [-o] <outputFile>
process two files without ambiguity.
How would this approach handle multiple implicit arguments (e.g. --input | $1 / --output | $2)?
Could this behavior be delegated to ArgumentSet instead by saying that an --expression option and positional argument constitute an argument set, rather than the expression option having an implicit label?
The parsing always extracts labeled values (options and flags) first, and treats the remaining inputs as positional arguments, so I believe you'd be able to call something with two implicit/named options as any of these:
$ example infile outfile
$ example infile --output outfile
$ example --input infile outfile
$ example outfile --input infile
$ example --output outfile infile
$ example --input infile --output outfile
$ example --output outfile --input infile