swift-argument-parser icon indicating copy to clipboard operation
swift-argument-parser copied to clipboard

Add a .implicit name specification, for @Option initializers

Open fblondiau opened this issue 5 years ago • 2 comments

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.

fblondiau avatar Mar 23 '20 11:03 fblondiau

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?

mattt avatar Apr 23 '20 16:04 mattt

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

natecook1000 avatar Apr 24 '20 17:04 natecook1000