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

Add two new `ArgumentArrayParsingStrategy` options

Open natecook1000 opened this issue 2 years ago • 1 comments

Description

This adds two new parsing options for argument arrays, and renames .unconditionalRemaining to .captureForPassthrough:

  • unconditionalRemaining has been an attractive nuisance. It looks like it means "everything leftover", but it actually has a specific and slightly strange set of behaviors to support SwiftPM capturing inputs to pass on to a sub-client, as in swift run MyExecutableTarget. The new name better matches this intended usage.
  • allUnrecognized collects all the inputs that weren't used during parsing, which is a more generally useful behavior. This essentially suppresses all "unrecognized flag/option" and "unexpected argument" errors, and makes those extra inputs available to the client.
  • postTerminator collects all inputs that follow the -- terminator, before trying to parse any other positional arguments. This is a non-standard, but sometimes useful parsing strategy.

Detailed Design

Two new symbols and one renaming — see the diff for full API documentation.

/// After parsing, capture all unrecognized inputs in this argument array.
public static var allUnrecognized: ArgumentArrayParsingStrategy {
  self.init(base: .allUnrecognized)
}

/// Before parsing, capture all inputs that follow the `--` terminator in this
/// argument array.
public static var postTerminator: ArgumentArrayParsingStrategy {
  self.init(base: .postTerminator)
}

/// Parse all remaining inputs after parsing any known options or flags,
/// including dash-prefixed inputs and the `--` terminator.
public static var captureForPassthrough: ArgumentArrayParsingStrategy {
  self.init(base: .allRemainingInput)
}

@available(*, deprecated, renamed: "captureForPassthrough")
public static var unconditionalRemaining: ArgumentArrayParsingStrategy {
  .captureForPassthrough
}

Documentation Plan

Includes API documentation for the new symbols and significantly expanded docs for the existing members.

Test Plan

Includes unit tests for the new cases.

Source Impact

This deprecates the unconditionalRemaining strategy, which was confusingly named. The new name is captureForPassthrough, which is more explicit about its intent.

Checklist

  • [x] 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
  • [x] I've updated the documentation if necessary

natecook1000 avatar Sep 22 '22 21:09 natecook1000

@swift-ci Please test

natecook1000 avatar Sep 30 '22 21:09 natecook1000

@swift-ci Please test

natecook1000 avatar Oct 07 '22 18:10 natecook1000

@natecook1000 Thanks for implementing this!

danpalmer avatar Oct 08 '22 19:10 danpalmer