问题
all! I'm trying to compile a program using PowerShell, but the command is being parsed strangely. This command executes correctly in cmd.exe:
dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d" "src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d" -D -O -release
But PowerShell executes it as: (blue, navy, purple texts as they appear in PowerShell ISE)
dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d" "src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d" -D -O -release
This spits the following error:
The string starting:
At line:1 char:147
+ dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d"
"src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d <<<< " -D -O -release
is missing the terminator: ".
At line:1 char:163
So it seems to be interpreting a period as a quote. This is peculiar. Has anyone else had this problem with PowerShell?
Things I've tried:
- escaping quotes
- making sure all quotes are "straight quotes" and not angled
- putting a space before quotes (parses correctly, but the program doesn't understand the arguments.)
Thanks, Charles.
回答1:
I believe this should do the trick (newlines added for clarity only, and removal of extra quotes):
dmd '-od"bin"' '-of"bin\convHull.exe"' '-I"src"'
src\concSort.d src\fileParser.d src\main.d src\pointLogic.d src\quickHull.d src\stupidHull.d
-D -O -release
Note that in the case where a quote (") is to be passed as part of the argument itself, I surrounded the entire argument with single quotes ('). From the experimentation below it can be seen that only -of"..."
needs the quotes about it.
Happy coding.
I can't find a good reference on this exact production, however note the following parsings:
-x"w." -> error: " expected (last " is special) -x"w."" -> -x"w and ."" (the . starts a new token and the " in that starts a quote; however, the quotes are not removed) '-x"w."' -> -x"w." (extra quote fine, neither special) -x"w" -> -x"w" (no . and " not special) -x"w"" -> -x"w"" (no . and " not special) a".b" -> a.b (didn't start with `-`, quotes removed) a".b -> error: " expected (" is special)
So it does indeed appear to have something to do with the .
and -
combination (and it might not be exclusive). From the above I believe that a token starting with -
does not include the .
character as a valid character in the token so the lexer terminates said token and starts a new token -- easily provable with a good EBNF reference, which I don't have.
The best I can find is Appendix C: The PowerShell Grammar:
The ParameterToken rule is used to match cmdlet parameters such as -foo or - boolProp: . Note that this rule will also match --foobar, so this rule has to be checked before the --token rule.
<ParameterToken> = -[:letter:]+[:]{0 |1}
However, this is incomplete at best and does not even include a definition of "letter".
回答2:
I don't have the executable, but this seems to want to work.
$cmd = @'
dmd -od"bin" -of"bin\convHull.exe" -I"src" "src\concSort.d" "src\fileParser.d" "src\main.d" "src\pointLogic.d" "src\quickHull.d" "src\stupidHull.d" -D -O -release
'@
&$cmd
来源:https://stackoverflow.com/questions/6880187/powershell-inconsistent-strange-behavior-with-quote-parsing