cmd- comma to separate parameters Compared to space?

眉间皱痕 提交于 2021-02-18 22:41:13

问题


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..

I know that when doing

c:\>program a b c

there are 4 parameters [0]=program [1]=a [2]=b [3]=c

According to hh ntcmds.chm concepts..

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 a b

but

c:\>program a,b,c gives parameters [0]=program [1]=a,b,c

So do some? or all? windows commands use ; and , ? and is that interpretation within the code of each command, or done by the shell like with space?

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,e.g. you can do explorer /e,.

but 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. 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?


回答1:


There are two differences here between Unix and Windows:

  • Internal commands such as DIR are built into the 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. The shell parses redirects and pipes, then passes the rest of the command line to the program in one string

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.

I've never seen a C program that uses , or ; as a command line separator. I was aware of the special case for explorer /e,., but I'd never seen the dir a,b example until just now.




回答2:


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). I guess (note: speculation) this is some sort of backwards compatibility that goes back into the DOS or even CP/M days. 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. 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).



来源:https://stackoverflow.com/questions/4156438/cmd-comma-to-separate-parameters-compared-to-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!