PS2EXE
PS2EXE copied to clipboard
How to add -help switch to a compiled script?
My script has a -help option which dumps the help screen, as shown in the excerpt below. But when compiled and run as an EXE, -help shows the PowerShell help, not the script help. Is there a correct way to do this?
Param(
[Parameter(Mandatory = $false, HelpMessage = "Display this help message")][switch]$help
)
if ($help) {
Get-Help $MyInvocation.InvocationName -detailed
exit
}
Hello @ajkessel,
there are two errors in your script:
- $MyInvocation has different content in executables than in scripts. "$MyInvocation.InvocationName" is empty in a compiled script (see remarks here: https://github.com/MScholtes/PS2EXE#script-variables)
- Get-Help does not work with executables as parameter
Greetings
Markus
Thanks for the tips. So is there a recommended "best practice" way to implement -help with a compiled script? Just manually process the switch and output the help, rather than relying on PowerShell's built-in functionality that converts parameter descriptions to a help message?
Hello @ajkessel,
I'm sorry to say that I found no easy way to implement an "automatic" help. I will further investigate this but I guess a solution will require an extension to PS2EXE.
Interesting is that the project PS2EXE exists for several years now and you are the first to realise that there is a basic function missing.
Greetings
Markus
Try this approach
@ajkessel
`if ($args -contains '-help' -or $args -contains '/help' -or $args -contains '--help') { Write-Host @" Usage: script.exe --help or /help
Parameters:
---help :Show this help information.
--switch1
Hello @ajkessel,
I uploaded a new version of PS2EXE today. Executables generated with the new version have a parameter -? now that shows the help of the compiled Powershell script. "-? -detailed", "-? -examples" or "-? -full" can be used to get the appropriate help text.
Greetings
Markus
Thanks very much! I'll give it a try.
Seems to be solved