Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Command wrappers in $PATH, and re-executing the "correct" value



On Sat, May 21, 2022 at 8:30 AM Zach Riggle <zachriggle@xxxxxxxxx> wrote:
>
> I find that very frequently I write a wrapper for some CLI tool, say "foobar".
>
> I have a wrapper script ~/bin/foobar, which will be invoked for the
> command "foobar".  What my wrapper script does is immaterial, but
> it eventually executes the /usr/local/bin/foobar with some set of
> arguments.

You can try this:

    #!/usr/bin/zsh
    #
    # usage: next-command <command> [arg]..

    path=(${path:#${commands[$1]:h}}) "$@"

Put this in a file named next-command somewhere in PATH. Then invoke
it as `next-command foobar` from within foobar or from anywhere else.
Nice things about this solution:

- It's a script rather than an alias or a function, so it can be
invoked from anywhere.
- You can peel several levels of wrappers.

Bad things about it:

- If /usr/local/bin/foobar invokes baz, which is supposed to resolve
as ~/bin/baz, it won't be found (or perhaps will be found in a
different directory).

Roman.




Messages sorted by: Reverse Date, Date, Thread, Author