Class: Dracula::Command

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

Defined Under Namespace

Classes: Desc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, method_name, desc, long_desc, options) ⇒ Command

Returns a new instance of Command.



13
14
15
16
17
18
19
# File 'lib/dracula/command.rb', line 13

def initialize(klass, method_name, desc, long_desc, options)
  @klass = klass
  @method_name = method_name
  @desc = desc
  @long_desc = long_desc
  @options = options || []
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



7
8
9
# File 'lib/dracula/command.rb', line 7

def desc
  @desc
end

#long_descObject (readonly)

Returns the value of attribute long_desc.



9
10
11
# File 'lib/dracula/command.rb', line 9

def long_desc
  @long_desc
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



6
7
8
# File 'lib/dracula/command.rb', line 6

def method_name
  @method_name
end

#optionsObject (readonly) Also known as: flags

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#helpObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dracula/command.rb', line 25

def help
  msg = [
    "Usage: #{Dracula.program_name} #{@klass.namespace.name ? "#{@klass.namespace.name}:" : "" }#{desc.name}",
    "",
    "#{desc.description}",
    ""
  ]

  if options.size > 0
    msg << "Flags:"

    options.each { |option| msg << "  #{option.banner}" }

    msg << ""
  end

  unless long_desc.nil?
    msg << long_desc
  end

  puts msg.join("\n")
end

#nameObject



21
22
23
# File 'lib/dracula/command.rb', line 21

def name
  desc.name
end

#run(params) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dracula/command.rb', line 48

def run(params)
  args  = params.take_while { |p| p[0] != "-" }
  flags = parse_flags(params.drop_while { |p| p[0] != "-" })

  missing_flags = missing_required_flags(flags)

  if missing_flags.empty?
    @klass.new(flags).public_send(method_name, *args)
  else
    puts "Required Parameter: --#{missing_flags.first.name}"
    puts ""
    help
    exit(1)
  end
end