Class: Boot::Lib::Core::SubCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/Boot/Lib/Core/SubCommand.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, options, assume_help = true, &block) ⇒ SubCommand

Returns a new instance of SubCommand.



10
11
12
13
14
15
16
# File 'lib/Boot/Lib/Core/SubCommand.rb', line 10

def initialize(name, description, options, assume_help = true, &block)
  @name        = name
  @description = description
  @options     = options
  @block       = block
  @assume_help  = assume_help
end

Instance Attribute Details

#assume_helpObject (readonly)

If true, print help message on empty args



8
9
10
# File 'lib/Boot/Lib/Core/SubCommand.rb', line 8

def assume_help
  @assume_help
end

#descriptionObject (readonly)

Returns the value of attribute description.



4
5
6
# File 'lib/Boot/Lib/Core/SubCommand.rb', line 4

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/Boot/Lib/Core/SubCommand.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/Boot/Lib/Core/SubCommand.rb', line 5

def options
  @options
end

Class Method Details

.is_flag(str) ⇒ Object



39
40
41
# File 'lib/Boot/Lib/Core/SubCommand.rb', line 39

def self.is_flag(str)
  return str.start_with?("--") || (str.start_with?("-") && str.length == 2)
end

Instance Method Details



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/Boot/Lib/Core/SubCommand.rb', line 43

def print_help_message
  puts 'boot ' + @name
  if (@description != '')
    puts "\tDescription:"
    puts "\t" + @description
  end
  if @options.options.length > 0
    puts
    puts "\tArguments:"
    @options.each do |a|
      print "\t" + '%-16.16s' % (a.flags * ', ')
      print "\t"
      print a.desc + "\n"
    end
  end
  puts
end

#run(args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/Boot/Lib/Core/SubCommand.rb', line 18

def run(args)
  begin
    # If no option spesified, assume --help
    # The underscore is a hack, see rescue block
    if (assume_help)
      fail Slop::UnknownOption.new "Unknown option --help", "--help" if (args.length == 0)
    end

    @options.parse(args); # Dryrun to check for argument errors
    @block.call(@options, args)
  rescue Slop::UnknownOption => e
    # Hack to get around the lack of e.getUnknownOption()
    # TODO Fix once avaiable
    if (e.flag == '--help')
      print_help_message
    else
      puts "#{e.message}. Try boot #{@name} --help"
    end
  end
end