Sha256: 1cb3c336143e2706b9d35f9035e21a83cfea1b83774887ca9a1c59c39587d20a

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

require 'anvil/task_manager'
require 'tasks/help_task'

module Anvil
  class Cli
    HELP = <<-HELP
Anvil is a tool for making your life easier.

Available tasks:
HELP

    def run(argv)
      load_tasks

      if argv.empty?
        print_help
      else
        build_task(argv).run
      end
    end

    def load_tasks
      Anvil::TaskManager.load_tasks
    end

    def build_task(argv)
      arguments = argv.dup
      task_name = arguments.shift
      klazz     = Task.from_name(task_name)
      klazz.new(*klazz.parse_options!(arguments))
    rescue NameError
      task_not_found(task_name)
      exit(FALSE)
    rescue ArgumentError
      help(task_name)
      exit(FALSE)
    end

    def task_not_found(task_name)
      printf("Task '#{task_name}' not found\n\n")
      print_help
    end

    def help(task_name)
      printf("Wrong number of arguments.\n\n")
      HelpTask.run(task_name)
    end

    def print_help
      printf('%s', HELP)
      tasks = Anvil::TaskManager.tasks_by_name
      tasks.each { |task| print_task_line(task) }
    end

    def print_task_line(task)
      printf("%-20s %s\n", task.task_name, task.description)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
anvil-core-0.0.1.pre.alpha.3 lib/anvil/cli.rb
anvil-core-0.0.1.pre.alpha.2 lib/anvil/cli.rb
anvil-core-0.0.1.alpha.1 lib/anvil/cli.rb