简体   繁体   English

cmd-逗号分隔参数与空间相比?

[英]cmd- comma to separate parameters Compared to space?

I have some questions on the subject, of commas compared to space, in delimiting parameters. 在分隔参数时,我对这个主题有一些疑问,比较空格的逗号。

They are questions that C programmers familiar with the cmd prompt, may be able to throw some light on.. 他们是熟悉cmd提示符的C程序员的问题,可能会对它们有所了解。

I know that when doing 我知道这样做的时候

c:\>program a b c

there are 4 parameters [0]=program [1]=a [2]=b [3]=c 有4个参数[0]=program [1]=a [2]=b [3]=c

According to hh ntcmds.chm concepts.. 根据hh ntcmds.chm概念..

Shell overview 壳概述

; and , are used to separate parameters

; or , command1 parameter1;parameter2  Use to separate command parameters.

I see dir a,b gives the same result as dir ab 我看到dir a,b给出与dir ab相同的结果

but

c:\\>program a,b,c gives parameters [0]=program [1]=a,b,c c:\\>program a,b,c给出参数[0] =程序[1] = a,b,c

So do some? 一些人呢? or all? 还是所有? windows commands use ; windows命令使用; and , ? 而且,? and is that interpretation within the code of each command, or done by the shell like with space? 并且是在每个命令的代码中解释,还是像shell一样用空间完成?

And if it is in the code of each command.. how would I know which do it? 如果它在每个命令的代码中......我怎么知道它做了什么? I notice that documentation of explorer.exe mentions the comma,eg you can do explorer /e,. 我注意到explorer.exe的文档提到了逗号,例如你可以做explorer /e,.

but DIR /? 但是DIR /? does not mention it, but can use it. 没有提到它,但可以使用它。 And a typical c program doesn't take , as a delimiter at all.. So is it the case that the shell doesn't use comma to delimit, it uses space. 并且一个典型的c程序根本不作为分隔符。所以shell不使用逗号分隔,它使用空格。 And windows commands that do, do so 'cos they are (all?) written to delimit the parameters the shell has given them further when commas are used? 和windows命令一样,这样做是因为它们(全部?)被写入来分隔shell在使用逗号时进一步给出的参数?

There are two differences here between Unix and Windows: Unix和Windows之间存在两个不同之处:

  • Internal commands such as DIR are built into the shell; 内部命令(如DIR内置于shell中; their command line syntax doesn't have to follow the same rules as for regular programs 他们的命令行语法不必遵循与常规程序相同的规则
  • On Windows, programs are responsible for parsing their own command lines. 在Windows上,程序负责解析自己的命令行。 The shell parses redirects and pipes, then passes the rest of the command line to the program in one string shell解析重定向和管道,然后将其余的命令行传递给一个字符串中的程序

Windows C programs built using Visual Studio use the command line parser in the Microsoft C runtime, which is similar to a typical Unix shell parser and obeys spaces and quotation marks. 使用Visual Studio构建的Windows C程序使用Microsoft C运行时中的命令行解析器,类似于典型的Unix shell解析器并遵循空格和引号。

I've never seen a C program that uses , or ; 我从未见过使用过的C程序,或者; as a command line separator. 作为命令行分隔符。 I was aware of the special case for explorer /e,. 我知道explorer /e,.的特殊情况explorer /e,. , but I'd never seen the dir a,b example until just now. ,但直到现在我才见过dir a,b例子。

Batch files use a comma or semicolon as an alternative argument separator. 批处理文件使用逗号或分号作为备用参数分隔符。

Test batch file: 测试批处理文件:

@echo %1/%2/%3

Test run: 测试运行:

> test.cmd 1,2,3
1/2/3
> test.cmd 1;2 3
1/2/3

And, as you note, dir uses it, copy as well – those are both shell built-ins and probably run through a similar parser like batch files as well (it isn't exactly the same, since you can do things like cd.. or dir/s which aren't possible for anything else). 并且,正如您所注意到的, dir使用它,也可以copy - 这些都是shell内置函数,并且可能也像批处理文件一样运行类似的解析器(它不完全相同,因为您可以执行像cd..这样的操作cd..dir/s这是不可能为别的)。 I guess (note: speculation) this is some sort of backwards compatibility that goes back into the DOS or even CP/M days. 我猜(注意:推测)这是某种向后兼容性,可以追溯到DOS甚至CP / M天。 Nowadays you probably should just use spaces. 现在你可能应该只使用空格。 And as Tim notes, the C runtime dictates certain things about arguments and how they are supposed to be parsed. 正如Tim所说,C运行时决定了有关参数的某些内容以及它们应该如何被解析。 Many other languages/frameworks follow that convention but not necessarily all. 许多其他语言/框架遵循该惯例,但不一定全部。 PowerShell for example has completely different argument handling and this can sometimes be a surprise when interacting with native programs from within it (that being said, PowerShell cmdlets and functions are no programs executable elsewhere, but batch files likewise). 例如,PowerShell具有完全不同的参数处理,当与其中的本机程序交互时,这有时会令人惊讶(可以说,PowerShell cmdlet和函数不是其他地方可执行的程序,同样也是批处理文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM