RFC: Shell Aliases and Arguments
Steve Losh
steve at stevelosh.com
Fri Aug 20 15:52:01 UTC 2010
Now that f853873fc66d (aliases: provide more flexible ways to work with shell alias arguments) is in main, I've been using shell aliases more and noticed a tricky issue. I talked with Martin on IRC and we came up with a fix but I later realized it isn't that simple.
Here's an example of the problem:
[alias]
lsroot = !ls $@ `hg root`
$ hg lsroot
file1 file2 ...
$ hg lsroot -l
hg lsroot: option -l not recognized
...
The issue is that the '-l' is interpreted as an argument to the aliased command by dispatch._parse(), and that command doesn't have a -l option.
You can get around this by using '--', but it's a pain to type:
$ hg lsroot -- -l
... works fine ...
Martin and I were discussing global options like '--repository' and eventually decided that it would make things simpler and easier to understand if global options were simply not allowed when running shell aliases. Example: 'hg lsroot -R foo' would run 'ls -R foo' instead of switching the repo to 'foo' and running 'ls'.
We also talked about adding a "debugalias" command (or something similar) that would let you use global options again, but with a more strict/verbose syntax.
The problem with this solution is that you can't tell whether an alias is an alias or not (let alone a shell alias specifically) until you've parsed the global options (or at least -R).
The current code parses out the global opts before it even looks up the command. I don't know the best way to get around this, but I'm guessing it wouldn't be pretty.
I'm looking for comments on how people think this should work (and hopefully how we can implement it). There are a few options Martin and I talked about:
* No global options allowed.
$ hg [shellargs] lsroot [shellargs]
* Global options allowed before the alias name only:
$ hg [globalopts] lsroot [shellargs]
* Mixed options allowed everywhere, with global options being
pulled out before they get to the shell. Use -- to pass options
that happen to be global opts to the shell.
$ hg [mixedopts] lsroot [mixedopts] -- [shellargs]
More information about the Mercurial-devel
mailing list