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



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

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

.environment(name, value) ⇒ Object



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

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

.example(text) ⇒ Object



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

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

.execute(argv = []) ⇒ Object



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

def execute(argv=[])
  args = Docopt.docopt docopt, version: meta.version, argv: argv
  instance = new args
  target = find_target_command instance, args
  exitcode = instance.send target
  exitcode.is_a?(Numeric) ? exitcode : 0

rescue Docopt::Exit => e
  puts e.message
  1
end

.help(text) ⇒ Object



33
34
35
# File 'lib/mister_bin/command.rb', line 33

def help(text)
  meta.help = text
end

.metaObject



66
67
68
# File 'lib/mister_bin/command.rb', line 66

def meta
  @meta ||= CommandMeta.new
end

.option(flags, text) ⇒ Object



45
46
47
# File 'lib/mister_bin/command.rb', line 45

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

.param(param, text) ⇒ Object



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

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

.summary(text) ⇒ Object

DSL



29
30
31
# File 'lib/mister_bin/command.rb', line 29

def summary(text)
  meta.summary = text
end

.usage(text) ⇒ Object



41
42
43
# File 'lib/mister_bin/command.rb', line 41

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

.version(text) ⇒ Object



37
38
39
# File 'lib/mister_bin/command.rb', line 37

def version(text)
  meta.version = text
end