davetron5000 / gli

Make awesome command-line applications the easy way
http://davetron5000.github.io/gli
Apache License 2.0
1.26k stars 102 forks source link

Print logo and help menu during exit #291

Closed KINGSABRI closed 4 years ago

KINGSABRI commented 4 years ago

Hi I could not find a way from the wiki to print the script logo only when the help menu shows up. How to print the help menu as we do with OptionParse. I tried help_now! in the main app and from within the command itself and I get an error similar to

Traceback (most recent call last):
        3: from bin/jwtear:16:in `<main>'
        2: from bin/jwtear:17:in `<module:JWTear>'
        1: from bin/jwtear:39:in `<module:CLI>'
/var/lib/gems/2.5.0/gems/gli-2.19.0/lib/gli/app.rb:235:in `help_now!': parse error:  (OptionParser::ParseError)

Thanks!

davetron5000 commented 4 years ago

Can you share the code to your app at all? help_now raises an exception, but the intention is that the exception is caught and prints out the help instead. But as you can see, there are ways to prevent that and the error will bubble up.

KINGSABRI commented 4 years ago

Hi @davetron5000 Yes, the code is here (https://github.com/KINGSABRI/jwtear)

davetron5000 commented 4 years ago

Ah, I see the issue. Because you've overridden on_error, GLI won't do its default handling if you don't return true from the block. Looks like you are exiting directly from there, but you don't have to do that.

Check out the docs to see how it works, but basically if you return true from on_error, it will use GLI's custom error handling and should print out the help text.

KINGSABRI commented 4 years ago

Great, Thank you. Regarding the logo, I want to print the logo only on the help not on every command executing

davetron5000 commented 4 years ago

If you mean what OptionParse calls the "banner", it looked the code is putsing it every time.

KINGSABRI commented 4 years ago

correct, and that's the problem. I want the banner to be printed only when the help menu is printed

davetron5000 commented 4 years ago

Yeah, sorry if I wasn't more clear. You need to remove that puts statement from your app that I linked to above. That will keep it printing the banner every time.

Then, change your on_error block so that it returns true. That will print the banner (and other help output) when you call help_now! in your code.

KINGSABRI commented 4 years ago

Thank you @davetron5000 for your instant support. It worked for me now

For people who are facing the same case, here is the solution.

on_error do |exception|
  puts banner
  # ... your custom exception handling code ...
  true
end