0

I try certain source codes using PowerShell to extract an password protected archive using 7zip:

This command doesn' work (7zip is an alias for $7zipPath):

& 7zip x "$zipFile" -o "$output" -p $zipFilePassword

I get the this error: Command Line Error: Too short switch:

But when I remove the spaces between the variables -o and -p, the archive can be extracted. This behaviour confuses me with other command line tools like git etc.? Why is it so?

1

1 Answer 1

1

The behavior is specific to 7-Zip (7z.exe) and applies to whatever program (shell) you invoke it from:

Deviating from widely used conventions observed by CLIs such as git, 7z requires that even switches (options) that have mandatory arguments, such as -o and -p, have the argument directly attached to the switch name - no spaces are allowed:

& 7zip x $zipFile -o"$output" -p"$zipFilePassword"

Note that you normally need not enclose variable references in PowerShell in "..." (note how $zipFile isn't), even if they contain spaces. However, in order to attach them directly to switch names, you do.

Alternatively, you could enclose the entire token - switch name and argument - in double quotes:

& 7zip x $zipFile "-o$output" "-p$zipFilePassword"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.