Class: Laborantin::CliRunner

Inherits:
Runner
  • Object
show all
Defined in:
lib/laborantin/runner.rb

Instance Attribute Summary

Attributes inherited from Runner

#config, #root_dir

Attributes included from Metaprog::Configurable

#config

Instance Method Summary collapse

Methods inherited from Runner

#config_path, #dir, #extra_dir, #file, #initialize, #load_analyses, #load_commands, #load_dir, #load_environments, #load_extra, #load_extra_commands, #load_local_dir, #load_scenarii, #load_user_commands, #prepare, #resultdir, #user_laborantin_dir

Methods included from Metaprog::Configurable

#config_path, #load_config!, #save_config

Constructor Details

This class inherits a constructor from Laborantin::Runner

Instance Method Details

#argv_klass_name(command_klass) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/laborantin/runner.rb', line 166

def argv_klass_name(command_klass)
  ary = command_klass.command_name.split('::').map{|s| s.duck_case}

  # Strip our default command path
  ['laborantin', 'commands'].each do |prefix|
    if ary.first == prefix
      ary = ary[1 .. -1] 
    end
  end

  ary
end

#cli_klass_name(command_klass) ⇒ Object



179
180
181
# File 'lib/laborantin/runner.rb', line 179

def cli_klass_name(command_klass)
  argv_klass_name(command_klass).join(' ')
end

#command_for_argv(argv) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/laborantin/runner.rb', line 158

def command_for_argv(argv)
  Command.sort_by{|c| - argv_klass_name(c).length}.find do |c|
    invokation = argv_klass_name(c)
    # does the first words on the CLI match this command's invokation?
    (argv.slice(0, invokation.size) == invokation)  
  end
end

#parse_opts(argv, klass) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/laborantin/runner.rb', line 183

def parse_opts(argv, klass)
  extra_opts = {}

  parser = OptionParser.new do |opt|
    opt.banner = "Usage: #{File.basename($0)} #{cli_klass_name(klass)} [options...] [args...]"
    opt.banner << "\n" + klass.description
    opt.on_tail('-h', '--help', "show this help and exits") {|val| puts opt ; exit}
    klass.options.each do |arg|
      opt.on(arg.cli_short, arg.cli_long, arg.description_with_default, arg.default_type) {|val| extra_opts[arg.name] = val}
    end
  end

  remaining_args = parser.parse!(argv)

  return remaining_args, extra_opts 
end


200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/laborantin/runner.rb', line 200

def print_generic_help
  proposed = Command.reject{|cmd| cmd.plumbery?}
  parser = OptionParser.new do |opt|
    opt.banner = "Usage: #{File.basename($0)} <command> [options] [args...]\n"
    opt.banner << "Run #{File.basename($0)} <command> --help to have help on a command\n"
    opt.banner << "Known commands are: \n"
    opt.banner << proposed.map do  |klass| 
	  line = klass.description.split("\n").first.chomp
      "\t #{cli_klass_name(klass)} \n\t\t #{line}" 
    end.join("\n")
  end
  puts parser
end

#run_argv(arguments) ⇒ Object

Actually runs the Runner, interpreting argv like an ARGV array of strings.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/laborantin/runner.rb', line 223

def run_argv(arguments)
  argv = arguments.dup
  prepare
  cmd_klass = command_for_argv(argv)
  if cmd_klass
    # removes the heading of argv (i.e. the part corresponding to the class name in most of the cases)
    argv_klass_name(cmd_klass).size.times do
      argv.shift()
    end

    begin
      args, opts = parse_opts(argv, cmd_klass)
      cmd = cmd_klass.new(self)
      cmd.run(args, opts)
    rescue OptionParser::InvalidOption => err
      puts err.message
      puts "use 'labor #{argv_klass_name(cmd_klass)} --help' for help"
    end
  else
    print_generic_help
  end
end

#run_cli(cli, splitter = ' ') ⇒ Object

Actually runs the Runner. This method interprets cli as a command line where cli only contains the arguments (i.e., without the executable name). The arguments are splitted as separated by splitter.



218
219
220
# File 'lib/laborantin/runner.rb', line 218

def run_cli(cli, splitter=' ')
  run_argv(cli.split(splitter))
end