PS2EXE icon indicating copy to clipboard operation
PS2EXE copied to clipboard

How to add -help switch to a compiled script?

Open ajkessel opened this issue 1 year ago • 6 comments

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
}

ajkessel avatar Jul 26 '24 21:07 ajkessel

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

MScholtes avatar Aug 09 '24 16:08 MScholtes

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?

ajkessel avatar Aug 09 '24 16:08 ajkessel

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

MScholtes avatar Aug 20 '24 08:08 MScholtes

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 }`

Oratorian avatar Sep 08 '24 12:09 Oratorian

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

MScholtes avatar Sep 15 '24 15:09 MScholtes

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

ajkessel avatar Sep 16 '24 19:09 ajkessel

Seems to be solved

MScholtes avatar Jan 05 '25 11:01 MScholtes