Skip to content

Commit d11f4b0

Browse files
committed
Add ArgumentInfoV0.ParsingStrategyV0 enum.
Signed-off-by: Ross Goldberg <[email protected]>
1 parent 050a536 commit d11f4b0

File tree

11 files changed

+157
-6
lines changed

11 files changed

+157
-6
lines changed

Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,10 @@ extension ArgumentInfoV0 {
262262
isRepeating
263263
else { return false }
264264

265-
// TODO: ensure parsingStrategy is in ToolInfoV0
266-
// switch parsingStrategy {
267-
// case .default, .unconditional: return true
268-
// default: return false
269-
// }
270-
return true
265+
switch parsingStrategy {
266+
case .default, .unconditional: return true
267+
default: return false
268+
}
271269
}
272270

273271
fileprivate var completionAbstract: String {

Sources/ArgumentParser/Usage/DumpHelpGenerator.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ extension ArgumentInfoV0 {
127127
sectionTitle: argument.help.parentTitle.nonEmpty,
128128
isOptional: argument.help.options.contains(.isOptional),
129129
isRepeating: argument.help.options.contains(.isRepeating),
130+
parsingStrategy: ArgumentInfoV0.ParsingStrategyV0(argument: argument),
130131
names: argument.names.map(ArgumentInfoV0.NameInfoV0.init),
131132
preferredName: argument.names.preferredName.map(
132133
ArgumentInfoV0.NameInfoV0.init),
@@ -159,6 +160,27 @@ extension ArgumentInfoV0.KindV0 {
159160
}
160161
}
161162

163+
extension ArgumentInfoV0.ParsingStrategyV0 {
164+
fileprivate init(argument: ArgumentDefinition) {
165+
switch argument.parsingStrategy {
166+
case .`default`:
167+
self = .default
168+
case .scanningForValue:
169+
self = .scanningForValue
170+
case .unconditional:
171+
self = .unconditional
172+
case .upToNextOption:
173+
self = .upToNextOption
174+
case .allRemainingInput:
175+
self = .allRemainingInput
176+
case .postTerminator:
177+
self = .postTerminator
178+
case .allUnrecognized:
179+
self = .allUnrecognized
180+
}
181+
}
182+
}
183+
162184
extension ArgumentInfoV0.NameInfoV0 {
163185
fileprivate init(name: Name) {
164186
switch name {

Sources/ArgumentParserToolInfo/ToolInfo.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ public struct ArgumentInfoV0: Codable, Hashable {
140140
case flag
141141
}
142142

143+
public enum ParsingStrategyV0: String, Codable, Hashable {
144+
/// Expect the next `SplitArguments.Element` to be a value and parse it.
145+
/// Will fail if the next input is an option.
146+
case `default`
147+
/// Parse the next `SplitArguments.Element.value`
148+
case scanningForValue
149+
/// Parse the next `SplitArguments.Element` as a value, regardless of its type.
150+
case unconditional
151+
/// Parse multiple `SplitArguments.Element.value` up to the next non-`.value`
152+
case upToNextOption
153+
/// Parse all remaining `SplitArguments.Element` as values, regardless of its type.
154+
case allRemainingInput
155+
/// Collect all the elements after the terminator, preventing them from
156+
/// appearing in any other position.
157+
case postTerminator
158+
/// Collect all unused inputs once recognized arguments/options/flags have
159+
/// been parsed.
160+
case allUnrecognized
161+
}
162+
143163
public enum CompletionKindV0: Codable, Hashable {
144164
/// Use the specified list of completion strings.
145165
case list(values: [String])
@@ -169,6 +189,9 @@ public struct ArgumentInfoV0: Codable, Hashable {
169189
/// Argument can be specified multiple times.
170190
public var isRepeating: Bool
171191

192+
/// Parsing strategy of the ArgumentInfo.
193+
public var parsingStrategy: ParsingStrategyV0
194+
172195
/// All names of the argument.
173196
public var names: [NameInfoV0]?
174197
/// The best name to use when referring to the argument in help displays.
@@ -208,6 +231,7 @@ public struct ArgumentInfoV0: Codable, Hashable {
208231
sectionTitle: String?,
209232
isOptional: Bool,
210233
isRepeating: Bool,
234+
parsingStrategy: ParsingStrategyV0,
211235
names: [NameInfoV0]?,
212236
preferredName: NameInfoV0?,
213237
valueName: String?,
@@ -226,6 +250,8 @@ public struct ArgumentInfoV0: Codable, Hashable {
226250
self.isOptional = isOptional
227251
self.isRepeating = isRepeating
228252

253+
self.parsingStrategy = parsingStrategy
254+
229255
self.names = names?.nonEmpty
230256
self.preferredName = preferredName
231257

Tests/ArgumentParserToolInfoTests/Examples/example1.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"shouldDisplay": true,
2121
"isOptional": true,
2222
"isRepeating": true,
23+
"parsingStrategy" : "default",
2324
"names": [
2425
{
2526
"kind": "long",
@@ -53,12 +54,14 @@
5354
"shouldDisplay": false,
5455
"isOptional": false,
5556
"isRepeating": false,
57+
"parsingStrategy" : "default",
5658
},
5759
{
5860
"kind": "flag",
5961
"shouldDisplay": false,
6062
"isOptional": false,
6163
"isRepeating": false,
64+
"parsingStrategy" : "default",
6265
}
6366
]
6467
}

Tests/ArgumentParserUnitTests/Snapshots/testADumpHelp().json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"name" : "enumerated-option"
2626
}
2727
],
28+
"parsingStrategy" : "default",
2829
"preferredName" : {
2930
"kind" : "long",
3031
"name" : "enumerated-option"
@@ -57,6 +58,7 @@
5758
"name" : "enumerated-option-with-default-value"
5859
}
5960
],
61+
"parsingStrategy" : "default",
6062
"preferredName" : {
6163
"kind" : "long",
6264
"name" : "enumerated-option-with-default-value"
@@ -74,6 +76,7 @@
7476
"name" : "no-help-option"
7577
}
7678
],
79+
"parsingStrategy" : "default",
7780
"preferredName" : {
7881
"kind" : "long",
7982
"name" : "no-help-option"
@@ -92,6 +95,7 @@
9295
"name" : "int-option"
9396
}
9497
],
98+
"parsingStrategy" : "default",
9599
"preferredName" : {
96100
"kind" : "long",
97101
"name" : "int-option"
@@ -111,6 +115,7 @@
111115
"name" : "int-option-with-default-value"
112116
}
113117
],
118+
"parsingStrategy" : "default",
114119
"preferredName" : {
115120
"kind" : "long",
116121
"name" : "int-option-with-default-value"
@@ -122,6 +127,7 @@
122127
"isOptional" : false,
123128
"isRepeating" : false,
124129
"kind" : "positional",
130+
"parsingStrategy" : "default",
125131
"shouldDisplay" : true,
126132
"valueName" : "arg"
127133
},
@@ -130,6 +136,7 @@
130136
"isOptional" : false,
131137
"isRepeating" : false,
132138
"kind" : "positional",
139+
"parsingStrategy" : "default",
133140
"shouldDisplay" : true,
134141
"valueName" : "arg-with-help"
135142
},
@@ -139,6 +146,7 @@
139146
"isOptional" : true,
140147
"isRepeating" : false,
141148
"kind" : "positional",
149+
"parsingStrategy" : "default",
142150
"shouldDisplay" : true,
143151
"valueName" : "arg-with-default-value"
144152
},
@@ -157,6 +165,7 @@
157165
"name" : "help"
158166
}
159167
],
168+
"parsingStrategy" : "default",
160169
"preferredName" : {
161170
"kind" : "long",
162171
"name" : "help"
@@ -175,6 +184,7 @@
175184
"isOptional" : true,
176185
"isRepeating" : true,
177186
"kind" : "positional",
187+
"parsingStrategy" : "default",
178188
"shouldDisplay" : true,
179189
"valueName" : "subcommands"
180190
},
@@ -196,6 +206,7 @@
196206
"name" : "help"
197207
}
198208
],
209+
"parsingStrategy" : "default",
199210
"preferredName" : {
200211
"kind" : "long",
201212
"name" : "help"

Tests/ArgumentParserUnitTests/Snapshots/testBDumpHelp().json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"name" : "verbose"
1212
}
1313
],
14+
"parsingStrategy" : "default",
1415
"preferredName" : {
1516
"kind" : "long",
1617
"name" : "verbose"
@@ -29,6 +30,7 @@
2930
"name" : "name"
3031
}
3132
],
33+
"parsingStrategy" : "default",
3234
"preferredName" : {
3335
"kind" : "long",
3436
"name" : "name"
@@ -52,6 +54,7 @@
5254
"name" : "help"
5355
}
5456
],
57+
"parsingStrategy" : "default",
5558
"preferredName" : {
5659
"kind" : "long",
5760
"name" : "help"
@@ -70,6 +73,7 @@
7073
"isOptional" : true,
7174
"isRepeating" : true,
7275
"kind" : "positional",
76+
"parsingStrategy" : "default",
7377
"shouldDisplay" : true,
7478
"valueName" : "subcommands"
7579
},
@@ -91,6 +95,7 @@
9195
"name" : "help"
9296
}
9397
],
98+
"parsingStrategy" : "default",
9499
"preferredName" : {
95100
"kind" : "long",
96101
"name" : "help"

Tests/ArgumentParserUnitTests/Snapshots/testCDumpHelp().json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"name" : "color"
3232
}
3333
],
34+
"parsingStrategy" : "default",
3435
"preferredName" : {
3536
"kind" : "long",
3637
"name" : "color"
@@ -69,6 +70,7 @@
6970
"name" : "default-color"
7071
}
7172
],
73+
"parsingStrategy" : "default",
7274
"preferredName" : {
7375
"kind" : "long",
7476
"name" : "default-color"
@@ -106,6 +108,7 @@
106108
"name" : "opt"
107109
}
108110
],
111+
"parsingStrategy" : "default",
109112
"preferredName" : {
110113
"kind" : "long",
111114
"name" : "opt"
@@ -143,6 +146,7 @@
143146
"name" : "extra"
144147
}
145148
],
149+
"parsingStrategy" : "default",
146150
"preferredName" : {
147151
"kind" : "long",
148152
"name" : "extra"
@@ -161,6 +165,7 @@
161165
"name" : "discussion"
162166
}
163167
],
168+
"parsingStrategy" : "default",
164169
"preferredName" : {
165170
"kind" : "long",
166171
"name" : "discussion"
@@ -183,6 +188,7 @@
183188
"name" : "help"
184189
}
185190
],
191+
"parsingStrategy" : "default",
186192
"preferredName" : {
187193
"kind" : "long",
188194
"name" : "help"
@@ -201,6 +207,7 @@
201207
"isOptional" : true,
202208
"isRepeating" : true,
203209
"kind" : "positional",
210+
"parsingStrategy" : "default",
204211
"shouldDisplay" : true,
205212
"valueName" : "subcommands"
206213
},
@@ -222,6 +229,7 @@
222229
"name" : "help"
223230
}
224231
],
232+
"parsingStrategy" : "default",
225233
"preferredName" : {
226234
"kind" : "long",
227235
"name" : "help"

Tests/ArgumentParserUnitTests/Snapshots/testMathAddDumpHelp().json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"name" : "x"
1818
}
1919
],
20+
"parsingStrategy" : "default",
2021
"preferredName" : {
2122
"kind" : "long",
2223
"name" : "hex-output"
@@ -29,6 +30,7 @@
2930
"isOptional" : true,
3031
"isRepeating" : true,
3132
"kind" : "positional",
33+
"parsingStrategy" : "default",
3234
"shouldDisplay" : true,
3335
"valueName" : "values"
3436
},
@@ -43,6 +45,7 @@
4345
"name" : "version"
4446
}
4547
],
48+
"parsingStrategy" : "default",
4649
"preferredName" : {
4750
"kind" : "long",
4851
"name" : "version"
@@ -65,6 +68,7 @@
6568
"name" : "help"
6669
}
6770
],
71+
"parsingStrategy" : "default",
6872
"preferredName" : {
6973
"kind" : "long",
7074
"name" : "help"
@@ -83,6 +87,7 @@
8387
"isOptional" : true,
8488
"isRepeating" : true,
8589
"kind" : "positional",
90+
"parsingStrategy" : "default",
8691
"shouldDisplay" : true,
8792
"valueName" : "subcommands"
8893
},
@@ -104,6 +109,7 @@
104109
"name" : "help"
105110
}
106111
],
112+
"parsingStrategy" : "default",
107113
"preferredName" : {
108114
"kind" : "long",
109115
"name" : "help"
@@ -122,6 +128,7 @@
122128
"name" : "version"
123129
}
124130
],
131+
"parsingStrategy" : "default",
125132
"preferredName" : {
126133
"kind" : "long",
127134
"name" : "version"

0 commit comments

Comments
 (0)