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

[WIP] Add ArgumentSource as the projected value of arguments

Open natecook1000 opened this issue 5 years ago • 2 comments

Description

This adds a new ArgumentSource type as the projected value for @Argument, @Option, and @Flag types. This way, you can access the original string and location in the command-line arguments that produced a value.

struct Example: ParsableCommand {
    @Argument()
    var name: String

    @Option()
    var age: Int

    func run() {
        print("Your name is \(name) and your age is \(age).")
        if $name.source.first!.position < $age.source.first!.position {
            print("You gave your name before your age.")
        } else {
            print("You gave your age before your name.")
        }
    }
}
$ example --age 43 Blake
Your name is Blake and your age is 43.
You gave your age before your name.

Detailed Design

TK

Documentation Plan

TK

Test Plan

TK

Source Impact

This would be an additive change only.

Checklist

  • [ ] I've added at least one test that validates that my change is working, if appropriate
  • [x] I've followed the code style of the rest of the project
  • [x] I've read the Contribution Guidelines
  • [ ] I've updated the documentation if necessary

natecook1000 avatar May 02 '20 18:05 natecook1000

Neat idea!

kylemacomber avatar May 02 '20 20:05 kylemacomber

Looks good, @natecook1000! Thanks for adding this.

mpapple-swift avatar May 04 '20 21:05 mpapple-swift