Skip to content

Conversation

rgoldberg
Copy link
Contributor

@rgoldberg rgoldberg commented Sep 13, 2025

Fix generated shell completion script handling of repeating & non-repeating positional arguments, flags & options.

Also fix an unrelated swift-format violation & DocC typos in Flag.swift.

Resolve #806

Checklist

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

@rgoldberg rgoldberg changed the title 806 repeating arguments Fix generated shell completion script handling of repeating & non-repeating positional arguments, flags & options Sep 13, 2025
@rgoldberg rgoldberg force-pushed the 806-repeating-arguments branch from c819a12 to 1ae39ef Compare September 19, 2025 18:09
Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still testing this locally, but looks good so far!

\(declareTopLevelArray)repeating_options=(\(options.filter(\.isRepeating).flatMap(\.completionWords).joined(separator: " ")))
\(declareTopLevelArray)non_repeating_options=(\(options.filter { !$0.isRepeating }.flatMap(\.completionWords).joined(separator: " ")))
\(offerFlagsOptionsFunctionName) \
\(positionalArguments.contains { $0.isRepeating } ? 9_223_372_036_854_775_807 : positionalArguments.count)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Int.max literal doesn't seem platform-safe – can you say a bit about the role it's serving?

Copy link
Contributor Author

@rgoldberg rgoldberg Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a positional arg is defined after a repeating positional arg, the prior repeating positional arg swallows all the positional arguments, so I wanted to ensure that no completion script is generated for any positional args after the first repeating positional arg.

I used the magic number to accept all non-flag/non-option/non-option-value arguments as positionals if there is any repeating positional.

I forgot that Swift runs on non-Macs…

Thanks for catching this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@natecook1000 Just pushed a fix for this. I also rewrote my earlier reply because it was wrong, as I misremembered exactly what the magic number was for; the solution was the same either way, but I realized I misspoke about its exact purpose when I went in to fix it. Early Monday isn't my best time for remembering stuff I haven't looked at for over a week… :)

// If there aren't any, skip the case block altogether.
let optionHandlers =
(arguments ?? []).compactMap { arg in
arguments.compactMap { arg in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use options from above here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should. Thanks for catching that. This prompted me to notice another older overlooked failure to use an earlier variable. Will fix both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just submitted a fix for this. Will fix the magic number issue now.

… accept all subsequent non-flag/non-option/non-option-value tokens as positionals.

Signed-off-by: Ross Goldberg <[email protected]>
@rgoldberg rgoldberg force-pushed the 806-repeating-arguments branch from ed5bcf3 to eb86da3 Compare September 22, 2025 14:50
@rgoldberg
Copy link
Contributor Author

@natecook1000 Thanks for the feedback. I've submitted fixes for the 2 issues you raised. Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shell completion scripts variously do not properly handle repeating and/or non-repeating positional arguments, flags, or options
2 participants