Class: MCLI::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/mcli/command.rb

Direct Known Subclasses

NullCommand

Defined Under Namespace

Classes: Option

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ Command

Returns a new instance of Command.



4
5
6
# File 'lib/mcli/command.rb', line 4

def initialize(args=[])
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



2
3
4
# File 'lib/mcli/command.rb', line 2

def args
  @args
end

Class Method Details

.call(args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/mcli/command.rb', line 87

def call(args)
  new(args).tap do |command|
    begin
      command.parse unless capture_all?
      command.run
    rescue MCLI::HelpError
      command.help
    end
  end
end

.capture_all!Object



71
72
73
# File 'lib/mcli/command.rb', line 71

def capture_all!
  @capture_all = true
end

.capture_all?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/mcli/command.rb', line 75

def capture_all?
  @capture_all
end

.command_nameObject



79
80
81
# File 'lib/mcli/command.rb', line 79

def command_name
  @command_name
end

.default_command_groupObject



83
84
85
# File 'lib/mcli/command.rb', line 83

def default_command_group
  MCLI::CommandGroup
end

.description(desc = nil) ⇒ Object



49
50
51
52
# File 'lib/mcli/command.rb', line 49

def description(desc=nil)
  @description = desc unless desc.nil?
  @description ||= ''
end

.option(option_name, opts = {}) ⇒ Object



54
55
56
# File 'lib/mcli/command.rb', line 54

def option(option_name, opts={})
  options << Option.new(option_name, opts)
end

.optionsObject



58
59
60
# File 'lib/mcli/command.rb', line 58

def options
  @options ||= []
end

.register_as(command_name, to: default_command_group) ⇒ Object



62
63
64
65
# File 'lib/mcli/command.rb', line 62

def register_as(command_name, to: default_command_group)
  @command_name = command_name
  to.register(command_name, self)
end

.register_as_root(to: default_command_group) ⇒ Object



67
68
69
# File 'lib/mcli/command.rb', line 67

def register_as_root(to: default_command_group)
  to.register_root(self)
end

Instance Method Details

#create_parserObject



38
39
40
41
42
43
44
45
46
# File 'lib/mcli/command.rb', line 38

def create_parser
  OptionParser.new.tap do |parser|
    parser.program_name = self.class.command_name

    parser.on("-h", "--help", "Help") do
      raise MCLI::HelpError.new
    end
  end
end

#optionsObject



30
31
32
# File 'lib/mcli/command.rb', line 30

def options
  @options ||= {}
end

#parseObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mcli/command.rb', line 8

def parse
  @options = {}

  self.class.options.each do |option|
    @options[option.name] = option.default if option.default

    parser.on(*option.to_args) do |o|
      @options[option.name] = o
    end
  end

  parser.parse!(@args)

  self.class.options
    .select { |option| option.required == true }
    .map do |required_option|
      if @options[required_option.name].nil?
        raise OptionParser::MissingArgument
      end
    end
end

#parserObject



34
35
36
# File 'lib/mcli/command.rb', line 34

def parser
  @parser ||= create_parser
end