0

Following-up on the last suggestion of a previous answer, I am trying to find a way to disable interpretation of the parameters I want to feed to an executable

Since

D:\program.exe this is a #full sentence

will discard #full sentence as a comment (and I specifically do not want to use quotes), I was hoping that an intermediate function would fix the problem:

function global:note { 
  D:\program.exe  --% @Args
}

The rationale was that I do not want to interpret anything after --% in the course of the execution of program.exe, but still want to replace @Args when running the function note (as part of the execution of the function). Unfortunately, the result is a verbatim

D:\program.exe @Args

(the argument of program.exe is the string @Args)

My question: why isn't @Args expanded in the presence of --%, and the expansion happens without? Isn't --% supposed to stop the interpretation of the command line (i.e. do not interpret the command line for D:\program.exe this is a #full sentence when the arguments of the function are this is a #full sentence)


Following Matthias' comment on splatting, here is what I get in different scenarios:

Case 1

function global:note { 
  D:\program.exe @Args
}

Output when typing note this is a #complete sentence at the PS prompt:

this is a

Case 2

function global:note { 
  D:\program.exe --% @Args
}

Output when typing note this is a #complete sentence at the PS prompt:

@Args
WoJ
  • 27,165
  • 48
  • 180
  • 345
  • Why _and I specifically do not want to use quotes_ ??? – Theo Jan 12 '23 at 10:59
  • @Theo: this is a cli that is intended to quickly capture an input, and process/forward it further. The input will be natural language so I do not want to add extra elements such as quotes. Everything would have been fine if not for that `#` interpretation – WoJ Jan 12 '23 at 11:09
  • 1
    `--%` stops parsing PowerShell syntax and treats the rest of the line as a literal string, so it of course also doesn't parse the `@`-operator and instead treats it as a literal. In any case, running the program through a function isn't going to make any difference here since the arguments are parsed/evaluated before they're passed to the function. Quoting the sentence is the best option. Or read it from stdin. – jkiiski Jan 12 '23 at 11:25
  • @jkiiski: *Or read it from stdin* oh yes - how could I have missed that. Too much backend dev I guess :) Thank you! – WoJ Jan 12 '23 at 11:28
  • @jkiiski: would you mind turning your comment into an answer, and if possible just expand a little about how is `--%` interfering with `@Args`. I have a hard time understanding why the flow is not "I call a function with arguments A and B, they are passed to the function, expanded as A and B, and then the program is ran with `--% A B` which means that A and B will not be interpreted" – WoJ Jan 12 '23 at 11:30
  • As an aside, why not just do `D:\program.exe @Args` (as suggested in the previous answer)? – Mathias R. Jessen Jan 12 '23 at 12:10
  • @MathiasR.Jessen because in such a case I would need to explicitly add `--%` as the first parameter of the command line (I mentioned in an earlier comment that *this is a cli that is intended to quickly capture an input, and process/forward it further. The input will be natural language so I do not want to add extra elements such as quotes*) – WoJ Jan 12 '23 at 12:13
  • You don't need quotes when you're splatting. What happens when you just define the function as `D:\program.exe @Args`? – Mathias R. Jessen Jan 12 '23 at 12:15
  • @MathiasR.Jessen: I updated the question (at the very bottom) with the information you requested – WoJ Jan 12 '23 at 14:30
  • That's a problem with how you _invoke_ the `note` function, the function itself works as expected. How are you receiving the input data in the first place? – Mathias R. Jessen Jan 12 '23 at 14:32
  • @MathiasR.Jessen: I type in the shell prompt `note this is a #complete sentence` (`note` being the name of the function) – WoJ Jan 12 '23 at 14:47

0 Answers0