Why PowerShell Splits Arguments At Dots In Some Cases

I have the following program (print-args.exe):

#include <iostream>
int main(int argc, char** argv)
{
  for( int i = 1; i < argc; ++i )
  {
    std::cout << "Arg " << i << ": " << argv[i] << std::endl;
  }
  return 0;
}

The arguments having dot (.) are split in some cases into two arguments:

PS C:\print-args> .\print-args.exe -text.txt /text.txt -path=c:\text.txt --foo=value.txt
Arg 1: -text
Arg 2: .txt
Arg 3: /text.txt
Arg 4: -path=c:\text.txt
Arg 5: --foo=value.txt

When run from cmd.exe the arguments are as expected:

C:\print-args>.\print-args.exe -text.txt /text.txt -path=c:\text.txt --foo=value.txt
Arg 1: -text.txt
Arg 2: /text.txt
Arg 3: -path=c:\text.txt
Arg 4: --foo=value.txt

The splitting of an argument happens when:

What is the reason for the behaviour described above?

Upvotes: 4

Views: 457

Answers (1)

Keith Hill
Keith Hill

Reputation: 202062

In PowerShell parameter parsing -<alphachar> is the name of a parameter. . isn't allowed in the name of the parameter so PowerShell apparently considers that the beginning of the parameter's argument. If you want PowerShell to dumb down its parsing use --% e.g.:

.\print-args.exe --% -text.txt /text.txt -path=c:\text.txt --foo=value.txt

You can read about --% in the man topic about_parsing. BTW, we have a command in PSCX just for this sort of debugging called echoargs.exe. :-)

Upvotes: 6

Related Questions