Skip to content

Instantly share code, notes, and snippets.

@codeword
Last active October 12, 2017 17:18
Show Gist options
  • Save codeword/f2d33edc566b8bf9de304b02179b177c to your computer and use it in GitHub Desktop.
Save codeword/f2d33edc566b8bf9de304b02179b177c to your computer and use it in GitHub Desktop.
Allows `--help` option anywhere in command line
class ThorHelpCatcher
def self.start(thor_clazz, args)
help_flag = (args & %w[--help -h])[0]
return thor_clazz.new.help(args.index(help_flag) == 0 ? args[1] : args[0]) if help_flag
thor_clazz.start(args)
end
end
@codeword
Copy link
Author

codeword commented Oct 12, 2017

usage:

class SomeCli < Thor
  class_option :help, alias: '-h', desc: 'Describe available commands or one specific command', type: :boolean

  desc 'foo', 'does foo'
  def foo; end
  desc 'bar', 'does bar'
  def bar; end
end
ThorHelpCatcher.start(SomeCli, ARGV)

this will look like:

> my-cli --help
Commands:
  my-cli bar             # does bar
  my-cli foo             # does foo
  my-cli help [COMMAND]  # Describe available commands or one specific command

Options:
  [--help], [--no-help]  # Describe available commands or one specific command
> my-cli --help foo # or my-cli foo --help
Usage:
  my-cli foo

Options:
  [--help], [--no-help]  # Describe available commands or one specific command

does foo

Note: if you want the --help option ([--help], [--no-help] # Describe available commands or one specific command) to show when helps are being shown you can also add a class option to the Thor subclass as in the example above

  class_option :help, alias: '-h', desc: 'Describe available commands or one specific command', type: :boolean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment