Class: PoolParty::Optioner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Optioner.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/poolparty/helpers/optioner.rb', line 22

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 Attribute Details

#loaded_cloudsObject

Returns the value of attribute loaded_clouds.



12
13
14
# File 'lib/poolparty/helpers/optioner.rb', line 12

def loaded_clouds
  @loaded_clouds
end

#loaded_poolObject

Returns the value of attribute loaded_pool.



12
13
14
# File 'lib/poolparty/helpers/optioner.rb', line 12

def loaded_pool
  @loaded_pool
end

#loaded_poolsObject

Returns the value of attribute loaded_pools.



12
13
14
# File 'lib/poolparty/helpers/optioner.rb', line 12

def loaded_pools
  @loaded_pools
end

Instance Method Details

#boolean_argsObject



47
48
49
# File 'lib/poolparty/helpers/optioner.rb', line 47

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

#cloudnamesObject



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

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

#daemonizeableObject



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

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

#flagged_argsObject



44
45
46
# File 'lib/poolparty/helpers/optioner.rb', line 44

def flagged_args
  @flagged_args ||= []
end

#output_optionsObject



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

def output_options
  puts ""
end

#output_versionObject



145
146
147
# File 'lib/poolparty/helpers/optioner.rb', line 145

def output_version
  puts ::PoolParty::Version
end

#parentObject

def parent

self

end



78
79
80
# File 'lib/poolparty/helpers/optioner.rb', line 78

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/poolparty/helpers/optioner.rb', line 55

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



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
126
127
128
129
130
# File 'lib/poolparty/helpers/optioner.rb', line 82

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? && verbose
  
  if @load_pools
    @loaded_pool = load_pool( spec ? spec : Binary.get_existing_spec_location)
    @loaded_clouds = extract_cloud_from_options(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.dsl_options.merge!(self.dsl_options)
    end
    loaded_clouds.each do |cl|
      cl.dsl_options.merge!(self.dsl_options)
    end
  end
end

#process_optionsObject



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

def process_options
end

#reject_junk_options!Object



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

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



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

def unflagged_args
  @unflagged_args ||= []
end