Ordered Multi-Option Collection Support#884
Open
manav-Mnv wants to merge 1 commit intoapple:mainfrom
Conversation
This feature enables the Swift Argument Parser to collect multiple different option names into a single ordered array while preserving the relative global ordering of those flags as they appear on the command line. This is achieved by introducing a new @option initializer that passes both the used flag name and its value to a transform closure, allowing for precise mapping of interleaved options to a single property. 1. ArgumentDefinition.swift I added a new initializer that changes the internal parsing block to pass the Name (as a String) to the transformation logic: // New ArgumentDefinition init transform: @escaping (_ name: String, _ valueString: String) throws -> Container.Contained 2. Option.swift I added overloads to the @option initializers that make this name available to the user-provided transform closure: swift // New @option initializer for arrays public init<T>( name: NameSpecification = .long, ..., transform: @escaping (String, String) throws -> T // name is now available here! ) where Value == [T] Existing Code: You had to create separate properties for different options (e.g., @option var resize: [Int] and @option var blur: [Int]). While each array was ordered, there was no way to know if the user typed --resize 10 --blur 5 or --blur 5 --resize 10. New Code: You can now define a single property that accepts multiple different flag names. Because the transform closure receives the name of the flag being parsed, you can collect different operations into one array while perfectly preserving the interleaved order of the command-line input.
Member
|
Hi @manav-Mnv! This looks like an implementation of #862 – could you take a look at the discussion there and address the comments in any implementation? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This feature enables the Swift Argument Parser to collect multiple different option names into a single ordered array while preserving the relative global ordering of those flags as they appear on the command line. This is achieved by introducing a new
@Optioninitializer that passes both the used flag name and its value to atransformclosure, allowing for precise mapping of interleaved options to a single property.1. ArgumentDefinition.swift
I added a new initializer that changes the internal parsing block to pass the Name (as a String) to the transformation logic:
2. Option.swift
I added overloads to the
@Optioninitializers that make this name available to the user-providedtransformclosure:Existing Code: You had to create separate properties for different options (e.g.,
@Option var resize: [Int]and@Option var blur: [Int]). While each array was ordered, there was no way to know if the user typed--resize 10 --blur 5or--blur 5 --resize 10.New Code: You can now define a single property that accepts multiple different flag names. Because the
transformclosure receives the name of the flag being parsed, you can collect different operations into one array while perfectly preserving the interleaved order of the command-line input.Checklist