MScholtes / PS2EXE

Module to compile powershell scripts to executables
Other
1.18k stars 197 forks source link

How to add -help switch to a compiled script? #136

Open ajkessel opened 1 month ago

ajkessel commented 1 month ago

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
}
MScholtes commented 1 month ago

Hello @ajkessel,

there are two errors in your script:

  1. $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)
  2. Get-Help does not work with executables as parameter

Greetings

Markus

ajkessel commented 1 month ago

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?

MScholtes commented 4 weeks ago

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

Oratorian commented 1 week ago

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 : Specify the IP address (default is taken from environment). --switch2 : Specify the port (default is 80). --switch3 : Specify the secret key (required for authentication). "@ exit # Exit after showing help }`

MScholtes commented 2 days ago

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

ajkessel commented 21 hours ago

Thanks very much! I'll give it a try.