class Flo::Cli

Builds the command line parser and executes Flo::Runner using the commands supplied. In general you should be able to create a new instance and then pass in ARGV directly into {#call}.

Attributes

runner_class[R]

Public Class Methods

new(opts={}) click to toggle source

Creates a new CLI runner instance. @option opts [Class] runner_class (Runner) Class of the runner object dependency

# File lib/flo/cli.rb, line 17
def initialize(opts={})
  @runner_class = opts[:runner_class] || Runner
end

Public Instance Methods

call(argv) click to toggle source

Runs a command using args directly from ARGV. @param argv [Array<String>] An array of strings. Typically you would pass in ARGV directly.

The first value should be the name of the command
# File lib/flo/cli.rb, line 25
def call(argv)
  generate_commands.run(argv)
end

Private Instance Methods

generate_commands() click to toggle source
# File lib/flo/cli.rb, line 33
def generate_commands
  flo_runner = runner_class.new
  flo_runner.load_default_config_files

  flo_runner.commands.each do |cmd_name, cmd|

    main_command.define_command do
      options = cmd[:command].required_parameters

      unless cmd[:command].optional_parameters.empty?
        options << '[options]'

        cmd[:command].optional_parameters.each do |param|
          optional(nil, param, param.to_s.tr('_', ' '))
        end
      end

      name(cmd_name)
      usage("#{cmd_name} #{options.join(' ')}")
      summary(cmd[:summary])
      description(cmd[:description])

      run do |opts, arguments, command|
        flo_runner.execute(command.name, arguments.to_a.push(opts))
      end
    end
  end

  main_command
end
main_command() click to toggle source
# File lib/flo/cli.rb, line 64
def main_command
  @main_command ||= Cri::Command.new_basic_root.modify do
    name        'flo'
    usage       'flo [command]'
    summary     'Local workflow automation'
    description 'Use `flo help [command]` for usage on individual commands'

    default_subcommand 'help'
  end
end