Skip to content

Aliases should support variable arguments or 'rest' arguments #1716

Closed

Description

@yvrhdn

Is your feature request related to a problem? Please describe.
I want to create an alias which will basically add a prefix to a command.

Use case: I use aws-vault to run commands with the correct AWS credentials:

aws-vault exec <my-profile> -- <some command>

I tried to create an alias like this:

alias kaws [cmd] { aws-vault exec <my-profile> -- $cmd }

But this will only capture one 'word'. For example, this fails:

kaws aws s3 ls
error: kaws unexpected s3
- shell:1:14
1 | kaws aws s3 ls
  |          ^^ unexpected argument (try kaws -h)

Describe the solution you'd like
I'd need a way to describe a parameter which captures multiple inputs.

Maybe something like:

alias kaws [...cmd] { aws-vault exec <my-profile> -- $cmd }

Describe alternatives you've considered
@jonathandturner proposed creating an alias with many arguements, with the idea that arguments that are not specified are left blank. But this doesn't work.

alias kaws [arg1 arg2 arg3 arg4 arg5] { .... }
kaws aws s3 ls
error: Alias failed to run
- shell:1:14
1 | kaws aws s3 ls
  | ^^^^ alias failed to run
- shell:1:14
1 | kaws aws s3 ls
  | ---- Type Error
(expected:
string,
actual:
nothing)

Additional context

Activity

  1. added
    category:enhancement
    on May 6, 2020
  2. fdncred commented on May 6, 2020

    @fdncred
    Contributor

    Here's another similar example.

    Alias:
    alias build-profile [arg1 arg2 arg3] { with-env [RUSTFLAGS '-C debuginfo=2 -C opt-level=0 -C link_args=/PROFILE'] { cargo build $arg1 $arg2 $arg3 }}

    Error:

    error: Alias failed to run
    - shell:1:13
    1 | build-profile
      | ^^^^^^^^^^^^^ alias failed to run
    - shell:1:13
    1 | build-profile
      | ------------- Type Error
    (expected:
    string,
    actual:
    nothing)```
    
  3. sophiajt commented on May 6, 2020

    @sophiajt
    Contributor

    Can you try it again with #1722 and see if that helps the issue? It might take a few tries, but this approach is what seemed to make sense initially

  4. fdncred commented on May 7, 2020

    @fdncred
    Contributor

    @jonathandturner my alias appears to be working with this change.

  5. github-actions commented on Jun 24, 2021

    @github-actions

    This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

  6. github-actions commented on Jul 7, 2021

    @github-actions

    This issue has been marked stale for more than 10 days without activity. Closing this issue, but if you find that the issue is still valid, please reopen.

  7. yashamon commented on Nov 22, 2022

    @yashamon

    What is the the status of this? Passing variables to aliases no longer appears possible.
    alias ls [$dir] = ls $dir
    is giving an error.

  8. fdncred commented on Nov 22, 2022

    @fdncred
    Contributor

    Custom commands is what you want.

  9. yashamon commented on Nov 22, 2022

    @yashamon
  10. fdncred commented on Nov 22, 2022

    @fdncred
    Contributor

    You're right about def being scoped but if you try def-env it should work for your cd custom command. Sorry for not making that clear.

  11. yashamon commented on Nov 22, 2022

    @yashamon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions