zsh-users
 help / color / mirror / code / Atom feed
* Command wrappers in $PATH, and re-executing the "correct" value
@ 2022-05-21  6:28 Zach Riggle
  2022-05-21  7:12 ` Roman Perepelitsa
  0 siblings, 1 reply; 4+ messages in thread
From: Zach Riggle @ 2022-05-21  6:28 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1128 bytes --]

I find that very frequently I write a wrapper for some CLI tool, say
"foobar".

Let's say lives at e.g. /usr/local/bin/foobar, and $path is ( ... ~/bin ...
/usr/local/bin ).

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.

As best I can tell, there are three ways to make this work neatly:

   - Implement "my" foobar as a function, and use "command foobar"
      - How do I make this an autoloadable module, which doesn't need to
      use e.g.
      "autoload foobar && foobar"
   - Remove ~/bin from $path, and add it to the end
      - This might break other things where system binaries take over
   - Some cool tricks with /usr/bin/env and such I haven't thought of

I've already adopted autoloadable functions via $fpath and using "command
foobar" inside the function, but I was curious if there's another way.  I
expect that removing ${0:h} and ${0:A:h} from $path are the most obvious
answers, but I didn't know if there's anything easier.

*Zach Riggle*

[-- Attachment #2: Type: text/html, Size: 1535 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Command wrappers in $PATH, and re-executing the "correct" value
  2022-05-21  6:28 Command wrappers in $PATH, and re-executing the "correct" value Zach Riggle
@ 2022-05-21  7:12 ` Roman Perepelitsa
  2022-05-21  7:20   ` Mikael Magnusson
  0 siblings, 1 reply; 4+ messages in thread
From: Roman Perepelitsa @ 2022-05-21  7:12 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On Sat, May 21, 2022 at 8:30 AM Zach Riggle <zachriggle@gmail.com> 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.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Command wrappers in $PATH, and re-executing the "correct" value
  2022-05-21  7:12 ` Roman Perepelitsa
@ 2022-05-21  7:20   ` Mikael Magnusson
  2022-05-21  7:31     ` Roman Perepelitsa
  0 siblings, 1 reply; 4+ messages in thread
From: Mikael Magnusson @ 2022-05-21  7:20 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zach Riggle, Zsh Users

On 5/21/22, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> On Sat, May 21, 2022 at 8:30 AM Zach Riggle <zachriggle@gmail.com> 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}}) "$@"

I'm using this in mine (wrapper script of same name as target binary),
path[(R)$0:h]=() exec $0:t "$@"

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Command wrappers in $PATH, and re-executing the "correct" value
  2022-05-21  7:20   ` Mikael Magnusson
@ 2022-05-21  7:31     ` Roman Perepelitsa
  0 siblings, 0 replies; 4+ messages in thread
From: Roman Perepelitsa @ 2022-05-21  7:31 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zach Riggle, Zsh Users

On Sat, May 21, 2022 at 9:20 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> On 5/21/22, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> >
> >     path=(${path:#${commands[$1]:h}}) "$@"
>
> I'm using this in mine (wrapper script of same name as target binary),
> path[(R)$0:h]=() exec $0:t "$@"

There is a difference in behavior if the wrapper directory is listed
in PATH more than once. My version removes all of them, while yours
removes only the **last** instance. I can see a point being made for
removing the **first** but removing the last seems wrong. Is this
intended?

Roman.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-05-21  7:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-21  6:28 Command wrappers in $PATH, and re-executing the "correct" value Zach Riggle
2022-05-21  7:12 ` Roman Perepelitsa
2022-05-21  7:20   ` Mikael Magnusson
2022-05-21  7:31     ` Roman Perepelitsa

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).