简体   繁体   English

是否可以从程序中检索 Powershell shell 中的#argument?

[英]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.只有thisisa被检索。 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.

相关问题 是否可以从 C# 运行 PowerShell shell 并使其保持运行,同时(同步)在其中执行多个 PowerShell 脚本? - Is it possible to run the PowerShell shell from C# and keep it running, while (synchronously) executing multiple PowerShell scripts within it? Powershell 正在从程序参数中删除逗号 - Powershell is removing comma from program argument 从传递参数的外部程序调用 Powershell 脚本 - Calling Powershell script from external program passing an argument 从Powershell调用一个带有非常长的可变参数列表的程序? - Call a program from Powershell w/ very long, variable argument list? 使用 PowerShell 中的 WinSCP 检索过去一小时内修改的文件 - Using WinSCP from PowerShell to retrieve files modified within the last hour 有没有办法从函数中检索PowerShell函数名? - Is there a way to retrieve a PowerShell function name from within a function? 在Powershell中,如何传递IEnumerable参数? - From within Powershell, how do I pass an IEnumerable argument? PowerShell 异常从另一个函数中使用“0”参数调用“Start” - PowerShell Exception calling “Start” with “0” argument(s) from within another function 是否可以从过滤器中终止或停止 PowerShell 管道 - Is it possible to terminate or stop a PowerShell pipeline from within a filter 是否可以从Cake任务中调用powershell命令 - Is it possible to call powershell command from within Cake task
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM