On Ruby's Thor, How to show the command usage from within the application -


i'm building cli using ruby , thor , print on screen command usage if no option passed.

something on line of pseudo code bellow:

class test < thor   desc 'test', 'test'   options :run_command    def run_command     if options.empty?       # print usage     end   end end 

im using following hack (and i'm not proud of it! =p):

class test < thor   desc 'test', 'test'   options :run_command    def run_command     if options.empty?       puts `my_test_command run_command`     end   end end 

what proper way this?

you can use command_help display information command:

require 'thor'  class test < thor   desc 'run_command --from=from', 'test usage help'   option :from   def run_command     unless options[:from]       test.command_help(thor::base.shell.new, 'run_command')       return     end      puts "called command #{options[:from]}"   end end  test.start 

and running no options:

$ ruby example.rb run_command usage:   example.rb run_command --from=from  options:   [--from=from]  test usage 

and running option:

$ ruby example.rb run_command --from=somewhere called command somewhere 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -