Class: EnfCli::EnfThor

Inherits:
Thor
  • Object
show all
Defined in:
lib/enfthor.rb

Class Method Summary collapse

Class Method Details

.capture_stdoutObject



71
72
73
74
75
76
77
# File 'lib/enfthor.rb', line 71

def capture_stdout
  $stdout = StringIO.new
  yield
  text = $stdout.string
  $stdout = STDOUT
  text
end

.command_help(shell, command_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/enfthor.rb', line 89

def command_help(shell, command_name)
  meth = normalize_command_name(command_name)
  command = all_commands[meth]
  handle_no_command_error(meth) unless command

  ## HACK: format to display proper help message for help command run from shell
  banner_text = banner(command)
  banner_text = banner_text.gsub(/enfcli /, "") unless self == EnfCli::CLI

  shell.say "Usage:"
  shell.say "  #{banner_text}"
  shell.say
  class_options_help(shell, nil => command.options.values)
  if command.long_description
    shell.say "Description:"
    shell.print_wrapped(command.long_description, :indent => 2)
  else
    shell.say command.description
  end
end

.handle_argument_error(command, error, args, arity) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/enfthor.rb', line 79

def handle_argument_error(command, error, args, arity)
  begin
    super(command, error, args, arity)
  rescue Thor::InvocationError => e
    msg = "#{e}"
    msg = msg.gsub(/enfcli /, "") unless self == EnfCli::CLI
    raise Thor::InvocationError, msg
  end
end

.help(shell, subcommand = false) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/enfthor.rb', line 110

def help(shell, subcommand = false)
  list = printable_commands(true, subcommand)
  Thor::Util.thor_classes_in(self).each do |klass|
    list += klass.printable_commands(false)
  end

  # Remove this line to disable alphabetical sorting
  list.sort! { |a, b| a[0] <=> b[0] }

  # Add this line to remove the help-command itself from the output
  list.reject! { |l| l[0].split[1] == "help" or l[0].split[2] == "help" }

  # Capture help message in a variable
  help_text = capture_stdout {
    shell.say "Commands:"
    shell.print_table(list, :indent => 2, :truncate => true)
    shell.say
    class_options_help(shell)
  }

  ## HACK: format to display proper help message for help command run from shell
  help_text = help_text.gsub(/enfcli /, "") unless self == EnfCli::CLI

  # Print the actual help message
  shell.say help_text

  # Add this line if you want to print custom text at the end of your help output.
  # (similar to how Rails does it)
  shell.say "All commands can be run with -h (or --help) for more information."
end