Home > other >  Is it possible to retrieve an #argument in a Powershell shell from within a program?
Is it possible to retrieve an #argument in a Powershell shell from within a program?

Time:01-12

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

CodePudding user response:

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
  • Related