mas icon indicating copy to clipboard operation
mas copied to clipboard

Shell completion overhaul

Open rgoldberg opened this issue 1 year ago • 2 comments

mas 1.8.7+ uses Swift Argument Parser (aka SAP), which can generate shell completion scripts for bash, fish & zsh.

Scripts for mas would be written to stdout by running:

mas --generate-completion-script <bash | fish | zsh>

The completion scripts generated by the current version of SAP (1.5.0), however, suffer from many problems.

I have opened 2 issues in SAP. I've submitted a PR for the first, and am finishing a PR for the second:

  • https://github.com/apple/swift-argument-parser/issues/672
  • https://github.com/apple/swift-argument-parser/issues/679

If SAP does not merge & release the associated PRs quickly, mas can depend on a branch off main in an mas fork of SAP that contains the aforementioned 2 PRs.

Once all SAP improvements are ready:

  • remove the existing bash & fish completion scripts from git
  • setup mas to generate completion scripts for all 3 shells using SAP
  • generate all 3 scripts in:
    • Homebrew/core/mas formula
    • mas-cli/tap/mas formula
    • *.pkg installation process
    • any other installation process
  • document completion script generation in README.md
  • possibly create a new command / option to write scripts for all three shells (or maybe just one at a time based on an argument) to their proper locations to be loaded by their respective shell & used for completion.
  • discontinue app names as arguments for mas upgrade
    • remove AppLibrary.installedApps(named:)
  • #608
  • Offer subcommands as completions after mas help

Assuming that bundle IDs are supported as arguments optionally in place of app IDs (see #541; app IDs & bundle IDs will be collectively termed IDs), completion for IDs would work as follows:

For commands that work on installed apps:

If the current word contains all digits, match against installed app IDs, then offer them as completions, annotated with the associated app name & bundle ID.

Otherwise, match against installed bundle IDs, then offer them as completions, annotated with the associated app name & app ID.

Also, search the installed app names for the text of the current word. Offer the bundle ID as the completion for each result (ordered as per the response from Apple), each annotated with the app name & app ID.

The above completion candidates would be filtered as relevant to the current command. e.g., only IDs of outdated apps would be offered for mas upgrade.

For commands that work on apps that aren't installed:

Search the Apple iTunes Store Web API for the text of the current word. Offer the bundle ID as the completion for each result (ordered as per the response from Apple), each annotated with the app name & app ID.

Supersede:

  • #109
  • #222
  • #453

rgoldberg avatar Dec 12 '24 17:12 rgoldberg

Hi! I’ve only just started using mas and noticed that completions for zsh aren’t available yet.

I was just wondering if there’s any rough timeline or plans for this? Totally understand if it’s not a priority — just curious, as it would be a really nice addition!

AWH1122 avatar Jun 09 '25 12:06 AWH1122

@AWH1122 I'm working on them now. The last of my extant pending improvements to Apple's Swift Argument Parser (SAP) library shell completion script generation code was just merged into that project yesterday, though I'm working on some more improvements for it (it handles bash, fish & zsh).

They'll probably release a new SAP version soon, soon after which I'll release preliminary completion scripts generated by SAP. Later, after more SAP improvements, I'll release improved completion scripts.

rgoldberg avatar Jun 09 '25 13:06 rgoldberg