Class: MisterBin::Command

Inherits:
Object
  • Object
show all
Includes:
Colsole
Defined in:
lib/mister_bin/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Command

Returns a new instance of Command.



10
11
12
# File 'lib/mister_bin/command.rb', line 10

def initialize(args = nil)
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/mister_bin/command.rb', line 8

def args
  @args
end

Class Method Details

.command(name, text) ⇒ Object



51
52
53
54
# File 'lib/mister_bin/command.rb', line 51

def command(name, text)
  target_commands << name.to_sym
  meta.commands << [name, text]
end

.docoptObject



72
73
74
# File 'lib/mister_bin/command.rb', line 72

def docopt
  meta.docopt
end

.environment(name, value) ⇒ Object



64
65
66
# File 'lib/mister_bin/command.rb', line 64

def environment(name, value)
  meta.env_vars << [name, value]
end

.example(text) ⇒ Object



60
61
62
# File 'lib/mister_bin/command.rb', line 60

def example(text)
  meta.examples << text
end

.execute(argv = []) ⇒ Object



25
26
27
# File 'lib/mister_bin/command.rb', line 25

def execute(argv = [])
  new.execute argv
end

.find_target_command(instance, args) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/mister_bin/command.rb', line 80

def find_target_command(instance, args)
  target_commands.each do |target|
    method_name = :"#{target}_command"
    return method_name if instance.respond_to?(method_name) && args[target.to_s]
  end
  :run
end

.help(text) ⇒ Object



35
36
37
# File 'lib/mister_bin/command.rb', line 35

def help(text)
  meta.help = text
end

.metaObject



68
69
70
# File 'lib/mister_bin/command.rb', line 68

def meta
  @meta ||= CommandMeta.new
end

.option(flags, text) ⇒ Object



47
48
49
# File 'lib/mister_bin/command.rb', line 47

def option(flags, text)
  meta.options << [flags, text]
end

.param(param, text) ⇒ Object



56
57
58
# File 'lib/mister_bin/command.rb', line 56

def param(param, text)
  meta.params << [param, text]
end

.summary(text) ⇒ Object

DSL



31
32
33
# File 'lib/mister_bin/command.rb', line 31

def summary(text)
  meta.summary = text
end

.target_commandsObject



76
77
78
# File 'lib/mister_bin/command.rb', line 76

def target_commands
  @target_commands ||= []
end

.usage(text) ⇒ Object



43
44
45
# File 'lib/mister_bin/command.rb', line 43

def usage(text)
  meta.usages << text
end

.version(text) ⇒ Object



39
40
41
# File 'lib/mister_bin/command.rb', line 39

def version(text)
  meta.version = text
end

Instance Method Details

#execute(argv = []) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/mister_bin/command.rb', line 14

def execute(argv = [])
  @args = DocoptNG.docopt self.class.docopt, version: self.class.meta.version, argv: argv
  target = self.class.find_target_command self, args
  exitcode = send target
  exitcode.is_a?(Numeric) ? exitcode : 0
rescue DocoptNG::Exit => e
  puts e.message
  e.exit_code
end