Class: PoolParty::Optioner

Inherits:
Object show all
Includes:
Dslify
Defined in:
lib/poolparty/helpers/optioner.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = [], opts = {}, &block) ⇒ Optioner

Returns a new instance of Optioner.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/poolparty/helpers/optioner.rb', line 17

def initialize(args=[], opts={}, &block)
  boolean_args << opts[:boolean_args] if opts.has_key?(:boolean_args)
  
  @arguments = parse_args(args)
  @extra_help = opts.has_key?(:extra_help) ? opts[:extra_help] : ""
  @abstract = opts.has_key?(:abstract) ? opts[:abstract] : false
  @load_pools = opts.has_key?(:load_pools) ? opts[:load_pools] : !@abstract
  @parse_options = opts.has_key?(:parse_options) ? opts[:parse_options] : true
  @command = opts.has_key?(:command) ? opts[:command] : false
  
  parse_options(&block) if @parse_options
  self
end

Instance Method Details

#boolean_argsObject



42
43
44
# File 'lib/poolparty/helpers/optioner.rb', line 42

def boolean_args
  @boolean_args ||= ['-V', '-h', '-t', '-v', '--debug']
end

#cloudnamesObject



33
34
35
# File 'lib/poolparty/helpers/optioner.rb', line 33

def cloudnames
  @opts.on('-n cloudname', '--name name', 'Start cloud by this name')    { |c| self.cloudname c }
end

#daemonizeableObject



30
31
32
# File 'lib/poolparty/helpers/optioner.rb', line 30

def daemonizeable
  @opts.on('-D', '--daemonize', 'Daemonize starting the cloud')    { self.daemon true }
end

#flagged_argsObject



39
40
41
# File 'lib/poolparty/helpers/optioner.rb', line 39

def flagged_args
  @flagged_args ||= []
end

#output_optionsObject



136
137
138
# File 'lib/poolparty/helpers/optioner.rb', line 136

def output_options
  puts ""
end

#output_versionObject



140
141
142
# File 'lib/poolparty/helpers/optioner.rb', line 140

def output_version
  puts ::PoolParty::Version
end

#parentObject

def parent

self

end



73
74
75
# File 'lib/poolparty/helpers/optioner.rb', line 73

def parent
  self
end

#parse_args(args = []) ⇒ Object

Break ARGV into 2 arrays, one for flagged options one for unflagged For example the “command -v -i 1 five six -x” becomes [‘-v’, ‘-i’, 1, ‘-x’] and [‘five’, ‘six’] Boolean options, such as -v, must be specified in the optioner definition



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/poolparty/helpers/optioner.rb', line 50

def parse_args(args=[])
  i=0
  while i < args.length
    if boolean_args.include?(args[i])
      flagged_args << args[i]
    else
      if args[i][0].chr == "-"
        flagged_args << args[i]
        flagged_args << args[i+1] if (args[i+1] and !args[i+1].nil?)
        i+=1
      else
        unflagged_args << args[i]
      end
    end
    i+=1
  end
  args
end

#parse_options(&blk) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/poolparty/helpers/optioner.rb', line 77

def parse_options(&blk)
  self.spec = nil
  self.num = nil
  
  progname = $0.include?("-") ? "#{::File.basename($0[/(\w+)-/, 1])} #{::File.basename($0[/-(.*)/, 1])}" : ::File.basename($0)
  @opts = OptionParser.new
  @opts.extend(Dslify)
  @opts.banner = "Usage: #{progname} #{@abstract ? "[command] " : ""}[options]"

  @opts.separator ""
  
  unless @abstract
    @opts.separator "Options:"
    
    @opts.on('-v', '--verbose', 'Be verbose') { self.verbose true }  
    @opts.on('-d', "--debug", "Debug setting") { self.debugging true }
    @opts.on('-s [file]', '--spec-file [file]', 'Set the spec file') { |file| self.spec file.chomp }
    @opts.on('-t', '--test', 'Testing mode') { self.testing true }
    
    blk.call(@opts, self) if blk
  end
  
  @opts.on('-V', '--version', 'Display the version')    { output_version ; exit 0 }
  @opts.on_tail("-h", "--help", "Show this message") do
    puts @opts
    puts @extra_help
    exit
  end
  
  @opts.parse(@arguments.dup)
  
  process_options
  output_options if verbose
  
  if @load_pools
    self.loaded_pool load_pool( spec? ? spec : Binary.get_existing_spec_location)
    self.loaded_clouds extract_cloud_from_options(self)
    self.loaded_pools extract_pool_from_options(self)

    reject_junk_options!
    raise CloudNotFoundException.new("Please specify your cloud with -s, move it to ./clouds.rb or in your POOL_SPEC environment variable") unless loaded_clouds && !loaded_clouds.empty?
    loaded_pools.each do |pl|
      pl.options(self.options)
    end
    loaded_clouds.each do |cl|
      cl.options(self.options)
    end
  end
end

#process_optionsObject



133
134
# File 'lib/poolparty/helpers/optioner.rb', line 133

def process_options
end

#reject_junk_options!Object



127
128
129
130
131
# File 'lib/poolparty/helpers/optioner.rb', line 127

def reject_junk_options!
  %w(loaded_pool cloudname extract_pool_from_options).each do |opt|
    @dsl_options.delete(opt.to_sym)
  end
end

#unflagged_argsObject



36
37
38
# File 'lib/poolparty/helpers/optioner.rb', line 36

def unflagged_args
  @unflagged_args ||= []
end