[英]Is it possible to retrieve an #argument in a Powershell shell from within a program?
I am writing a program prog.exe
that retrieves all arguments that are passed to it (in the form of a "sentence", not standalone arguments).我正在编写一个程序prog.exe
,它检索传递给它的所有 arguments(以“句子”的形式,而不是独立的参数)。
I just realized that in some cases only part of the line is retrieved, and this is when there are #parameters
:我刚刚意识到,在某些情况下,只有部分行被检索到,这是在有#parameters
的时候:
PS > ./prog.exe this is a #nice sentence
Only this
, is
and a
are retrieved.只有this
、 is
和a
被检索。 In case I do not use #
I get all of them.如果我不使用#
我会得到所有的。 I presume this is because everything after the #
is interpreted by Powershell as a comment.我认为这是因为#
之后的所有内容都被 Powershell解释为注释。
Is there a way to retrieve everything that is on the command line?有没有办法检索命令行上的所有内容?
If this makes a difference, I code in Go and get the arguments via os.Args[1:]
.如果这会产生影响,我会输入 Go 并通过os.Args[1:]
获取 arguments。
You can prevent PowerShell from interpreting #
as a comment token by explicitly quoting the input arguments:您可以通过显式引用输入 arguments 来防止 PowerShell 将#
解释为注释标记:
./prog.exe one two three '#four' five
A better way exists, though, especially if you don't control the input: split the arguments into individual strings then use the splatting operator @
on the array containing them:但是,存在更好的方法,尤其是在您不控制输入的情况下:将 arguments 拆分为单独的字符串,然后在包含它们的数组上使用展开运算符@
:
$paramArgs = -split 'one two three #four five'
./prog.exe @paramArgs
Finally, using the --%
end-of-parsing token in a command context will cause the subsequent arguments on the same line to be passed as-is , no parsing of language syntax:最后,在命令上下文中使用--%
end-of-parsing 标记将导致同一行上的后续 arguments 按原样传递,不解析语言语法:
./prog.exe --% one two three #four five
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.