Class: BranchIOCLI::CLI

Inherits:
Object
  • Object
show all
Includes:
Format::HighlineFormat, Commander::Methods
Defined in:
lib/branch_io_cli/cli.rb

Instance Method Summary collapse

Methods included from Format::HighlineFormat

#header, #highlight, #italics

Methods included from Format

#option, #render

Instance Method Details

#add_options_for_command(c) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/branch_io_cli/cli.rb', line 73

def add_options_for_command(c)
  configuration_class = configuration_class(c.name)
  return unless configuration_class.respond_to?(:available_options)

  available_options = configuration_class.available_options
  available_options.each do |option|
    args = option.aliases
    declaration = "--"
    declaration += "[no-]" if option.negatable
    declaration += option.name.to_s.gsub(/_/, '-')
    if option.example
      declaration += " "
      declaration += "[" if option.argument_optional
      declaration += option.example
      declaration += "]" if option.argument_optional
    end
    args << declaration
    args << option.type if option.type

    if option.type.nil?
      default_value = option.default_value ? "yes" : "no"
    else
      default_value = option.default_value
    end

    default_string = default_value ? " (default: #{default_value})" : nil
    args << "#{option.description}#{default_string}"

    c.option(*args)
  end
end

#class_for_command(name, type) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/branch_io_cli/cli.rb', line 62

def class_for_command(name, type)
  type_name = type.to_s.capitalize
  type_module = BranchIOCLI.const_get(type_name)
  candidate = type_module.const_get("#{name.to_s.capitalize}#{type_name}")
  return nil unless candidate

  base = type_module.const_get(type_name)
  return nil unless candidate.superclass == base
  candidate
end

#command_class(name) ⇒ Object



58
59
60
# File 'lib/branch_io_cli/cli.rb', line 58

def command_class(name)
  class_for_command name, :command
end

#configuration_class(name) ⇒ Object



54
55
56
# File 'lib/branch_io_cli/cli.rb', line 54

def configuration_class(name)
  class_for_command name, :configuration
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/branch_io_cli/cli.rb', line 10

def run
  program :name, "Branch.io command-line interface"
  program :version, VERSION
  program :description, render(:program_description)

  # Automatically detect all commands from branch_io_cli/command.
  all_commands = Dir[File.expand_path(File.join("..", "command", "*_command.rb"), __FILE__)].map do |path|
    File.basename(path, ".rb").sub(/_command$/, "")
  end

  all_commands.each do |command_name|
    configuration_class = configuration_class command_name
    command_class = command_class command_name
    next unless configuration_class && command_class

    command command_name do |c|
      c.syntax = "branch_io #{c.name} [OPTIONS]\n    br #{c.name} [OPTIONS]"
      c.summary = configuration_class.summary if configuration_class.respond_to?(:summary)

      begin
        c.description = render "#{c.name}_description"
      rescue Errno::ENOENT
      end

      add_options_for_command c

      if configuration_class.respond_to?(:examples) && configuration_class.examples
        configuration_class.examples.each_key do |text|
          example = configuration_class.examples[text]
          c.example text, example
        end
      end

      c.action do |args, options|
        options.default configuration_class.defaults
        return_value = command_class.new(options).run!
        exit(return_value.respond_to?(:to_i) ? return_value.to_i : 0)
      end
    end
  end

  run!
end