Class: Stamina::Command::StaminaCommand

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

Overview

Helper to create stamina commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, usage, description) ⇒ StaminaCommand

Creates a command with a name, usage and description



19
20
21
22
23
# File 'lib/stamina/command/stamina_command.rb', line 19

def initialize(name, usage, description)
  @name = name
  @usage = usage
  @description = description
end

Instance Attribute Details

#descriptionObject (readonly)

Command description



13
14
15
# File 'lib/stamina/command/stamina_command.rb', line 13

def description
  @description
end

#nameObject (readonly)

Command name



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

def name
  @name
end

#usageObject (readonly)

Command usage



16
17
18
# File 'lib/stamina/command/stamina_command.rb', line 16

def usage
  @usage
end

Instance Method Details

#assert_readable_file(file) ⇒ Object

Checks that a given file is readable or raises an ArgumentError

Raises:

  • (ArgumentError)


52
53
54
55
# File 'lib/stamina/command/stamina_command.rb', line 52

def assert_readable_file(file)
  raise ArgumentError, "File #{file} does not exists" unless File.exists?(file)
  raise ArgumentError, "File #{file} cannot be read" unless File.readable?(file)
end

#assert_writable_file(file) ⇒ Object

Checks that a given file is writable or raises an ArgumentError

Raises:

  • (ArgumentError)


58
59
60
61
# File 'lib/stamina/command/stamina_command.rb', line 58

def assert_writable_file(file)
  raise ArgumentError, "File #{file} cannot be written" \
    unless not(File.exists?(file)) or File.writable?(file)
end

#options(&block) ⇒ Object

Creates options



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

def options(&block)
  OptionParser.new do |opt|
    opt.program_name = name
    opt.version = Stamina::VERSION
    opt.release = nil
    opt.summary_indent = ' ' * 4
    banner = <<-EOF
      # usage: #{opt.program_name} #{usage}
      # #{description}
    EOF
    opt.banner = banner.gsub(/[ \t]+# /, "")
    block.call(opt) if block
    opt.on_tail("-h", "--help", "Show this message") do
      puts opt
      exit
    end
  end
end

#parse(argv, *variables) ⇒ Object

Parses arguments and install last argument as instance variables



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/stamina/command/stamina_command.rb', line 64

def parse(argv, *variables)
  rest = options.parse(argv)
  show_usage(true) unless rest.size==variables.size
  variables.each_with_index do |var,i|
    self.send("#{var}=".to_sym, rest[i])
  end
rescue ArgumentError => ex
  puts ex.message
  puts
  show_usage(true)
end

#show_usage(and_exit = true) ⇒ Object

Prints usage (and optionnaly exits)



46
47
48
49
# File 'lib/stamina/command/stamina_command.rb', line 46

def show_usage(and_exit=true)
  puts options
  Kernel.exit if and_exit
end