Class: Quadtone::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/quadtone/tool.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#profileObject

Returns the value of attribute profile.



7
8
9
# File 'lib/quadtone/tool.rb', line 7

def profile
  @profile
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/quadtone/tool.rb', line 8

def verbose
  @verbose
end

Class Method Details

.process_args(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/quadtone/tool.rb', line 10

def self.process_args(args)
  begin
    name = args.shift or raise ToolUsageError, "No subcommand specified"
    klass_name = 'Quadtone::Tools::' + name.split('-').map { |p| p.capitalize }.join
    begin
      klass = Kernel.const_get(klass_name)
      raise NameError unless klass.respond_to?(:process_args)
    rescue NameError => e
      raise ToolUsageError, "Unknown subcommand specified: #{name.inspect} (#{klass_name})"
    end
    tool = klass.new
    tool.process_environment
    tool.load_profile
    while args.first && args.first[0] == '-'
      option = args.shift
      tool.parse_global_option(option, args) or tool.parse_option(option, args) or raise ToolUsageError, "Unknown option for #{name.inspect} tool: #{option}"
    end
    tool.run(*args)
  rescue ToolUsageError => e
    warn e
    exit 1
  end
end

Instance Method Details

#load_profileObject

subclass can override this to avoid requirement of profile being set

Raises:



55
56
57
# File 'lib/quadtone/tool.rb', line 55

def load_profile
  raise ToolUsageError, "No profile set" unless @profile
end

#parse_global_option(option, args) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/quadtone/tool.rb', line 40

def parse_global_option(option, args)
  case option
  when '--verbose'
    @verbose = true
  when '--profile'
    @profile = Profile.load(args.shift)
  end
end

#parse_option(option, args) ⇒ Object



49
50
51
# File 'lib/quadtone/tool.rb', line 49

def parse_option(option, args)
  # overridden by subclass
end

#process_environmentObject



34
35
36
37
38
# File 'lib/quadtone/tool.rb', line 34

def process_environment
  if (profile_name = ENV['PROFILE'])
    @profile = Profile.load(profile_name)
  end
end