hg extdiff -p 和 %~dp0
myprogram.cmd
位于 PATH 中;
myprogram.cmd
使用 %~dp0
来确定它所在的文件夹;
我已将 @echo %~dp0
包含到 myprogram.cmd
中进行调试;
当我从任何地方调用 myprogram.cmd 时,它都能正常工作,显示 myprogram.cmd 所在的文件夹;
当我调用 hg extdiff -p myprogram.cmd 时,它不起作用,显示类似 c:\Users\Username\AppData\Local\Temp\extdiff.3n8op2\ 。
这是 hgrc
文件的相关部分:
[extensions]
hgext.extdiff =
我做错了什么? %~dp0
不应该返回批处理文件的驱动器和路径吗?我该用什么来代替?我是否必须对 Mercurial 存储库应用一些特殊配置?将 myprogram.cmd
的完整路径传递给 hg extdiff -p
不是一个选项,除非它自动完成。
myprogram.cmd
is in PATH;
myprogram.cmd
uses %~dp0
to determine the folder it is in;
I have included @echo %~dp0
into myprogram.cmd
for debugging;
When I call myprogram.cmd
from anywhere, it works perfectly, displaying the folder myprogram.cmd
is in;
When I call hg extdiff -p myprogram.cmd
, it does not work, displaying something like c:\Users\Username\AppData\Local\Temp\extdiff.3n8op2\
.
Here is the related part of hgrc
file:
[extensions]
hgext.extdiff =
What do I do wrong? Should not %~dp0
return drive and path of the batch file? What do I use instead? Do I have to apply some special configuration to Mercurial repository? Passing the full path of myprogram.cmd
to hg extdiff -p
is not an option, unless it is done automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
%~dp0 技巧是一个弥天大谎。它实际上并不是一个神奇的变量,它只是对 %0 (或者你将 ~dp 放在前面的任何变量)的操作。它只是获取该变量中的任何字符串,并告诉您其驱动器和路径组件是什么。如果该字符串只是一个像“myprogram”这样的名称,那么它表示“好吧,没有驱动器和路径的文件名假定位于当前目录中”。
因此 %~dp0 技巧仅在以下情况下才有效:
a)您已通过其全名启动了脚本或
b) 你碰巧在它所在的目录中
在这种情况下,你运行:
gets 变成对 Windows 的以下调用:
这在道德上相当于打开 shell 并运行:
我建议传递你的工具的完整程序名称,例如通过您的 .hgrc :
但请注意,这可能会因文件名中存在空格而感到困惑,您可能需要尝试引用。
The %~dp0 trick is a big lie. It is not actually a magic variable, it's simply a manipulation of %0 (or whichever var you stick the ~dp in front of). It simply takes whatever string is in that variable and tells you what its drive and path components appear to be. If that string is simply a name like "myprogram", it says "well, filenames without drives and paths are assumed to be in the current directory".
So the %~dp0 trick only works if either:
a) you've launched your script by its full name or
b) you happen to be in the directory where it resides
In this case, you run:
gets turned into the following call to Windows:
which is morally equivalent to opening a shell and running:
I recommend passing the full program name for your tool like this via your .hgrc:
But be warned, this can get confused by the presence of spaces in your filename, you may need to experiment with quoting.
您可以尝试使用 %~dp$PATH:0,但您需要始终指定扩展名。例如,对于以下
test.cmd
:这是来自
d:\hg
文件夹的两个示例运行:来自 http://ss64.com/nt/syntax-args.html:
You can try using %~dp$PATH:0, but you need to always specify the extension. For example, for the following
test.cmd
:these are two sample runs from the
d:\hg
folder:From http://ss64.com/nt/syntax-args.html: