Skip to content
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

feat(complete): generate completions for visible aliases #5476

Merged
merged 2 commits into from
Jun 6, 2024

Conversation

pzmarzly
Copy link
Contributor

Fixes #4265

Closes #5412

This PR picks up work from #5412 to get it to a merge-able state.

I'm splitting the work into 2 standalone commits (all tests pass on both of them), to make it easier to review. I leave it up to you whether to merge them in or squash them into one.

Commit 1. Fixing the iteration over all_subcommands in zsh.rs. We deduplicate values on (sc_name, bin_name) keys, but then only iterate on bin_name. This doesn't cause problems now, since all bin names seem to be unique. However, without fixing this, the second commit would have started generating duplicated functions with same names.

Commit 2. Starting to generate autocompletions for aliased subcommands. This is taking the code from #5412 and updating it to latest changes.

Testing the change

Adapting the test code from #4265's first comment:

[package]
name = "clap-test"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { path = "../clap", version = "4.5.4" }
clap_complete = { path = "../clap/clap_complete", version = "4.5.2" }
clap_derive = { path = "../clap/clap_derive", version = "4.5.4" }
use std::io::{stdout, Write};
use clap::CommandFactory;
use clap_derive::Parser;
use clap_derive::Args;

#[derive(Parser, Debug)]
enum App {
    #[clap(visible_alias = "bar")]
    Foo(FooArgs),
}

#[derive(Args, Clone, Debug)]
struct FooArgs {
    #[clap(long)]
    my_flag: bool,
}

fn main() {
    let mut app = App::command().disable_help_flag(true).disable_help_subcommand(true);
    let mut buf = vec![];
    clap_complete::generate(clap_complete::Shell::Zsh, &mut app, "clap-test", &mut buf);
    stdout().write_all(&mut buf).unwrap();
}
$ cargo run > before.zsh
$ source before.zsh
$ clap-test [TAB] <- gives me "foo bar --"
$ clap-test foo [TAB] <- gives me "--my-flag"
$ clap-test bar [TAB] <- no reaction

After making this change to clap_complete:

$ cargo run > after.zsh
$ source after.zsh
$ clap-test [TAB] <- gives me "foo bar --"
$ clap-test foo [TAB] <- gives me "--my-flag"
$ clap-test bar [TAB] <- gives me "--my-flag"

@pzmarzly
Copy link
Contributor Author

Re: linter error. Commit subject max line length of 50 is brutal, even feat(complete): Autocomplete visible_alias powershell doesn't fit (53 chars).

@pzmarzly
Copy link
Contributor Author

I fixed the linter issues, and implemented support for all shells.

@epage is there something you'd like me to do here, or something I can do to make it easier for you to review it? Many thanks.

Fixing the iteration over all_subcommands in zsh.rs. We deduplicate
values on (sc_name, bin_name) keys, but then only iterate on bin_name.
This doesn't cause problems now, since all bin names seem to be unique.
However, without fixing this, the next commit would have started
generating duplicated functions with same names.

For example, with an #[long = "foo", visible_alias = "bar"] subcommand,
we'll end up with 2 pairs: [("foo", "foo"), ("bar", "foo")]. Before this
commit, we would have ended up generating _my-app__foo_commands()
functions. These functions should have identical content, so it is not
an error, just an inefficiency that we can fix.
Let's generate autocompletions for aliased subcommands.

    $ source before.zsh
    $ clap-test [TAB] <- gives me "foo bar --"
    $ clap-test foo [TAB] <- gives me "--my-flag"
    $ clap-test bar [TAB] <- no reaction

    $ source after.zsh
    $ clap-test [TAB] <- gives me "foo bar --"
    $ clap-test foo [TAB] <- gives me "--my-flag"
    $ clap-test bar [TAB] <- gives me "--my-flag"
@epage
Copy link
Member

epage commented Jun 6, 2024

Thanks!

@epage epage merged commit d87dee6 into clap-rs:master Jun 6, 2024
22 checks passed
facebook-github-bot pushed a commit to facebookincubator/below that referenced this pull request Jun 10, 2024
Summary: Upgrading clap to new version (released yesterday). My motivation for doing it now is that my PR clap-rs/clap#5476 has been released as part of this release, which should fix autocompletion for all CLIs using `visible_alias` option.

Reviewed By: ukautz

Differential Revision: D58283249

fbshipit-source-id: 4a48e86c47519431913c34880a132049b5522e7b
facebook-github-bot pushed a commit to facebookincubator/antlir that referenced this pull request Jun 10, 2024
Summary: Upgrading clap to new version (released yesterday). My motivation for doing it now is that my PR clap-rs/clap#5476 has been released as part of this release, which should fix autocompletion for all CLIs using `visible_alias` option.

Test Plan:
```
$ fbcode/common/rust/tools/reindeer/vendor
...
33 vulnerabilities, 1 warnings in 2989 packages
...
03:09:07 -07:00 INFO Successfully generated 2317 Cargo.toml and 456 additional files and stored them on disk

$ fbcode/common/rust/tools/scripts/third-party-check.sh
BUILD SUCCEEDED x5

$ fbcode/common/rust/tools/scripts/check_all.sh
no errors for first 15 minutes, then buck died
Command failed: Failed to connect to buck daemon.
        Try running `buck2 kill` and your command afterwards.
        Alternatively, try running `rm -rf ~/.buck/buckd` and your command afterwards
```

