Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why PowerShell Splits Arguments At Dots In Some Cases

Tags:

powershell

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:

  • Argument starts with exactly one minus (-)
  • Argument contains one or more dots
  • Argument does not contain colon (:) before the first dot

What is the reason for the behaviour described above?

like image 308
Janne Rönkkö Avatar asked Sep 14 '25 17:09

Janne Rönkkö


1 Answers

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

like image 58
Keith Hill Avatar answered Sep 17 '25 20:09

Keith Hill