Skip to content

Add completion script generation #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
Jul 29, 2020
Merged

Conversation

natecook1000
Copy link
Member

@natecook1000 natecook1000 commented Apr 22, 2020

Description

This adds support for generating shell completion scripts for tools built using ArgumentParser.

Detailed Design

This adds a CompletionKind struct that you can specify when declaring an @Option property:

struct CompletionKind {
  static var `default`: CompletionKind
  static func file(extensions: [String] = []) -> CompletionKind
  static var directory: CompletionKind
  static func list([String]) -> CompletionKind
  static func shellCommand(_ command: String) -> CompletionKind
  static func custom((String) -> [String]) -> CompletionKind
}

Types that conform to ExpressibleByArgument can also provide a static var defaultCompletionKind: CompletionKind to provide the completion kind when it's omitted from the property wrapper declaration.

To generate completion scripts, you use the --generate-completions option on your command-line tool:

$ example --generate-completion-script
(generated completion script for autodetected shell)
$ example --generate-completion-script zsh
(generated completion script for zsh)

You can generate completion scripts programmatically by calling the completionScript(for:) method on the root ParsableCommand type for a command.

Documentation Plan

Symbol documentation for all of the new additions, along with a new guide specifically about using, customizing, and installing completion scripts.

Test Plan

I've added unit tests for generating completion scripts with different completion parameters, as well as an integration test for the math command. I've also been testing this feature in the context of SwiftPM adoption of ArgumentParser. Unfortunately, we don't have an automated way to test that the completion scripts actually work properly, so that verification has been manual.

Source Impact

No source impact for existing code.

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

This makes the ordering of names deterministic across program runs.
This lets CaseIterable types get completion values for free, and should
let URL specify .files when someone adds ExpressibleByArgument conformance
This moves shell completion scripts further toward feature completeness:

- auto-detecting the current user's shell
- using patterns for file or directory completions
- using a custom function for custom completions
@natecook1000 natecook1000 force-pushed the nate/you_had_me_at_hello branch from ceb9494 to 5e988ae Compare May 6, 2020 18:22
@natecook1000 natecook1000 force-pushed the nate/you_had_me_at_hello branch from 736eda2 to 03c7aec Compare June 9, 2020 18:28
This moves from having a general file-matching pattern to just
a list of valid extensions, which is simpler to implement across
different shells.
@natecook1000 natecook1000 force-pushed the nate/you_had_me_at_hello branch from 8893ae5 to 02ec7e4 Compare June 16, 2020 22:18
@natecook1000 natecook1000 force-pushed the nate/you_had_me_at_hello branch from 18e4664 to ec90064 Compare July 26, 2020 00:31
@natecook1000 natecook1000 changed the title [wip] Add completion script generation Add completion script generation Jul 26, 2020
@natecook1000 natecook1000 marked this pull request as ready for review July 26, 2020 00:31
@natecook1000
Copy link
Member Author

@swift-ci Please test

@natecook1000
Copy link
Member Author

@swift-ci Please test

@natecook1000 natecook1000 merged commit 280700d into master Jul 29, 2020
@natecook1000 natecook1000 deleted the nate/you_had_me_at_hello branch July 29, 2020 22:58
@ddunbar
Copy link
Member

ddunbar commented Jul 31, 2020

When you say no automated way to test completions work, is this not possible with say opening a PTY and attempting at least some completions?

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.

2 participants