Testing if my PR clap-rs/clap#5476 works in fbcode:

```name=Before
$ faceboot3 --help
...
  boot-from-network        Creates, renders and activates the config, making a host boot from network [aliases: net,
                               create-render-and-activate-config]
...

$ faceboot3 boot-from-network [TAB] # autocompletes in zsh, fish and bash
corp-provisioning                                      (Boot using faceboot2 FacebootOptions)
help                              (Print this message or the help of the given subcommand(s))
legacy-from-faceboot-options                           (Boot using faceboot2 FacebootOptions)
legacy-from-metalos-config-url                               (Boot using MetalOS MultiConfig)
…and 7 more rows

$ faceboot3 net [TAB] # autocompletes in bash, nothing happens in zsh, fish starts suggesting files and directories in current directory
```

```name=After
$ buck2 build fbcode//syseng/faceboot3/cli:cli-completions.zsh fbcode//syseng/faceboot3/cli:cli-completions.bash fbcode//syseng/faceboot3/cli:cli-completions.fish --show-full-output
fbcode//syseng/faceboot3/cli:cli-completions.bash /data/users/pzmarzly/fbsource/buck-out/v2/gen/fbcode/767cf7b4a84025eb/syseng/faceboot3/cli/__cli-completions.bash__/out/cli-completions.bash
fbcode//syseng/faceboot3/cli:cli-completions.fish /data/users/pzmarzly/fbsource/buck-out/v2/gen/fbcode/767cf7b4a84025eb/syseng/faceboot3/cli/__cli-completions.fish__/out/cli-completions.fish
fbcode//syseng/faceboot3/cli:cli-completions.zsh /data/users/pzmarzly/fbsource/buck-out/v2/gen/fbcode/767cf7b4a84025eb/syseng/faceboot3/cli/__cli-completions.zsh__/out/cli-completions.zsh

$ sudo cp /data/users/pzmarzly/fbsource/buck-out/v2/gen/fbcode/767cf7b4a84025eb/syseng/faceboot3/cli/__cli-completions.bash__/out/cli-completions.bash /usr/share/bash-completion/completions/faceboot3
$ sudo cp /data/users/pzmarzly/fbsource/buck-out/v2/gen/fbcode/767cf7b4a84025eb/syseng/faceboot3/cli/__cli-completions.fish__/out/cli-completions.fish /etc/fish/completions/faceboot3.fish
$ sudo cp /data/users/pzmarzly/fbsource/buck-out/v2/gen/fbcode/767cf7b4a84025eb/syseng/faceboot3/cli/__cli-completions.zsh__/out/cli-completions.zsh /usr/share/zsh/site-functions/_faceboot3

$ faceboot3 boot-from-network [TAB] # autocompletes in zsh, fish and bash

$ faceboot3 net [TAB] # autocompletes in zsh, fish and bash
```

{P1400427477}

{P1400437414}

{P1400427342}

Reviewed By: ukautz

Differential Revision: D58283249

fbshipit-source-id: 4a48e86c47519431913c34880a132049b5522e7b
facebook-github-bot pushed a commit to facebookincubator/reindeer that referenced this pull request Jun 10, 2024
Summary: Upgrading clap to new version (released yesterday). My motivation for doing it now is that my PR clap-rs/clap#5476 has been released as part of this release, which should fix autocompletion for all CLIs using `visible_alias` option.

Reviewed By: ukautz

Differential Revision: D58283249

fbshipit-source-id: 4a48e86c47519431913c34880a132049b5522e7b
facebook-github-bot pushed a commit to facebookexperimental/rust-shed that referenced this pull request Jun 10, 2024
Summary: Upgrading clap to new version (released yesterday). My motivation for doing it now is that my PR clap-rs/clap#5476 has been released as part of this release, which should fix autocompletion for all CLIs using `visible_alias` option.

Reviewed By: ukautz

Differential Revision: D58283249

fbshipit-source-id: 4a48e86c47519431913c34880a132049b5522e7b
facebook-github-bot pushed a commit to facebook/sapling that referenced this pull request Jun 10, 2024
Summary: Upgrading clap to new version (released yesterday). My motivation for doing it now is that my PR clap-rs/clap#5476 has been released as part of this release, which should fix autocompletion for all CLIs using `visible_alias` option.

Reviewed By: ukautz

Differential Revision: D58283249

fbshipit-source-id: 4a48e86c47519431913c34880a132049b5522e7b
facebook-github-bot pushed a commit to facebook/hhvm that referenced this pull request Jun 10, 2024
Summary: Upgrading clap to new version (released yesterday). My motivation for doing it now is that my PR clap-rs/clap#5476 has been released as part of this release, which should fix autocompletion for all CLIs using `visible_alias` option.

Reviewed By: ukautz

Differential Revision: D58283249

fbshipit-source-id: 4a48e86c47519431913c34880a132049b5522e7b
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.

Subcommand aliases not included in completion scripts
2 participants