Class: Simple::CLI::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/cli/runner.rb,
lib/simple/cli/runner.rb

Overview

A Runner object manages running a CLI application module with a set of string arguments (usually taken from ARGV)

Defined Under Namespace

Classes: CommandHelp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Runner

Returns a new instance of Runner.



19
20
21
# File 'lib/simple/cli/runner.rb', line 19

def initialize(app)
  @app = app
end

Instance Attribute Details

#subcommandObject

Returns the value of attribute subcommand.



32
33
34
# File 'lib/simple/cli/runner.rb', line 32

def subcommand
  @subcommand
end

Class Method Details

.run(app, *args) ⇒ Object



15
16
17
# File 'lib/simple/cli/runner.rb', line 15

def self.run(app, *args)
  new(app).run(*args)
end

Instance Method Details

#args_with_options(args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/simple/cli/runner.rb', line 123

def args_with_options(args)
  r = []
  options = {}
  while (arg = args.shift)
    case arg
    when /^--(.*)=(.*)/ then options[$1.to_sym] = $2
    when /^--no-(.*)/   then options[$1.to_sym] = false
    when /^--(.*)/      then options[$1.to_sym] = true
    else r << arg
    end
  end

  r << options unless options.empty?
  r
end

#binary_nameObject



156
157
158
# File 'lib/simple/cli/runner.rb', line 156

def binary_name
  $0.gsub(/.*\//, "")
end

#command_to_string(sym) ⇒ Object



139
140
141
# File 'lib/simple/cli/runner.rb', line 139

def command_to_string(sym)
  sym.to_s.tr("_", ":")
end

#commandsObject



147
148
149
# File 'lib/simple/cli/runner.rb', line 147

def commands
  @app.public_instance_methods(false).grep(/^[_a-zA-Z0-9]+$/)
end

#do_help!(subcommand = nil) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/simple/cli/runner.rb', line 65

def do_help!(subcommand = nil)
  if !subcommand
    help!
  else
    help_subcommand!(subcommand)
  end
end

#extract_default_flags!(args) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/simple/cli/runner.rb', line 23

def extract_default_flags!(args)
  args.reject! do |arg|
    case arg
    when "--verbose", "-v" then logger.level = Logger::DEBUG
    when "--quiet", "-q"   then logger.level = Logger::WARN
    end
  end
end

#has_subcommands?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/simple/cli/runner.rb', line 61

def has_subcommands?
  commands.length > 1
end

#help(command) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/simple/cli/runner.rb', line 34

def help(command)
  if command
    run "help", command
  else
    run "help"
  end
end

#help!Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/simple/cli/runner.rb', line 160

def help!
  # collect help information on individual comments; when not on DEBUG
  # level skipping the commands that don't jave a command help.
  command_helps = commands.inject({}) do |hsh, sym|
    edoc = CommandHelp.new(@app, sym)
    next hsh if !edoc.head && logger.level != ::Logger::DEBUG

    hsh.update sym => help_for_command(sym)
  end

  # build a lambda which prints a help line with nice formatting
  max_length = command_helps.values.map(&:length).max
  print_help_line = lambda do |cmd, description|
    if description
      STDERR.puts format("    %-#{max_length}s    # %s", cmd, description)
    else
      STDERR.puts format("    %-#{max_length}s", cmd)
    end
  end

  # print help for commands
  STDERR.puts "Usage:\n\n"

  command_helps.keys.sort.each do |sym|
    command_help = command_helps[sym]
    edoc = CommandHelp.new(@app, sym)
    print_help_line.call command_help, edoc.head
  end

  # print help for default commands

  STDERR.puts "\n    Default options include:\n\n  DOC\n\n  print_help_line.call \"\#{binary_name} [ --verbose | -v ]\", \"run on DEBUG log level\"\n  print_help_line.call \"\#{binary_name} [ --quiet | -q ]\", \"run on WARN log level\"\n\n  STDERR.puts <<~DOC\n\n    Other commands:\n\n  DOC\n\n  print_help_line.call \"\#{binary_name} help [ subcommand ]\", \"print help on a specific subcommand\"\n  print_help_line.call \"\#{binary_name} help -v\", \"show help for internal commands as well\"\n\n  STDERR.puts \"\\n\"\n\n  exit 1\nend\n"

#help_for_command(sym) ⇒ Object



151
152
153
154
# File 'lib/simple/cli/runner.rb', line 151

def help_for_command(sym)
  cmd = string_to_command(sym)
  CommandHelp.new(@app, cmd).interface(binary_name, cmd, include_subcommand: has_subcommands?)
end

#help_subcommand!(subcommand) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/simple/cli/runner.rb', line 73

def help_subcommand!(subcommand)
  edoc = CommandHelp.new(@app, string_to_command(subcommand))

  puts "    \#{help_for_command(subcommand)}\n\n    \#{edoc.full}\n  MSG\n\n  unless has_subcommands?\n\n    STDERR.puts <<~MSG\n\n      Default options include:\n\n      \#{binary_name} [ --help | -h ]                  ... print this help\n      \#{binary_name} [ --verbose | -v ]               ... run on DEBUG log level\n      \#{binary_name} [ --quiet | -q ]                 ... run on WARN log level\n    MSG\n  end\n  exit 1\nend\n"

#loggerObject



96
97
98
# File 'lib/simple/cli/runner.rb', line 96

def logger
  Simple::CLI.logger
end

#on_exception(e) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/simple/cli/runner.rb', line 100

def on_exception(e)
  raise(e) if Simple::CLI.logger.level == Logger::DEBUG

  verbosity_hint = "Backtraces are currently silenced. Run with --verbose to see backtraces."

  case e
  when ArgumentError
    logger.error e.message
    logger.warn verbosity_hint
    if subcommand
      help_subcommand! subcommand
    else
      help!
    end
  else
    msg = e.message
    msg += " (#{e.class.name})" unless $!.class.name == "RuntimeError"
    logger.error msg
    logger.warn verbosity_hint
    exit 2
  end
end

#run(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/simple/cli/runner.rb', line 42

def run(*args)
  extract_default_flags!(args)

  @instance = Object.new.extend(@app)
  command_name = args.shift || help!
  command = string_to_command(command_name)

  if command == :help
    do_help!(*args)
  elsif commands.include?(command)
    self.subcommand = command
    @instance.run! command, *args_with_options(args)
  else
    help!
  end
rescue StandardError => e
  on_exception(e)
end

#string_to_command(s) ⇒ Object



143
144
145
# File 'lib/simple/cli/runner.rb', line 143

def string_to_command(s)
  s.to_s.tr(":", "_").to_sym
end