Determine the type of the `@Argument` `var` represented by an `ArgumentDefinition` or by an `ArgumentInfoV0`

How can I determine the type of the @Argument var represented by an ArgumentDefinition or by an ArgumentInfoV0?

If I cannot determine that from either of those, is there any way to determine that info when the ArgumentDefinition / ArgumentInfoV0 is created, so I can modify ArgumentDefinition and/or ArgumentInfoV0 to supply that information?

What do you mean by Type? if you mean the type according to the Swift type checker this isn't currently possible nor stored anywhere in ArgumentDefinition and ArgumentInfoV0. It might be possible to store it as an Any.Type in ArgumentDefinition but we dont want to leak that out via ArgumentInfoV0.

cc @nnnnnnnn

@rauhul Thanks for the info.

By type, I mean [String] from the following example Swift property of a ParsableArguments:

@Argument
var args: [String]

I actually don't need to have the type available, but having the type available would have solved my issue, so I asked about something broader than I actually need.

Specifically, I want to fix zsh completion candidates are offered only once for `Sequence` `@Argument`s · Issue #776 · apple/swift-argument-parser · GitHub by setting ArgumentInfoV0.isRepeating = true if the type of the Swift variable for the argument is a Sequence, because multiple values can be supplied for such arguments (this will only work after completions have been migrated to ToolInfo).

I thus only need to know if the argument is repeatable (i.e. its Swift type is a Sequence or Array or whatever is the most general supported collection type) at the one place where ArgumentInfoV0s are constructed: this line swift-argument-parser/Sources/ArgumentParser/Usage/DumpHelpGenerator.swift at fd0db17fa290b350a06b937ba55c5e850bbfeb25 · apple/swift-argument-parser · GitHub.

Is there any way to get that info there?

I think the DumpHelpGenerator already sets isRepeating properly?

See: swift-argument-parser/Sources/ArgumentParser/Usage/DumpHelpGenerator.swift at fd0db17fa290b350a06b937ba55c5e850bbfeb25 · apple/swift-argument-parser · GitHub

I think the issue lies in the completion generator not leveraging this information in ToolInfo

As far as I can tell, ArgumentInfoV0 is already capturing that info here: swift-argument-parser/Sources/ArgumentParser/Usage/DumpHelpGenerator.swift at fd0db17fa290b350a06b937ba55c5e850bbfeb25 · apple/swift-argument-parser · GitHub

1 Like

@rauhul @nnnnnnnn While ArgumentInfoV0.isRepeating is being set from argument.help.options.contains(.isRepeating), .isRepeating seems to only be in argument.help.options for flags & options, not for repeating arguments.

The completions generators therefore cannot properly handle repeating arguments without some additional information.

As long as .isRepeating can be added to argument.help.options for all Sequence arguments, then it will flow through to ArgumentInfoV0.isRepeating, then zsh completions can handle them.

I assume that the same problem exists for the other 2 shells. I haven't yet checked them because I was focusing on getting zsh completions working completely for my project before looking into the other shells. I will probably try the other shells soon, though,

I should have mentioned all this info in my first post, but I was trying to not overwhelm with detail unless necessary, but obviously, the detail was necessary…

Thanks for discussing this.

@rauhul @nnnnnnnn I'm very sorry, it looks like positional arguments are properly being labeled as repeatable in ArgumentDefinition & ArgumentInfoV0.

I had a typo that caused my changes to not work. All is good now. Sorry for the waste of time.

I've submitted a PR to fix the issue that I mentioned before in this convo.

2 Likes