mviereck / runx

Provide X server on MS Windows with cookie authentication.
MIT License
87 stars 14 forks source link

When sourced, runx -h outputs help and then a load of error messages #14

Open RolandRosier opened 1 year ago

RolandRosier commented 1 year ago

Context, my system is an Ubuntu WSL2 system.

When I source runx and pass the "-h" option, after the help output, I get the following errors:

Command 'main' not found, did you mean:
  command 'maim' from deb maim (5.6.3-1)
  command 'rain' from deb bsdgames (2.17-29)
  command 'man' from deb man-db (2.10.2-1)
  command 'mail' from deb mailutils (1:3.14-1)
Try: apt install <deb name>
finish: command not found

This is because in parse_options, when the "-h" or "--help" option is detected, the finish function is called, which uses "$Sourced" to detect that the script has been sourced and then unsets all the variables and functions defined in the script. After returning from parse_options, the script tries to call the main and then finish functions, but these have already been unset, so the shell tries to find them as standard commands, which it can't. As such, it then tries to suggest packages to install which could provide these functions as standard commands.

Furthermore, if a fix to issue #13 is implemented, moving the parse_options function to before the check_host function, this breaks the "$Sourced" logic because the detection of whether the script is "$Sourced" is performed in the check_host function, so when "-h" is then detected and finish is called, the finish function no longer knows that the script has been "$Sourced".

So:

  1. The detection of "$Sourced" should be moved into the parse_options function, because sourcing the function is a 'kind-of' option - and that detection should be done as early as possible, before there is any chance that the script will need to know that it has been "$Sourced".
  2. The finish function should not be called on detection of "-h" or "--help" by parse_options. Instead, the parse_options function should return as normal, but setting "$Exitcode" to a value (0 is suggested, because outputting help is a 'kind-of' success) and then testing "$Exitcode" to ensure that it is still unset before calling any of the subsequent functionality.
  3. The finish function is then called as the last function call in the script, and it can happily unset everything without there being any resulting errors.