hg extdiff -p 和 %~dp0

发布于 2024-12-07 01:22:36 字数 632 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2024-12-14 01:22:37

%~dp0 技巧是一个弥天大谎。它实际上并不是一个神奇的变量,它只是对 %0 (或者你将 ~dp 放在前面的任何变量)的操作。它只是获取该变量中的任何字符串,并告诉您其驱动器和路径组件是什么。如果该字符串只是一个像“myprogram”这样的名称,那么它表示“好吧,没有驱动器和路径的文件名假定位于当前目录中”。

因此 %~dp0 技巧仅在以下情况下才有效:

a)您已通过其全名启动了脚本或
b) 你碰巧在它所在的目录中

在这种情况下,你运行:

hg extdiff -p myprogram

gets 变成对 Windows 的以下调用:

CreateProcess(NULL, "cmd.exe /c myprogram some diff args", ..., "c:/some/temp/path", ...)

这在道德上相当于打开 shell 并运行:

C:\>cd c:\some\temp\path
C:\some\temp\path>myprogram some diff args
%0 is myprogram
%~dp0 is C:\some\temp\path

我建议传递你的工具的完整程序名称,例如通过您的 .hgrc :

[extdiff]
myprogram=c:/tools/myprogram.cmd

但请注意,这可能会因文件名中存在空格而感到困惑,您可能需要尝试引用。

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:

hg extdiff -p myprogram

gets turned into the following call to Windows:

CreateProcess(NULL, "cmd.exe /c myprogram some diff args", ..., "c:/some/temp/path", ...)

which is morally equivalent to opening a shell and running:

C:\>cd c:\some\temp\path
C:\some\temp\path>myprogram some diff args
%0 is myprogram
%~dp0 is C:\some\temp\path

I recommend passing the full program name for your tool like this via your .hgrc:

[extdiff]
myprogram=c:/tools/myprogram.cmd

But be warned, this can get confused by the presence of spaces in your filename, you may need to experiment with quoting.

ぶ宁プ宁ぶ 2024-12-14 01:22:37

您可以尝试使用 %~dp$PATH:0,但您需要始终指定扩展名。例如,对于以下 test.cmd

rem test.cmd
@echo dp0 == %~dp0
@echo dp$PATH:0 == %~dp$PATH:0

这是来自 d:\hg 文件夹的两个示例运行:

$ hg extdiff -p test.cmd
dp0 == c:\Users\estefan\AppData\Local\Temp\extdiff.pamj6n\
dp$PATH:0 == k:\home\Scripts\

$ hg extdiff -p test
dp0 == c:\Users\estefan\AppData\Local\Temp\extdiff.dgp0qz\
dp$PATH:0 ==

来自 http://ss64.com/nt/syntax-args.html

%~$PATH:1 搜索 PATH 环境变量并将 %1 展开为找到的第一个匹配项的完全限定名称。

You can try using %~dp$PATH:0, but you need to always specify the extension. For example, for the following test.cmd:

rem test.cmd
@echo dp0 == %~dp0
@echo dp$PATH:0 == %~dp$PATH:0

these are two sample runs from the d:\hg folder:

$ hg extdiff -p test.cmd
dp0 == c:\Users\estefan\AppData\Local\Temp\extdiff.pamj6n\
dp$PATH:0 == k:\home\Scripts\

$ hg extdiff -p test
dp0 == c:\Users\estefan\AppData\Local\Temp\extdiff.dgp0qz\
dp$PATH:0 ==

From http://ss64.com/nt/syntax-args.html:

%~$PATH:1 Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文