简体   繁体   中英

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

I just realized that in some cases only part of the line is retrieved, and this is when there are #parameters :

PS > ./prog.exe this is a #nice sentence

Only this , is and a are retrieved. 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.

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

You can prevent PowerShell from interpreting # as a comment token by explicitly quoting the input arguments:

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

$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:

./prog.exe --% one two three #four five

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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