diff options
| author | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-04 13:33:03 -0700 |
|---|---|---|
| committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-04 13:33:03 -0700 |
| commit | 9ce031d300738c778254358792ee295c08252cff (patch) | |
| tree | c0489dfd1b5d0a2cdf516d51f34a87e18c150e94 /lib/puppet/application | |
| parent | 505a48c0d316aad7ff26ae2c0ade294707ca081e (diff) | |
| parent | 0c74495529bd697cdc42986882fc3efb4cdc9903 (diff) | |
| download | puppet-9ce031d300738c778254358792ee295c08252cff.tar.gz puppet-9ce031d300738c778254358792ee295c08252cff.tar.xz puppet-9ce031d300738c778254358792ee295c08252cff.zip | |
Merge branch 'feature/master/6749-actions-need-to-support-options'
Diffstat (limited to 'lib/puppet/application')
| -rw-r--r-- | lib/puppet/application/configurer.rb | 4 | ||||
| -rw-r--r-- | lib/puppet/application/indirection_base.rb | 15 | ||||
| -rw-r--r-- | lib/puppet/application/string.rb | 2 | ||||
| -rw-r--r-- | lib/puppet/application/string_base.rb | 109 |
4 files changed, 73 insertions, 57 deletions
diff --git a/lib/puppet/application/configurer.rb b/lib/puppet/application/configurer.rb index b440098ee..be018338f 100644 --- a/lib/puppet/application/configurer.rb +++ b/lib/puppet/application/configurer.rb @@ -5,8 +5,8 @@ class Puppet::Application::Configurer < Puppet::Application should_parse_config run_mode :agent - option("--debug","-d") - option("--verbose","-v") + option("--debug", "-d") + option("--verbose", "-v") def setup if options[:debug] or options[:verbose] diff --git a/lib/puppet/application/indirection_base.rb b/lib/puppet/application/indirection_base.rb index da61f408d..cfa1ea529 100644 --- a/lib/puppet/application/indirection_base.rb +++ b/lib/puppet/application/indirection_base.rb @@ -1,19 +1,4 @@ require 'puppet/application/string_base' class Puppet::Application::IndirectionBase < Puppet::Application::StringBase - option("--terminus TERMINUS") do |arg| - @terminus = arg - end - - attr_accessor :terminus, :indirection - - def setup - super - - if string.respond_to?(:indirection) - raise "Could not find data type #{type} for application #{self.class.name}" unless string.indirection - - string.set_terminus(terminus) if terminus - end - end end diff --git a/lib/puppet/application/string.rb b/lib/puppet/application/string.rb index aa369e669..0a6a798ce 100644 --- a/lib/puppet/application/string.rb +++ b/lib/puppet/application/string.rb @@ -83,7 +83,7 @@ class Puppet::Application::String < Puppet::Application def actions(indirection) return [] unless string = Puppet::String[indirection, '0.0.1'] string.load_actions - return string.actions.sort { |a,b| a.to_s <=> b.to_s } + return string.actions.sort { |a, b| a.to_s <=> b.to_s } end def load_applications diff --git a/lib/puppet/application/string_base.rb b/lib/puppet/application/string_base.rb index bc627adde..06e5789be 100644 --- a/lib/puppet/application/string_base.rb +++ b/lib/puppet/application/string_base.rb @@ -5,14 +5,6 @@ class Puppet::Application::StringBase < Puppet::Application should_parse_config run_mode :agent - def preinit - super - trap(:INT) do - $stderr.puts "Cancelling String" - exit(0) - end - end - option("--debug", "-d") do |arg| Puppet::Util::Log.level = :debug end @@ -32,7 +24,7 @@ class Puppet::Application::StringBase < Puppet::Application end - attr_accessor :string, :type, :verb, :arguments, :format + attr_accessor :string, :action, :type, :arguments, :format attr_writer :exit_code # This allows you to set the exit code if you don't want to just exit @@ -41,14 +33,6 @@ class Puppet::Application::StringBase < Puppet::Application @exit_code || 0 end - def main - # Call the method associated with the provided action (e.g., 'find'). - if result = string.send(verb, *arguments) - puts render(result) - end - exit(exit_code) - end - # Override this if you need custom rendering. def render(result) render_method = Puppet::Network::FormatHandler.format(format).render_method @@ -60,38 +44,85 @@ class Puppet::Application::StringBase < Puppet::Application end end - def setup - Puppet::Util::Log.newdestination :console + def preinit + super + trap(:INT) do + $stderr.puts "Cancelling String" + exit(0) + end - @verb = command_line.args.shift - @arguments = command_line.args - @arguments ||= [] + # We need to parse enough of the command line out early, to identify what + # the action is, so that we can obtain the full set of options to parse. - @arguments = Array(@arguments) + # TODO: These should be configurable versions, through a global + # '--version' option, but we don't implement that yet... --daniel 2011-03-29 + @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym + @string = Puppet::String[@type, :current] + @format = @string.default_format + + # Now, walk the command line and identify the action. We skip over + # arguments based on introspecting the action and all, and find the first + # non-option word to use as the action. + action = nil + index = -1 + until @action or (index += 1) >= command_line.args.length do + item = command_line.args[index] + if item =~ /^-/ then + option = @string.options.find { |a| item =~ /^-+#{a}\b/ } + if option then + option = @string.get_option(option) + # If we have an inline argument, just carry on. We don't need to + # care about optional vs mandatory in that case because we do a real + # parse later, and that will totally take care of raising the error + # when we get there. --daniel 2011-04-04 + if option.takes_argument? and !item.index('=') then + index += 1 unless + (option.optional_argument? and command_line.args[index + 1] =~ /^-/) + end + else + raise ArgumentError, "Unknown option #{item.sub(/=.*$/, '').inspect}" + end + else + action = @string.get_action(item.to_sym) + if action.nil? then + raise ArgumentError, "#{@string} does not have an #{item.inspect} action!" + end + @action = action + end + end - @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym + @action or raise ArgumentError, "No action given on the command line!" - # TODO: These should be configurable versions. - unless Puppet::String.string?(@type, :current) - raise "Could not find any version of string '#{@type}'" + # Finally, we can interact with the default option code to build behaviour + # around the full set of options we now know we support. + @action.options.each do |option| + option = @action.get_option(option) # make it the object. + self.class.option(*option.optparse) # ...and make the CLI parse it. end - @string = Puppet::String[@type, :current] - @format ||= @string.default_format + end - # We copy all of the app options to the string. - # This allows each action to read in the options. - @string.options = options + def setup + Puppet::Util::Log.newdestination :console - validate + # We copy all of the app options to the end of the call; This allows each + # action to read in the options. This replaces the older model where we + # would invoke the action with options set as global state in the + # interface object. --daniel 2011-03-28 + # + # Note: because of our definition of where the action is set, we end up + # with it *always* being the first word of the remaining set of command + # line arguments. So, strip that off when we construct the arguments to + # pass down to the string action. --daniel 2011-04-04 + @arguments = command_line.args[1, -1] || [] + @arguments << options end - def validate - unless verb - raise "You must specify #{string.actions.join(", ")} as a verb; 'save' probably does not work right now" - end - unless string.action?(verb) - raise "Command '#{verb}' not found for #{type}" + def main + # Call the method associated with the provided action (e.g., 'find'). + if result = @string.send(@action.name, *arguments) + puts render(result) end + exit(exit_code) end end |
