elvish icon indicating copy to clipboard operation
elvish copied to clipboard

Provide a mechanism for writing safe wrappers to external commands

Open xiaq opened this issue 7 years ago • 3 comments

External commands mix options and arguments together, and this is a common source of errors. The following POSIX shell code breaks when there is any file whose name starts with a dash:

for f in *; do
  ls $f
done

The correct way is to always write ls -- $f, but that is cumbersome.

In Elvish options use the special &name=value syntax and are completely from arguments. It should be possible to wrap external commands. For instance, a "safe" version of ls can be used like this

ls &l # passes the "l" option; calls "e:ls -l"
ls -l # lists the "-l" file; calls "e:ls -- -l"
ls &l $x # calls "e:ls -l -- $x"

xiaq avatar Jun 28 '17 22:06 xiaq

@xiaq i said don't inspire from others -_-

ganeshi4u avatar Jun 30 '17 15:06 ganeshi4u

There is no practical way to make this proposal work. Not least because far too many external commands do not recognize the -- argument idiom to stop parsing options. This would require predicating the rewrite on both the platform and external command being run. Rewriting the external command invocation in this manner is also a spooky action at a distance that is guaranteed to result in confusing behavior; i.e., errors.

Furthermore, how does Elvish know that ls &l should be translated to ls -l -- rather than ls --l --? Even with AI there is probably no way to make this proposal work reliably enough to be the default behavior. What happens if I have a private ls command that has different options than the platform ls command?

This type of magic can be made to work some of the time, but will fail often enough that it causes more problems than it solves.

krader1961 avatar Apr 01 '23 03:04 krader1961

I will also point out that invoking an external command, such as ls, via implicit E:PATH searching assumes that the external command resolves to one whose argument parsing matches the expectations of this proposal. That is obviously invalid.

krader1961 avatar Apr 13 '23 03:04 krader1961