diff --git a/lib/puppet/face/help/action.erb b/lib/puppet/face/help/action.erb index c84e46cf9..ac048c801 100644 --- a/lib/puppet/face/help/action.erb +++ b/lib/puppet/face/help/action.erb @@ -1,53 +1,86 @@ <% if action.synopsis -%> USAGE: <%= action.synopsis %> <% end -%> <%= action.short_description || action.summary || face.summary || "undocumented subcommand" %> <% if action.returns -%> RETURNS: <%= action.returns.strip %> <% end -%> OPTIONS: <%# Remove these options once we can introspect them normally. -%> --mode MODE - The run mode to use (user, agent, or master). --render-as FORMAT - The rendering format to use. --verbose - Whether to log verbosely. --debug - Whether to log debug information. -<% unless action.options.empty? - optionroom = 30 - summaryroom = 80 - 5 - optionroom +<% optionroom = 30 + summaryroom = 80 - 5 - optionroom + + disp_glob_opts = action.display_global_options.uniq + unless disp_glob_opts.empty? + disp_glob_opts.sort.each do |name| + option = name + desc = Puppet.settings.setting(option).desc + type = Puppet.settings.setting(option).default + type ||= Puppet.settings.setting(option).type.to_s.upcase -%> + <%= "--#{option} #{type}".ljust(optionroom) + ' - ' -%> +<% if !(desc) -%> +undocumented option +<% elsif desc.length <= summaryroom -%> +<%= desc %> +<% + else + words = desc.split + wrapped = [''] + i = 0 + words.each do |word| + if wrapped[i].length + word.length <= summaryroom + wrapped[i] << word + ' ' + else + i += 1 + wrapped[i] = word + ' ' + end + end -%> +<%= wrapped.shift.strip %> +<% wrapped.each do |line| -%> +<%= (' ' * (optionroom + 5) ) + line.strip %> +<% end + end + end + end + unless action.options.empty? action.options.sort.each do |name| option = action.get_option name -%> <%= " " + option.optparse.join(" | ")[0,(optionroom - 1)].ljust(optionroom) + ' - ' -%> <% if !(option.summary) -%> undocumented option <% elsif option.summary.length <= summaryroom -%> <%= option.summary %> <% else words = option.summary.split wrapped = [''] i = 0 words.each do |word| if wrapped[i].length + word.length <= summaryroom wrapped[i] << word + ' ' else i += 1 wrapped[i] = word + ' ' end end -%> <%= wrapped.shift.strip %> <% wrapped.each do |line| -%> <%= (' ' * (optionroom + 5) ) + line.strip %> <% end end end -%> <% end -%> <% if face.respond_to? :indirection -%> TERMINI: <%= face.class.terminus_classes(face.indirection.name).join(", ") %> <% end -%> See 'puppet man <%= face.name %>' or 'man puppet-<%= face.name %>' for full help. diff --git a/lib/puppet/face/help/face.erb b/lib/puppet/face/help/face.erb index 255d3d55b..968d443a2 100644 --- a/lib/puppet/face/help/face.erb +++ b/lib/puppet/face/help/face.erb @@ -1,79 +1,111 @@ <% if face.synopsis -%> USAGE: <%= face.synopsis %> <% end -%> <%= (face.short_description || face.summary || "undocumented subcommand").strip %> OPTIONS: <%# Remove these options once we can introspect them normally. -%> --mode MODE - The run mode to use (user, agent, or master). --render-as FORMAT - The rendering format to use. --verbose - Whether to log verbosely. --debug - Whether to log debug information. -<% unless face.options.empty? - optionroom = 30 - summaryroom = 80 - 5 - optionroom +<% optionroom = 30 + summaryroom = 80 - 5 - optionroom + + disp_glob_opts = face.display_global_options.uniq + unless disp_glob_opts.empty? + disp_glob_opts.sort.each do |name| + option = name + desc = Puppet.settings.setting(option).desc + type = Puppet.settings.setting(option).default + type ||= Puppet.settings.setting(option).type.to_s.upcase -%> + <%= "--#{option} #{type}".ljust(optionroom) + ' - ' -%> +<% if !(desc) -%> +undocumented option +<% elsif desc.length <= summaryroom -%> +<%= desc %> +<% else + words = desc.split + wrapped = [''] + i = 0 + words.each do |word| + if wrapped[i].length + word.length <= summaryroom + wrapped[i] << word + ' ' + else + i += 1 + wrapped[i] = word + ' ' + end + end -%> +<%= wrapped.shift.strip %> +<% wrapped.each do |line| -%> +<%= (' ' * (optionroom + 5) ) + line.strip %> +<% end + end + end + end + unless face.options.empty? face.options.sort.each do |name| option = face.get_option name -%> <%= " " + option.optparse.join(" | ")[0,(optionroom - 1)].ljust(optionroom) + ' - ' -%> <% if !(option.summary) -%> undocumented option <% elsif option.summary.length <= summaryroom -%> <%= option.summary %> <% else words = option.summary.split wrapped = [''] i = 0 words.each do |word| if wrapped[i].length + word.length <= summaryroom wrapped[i] << word + ' ' else i += 1 wrapped[i] = word + ' ' end end -%> <%= wrapped.shift.strip %> <% wrapped.each do |line| -%> <%= (' ' * (optionroom + 5) ) + line.strip %> <% end end end -%> <% end -%> ACTIONS: <% padding = face.actions.map{|x| x.to_s.length}.max + 2 summaryroom = 80 - (padding + 4) face.actions.each do |actionname| action = face.get_action(actionname) -%> <%= action.name.to_s.ljust(padding) + ' ' -%> <% if !(action.summary) -%> undocumented action <% elsif action.summary.length <= summaryroom -%> <%= action.summary %> <% else words = action.summary.split wrapped = [''] i = 0 words.each do |word| if wrapped[i].length + word.length <= summaryroom wrapped[i] << word + ' ' else i += 1 wrapped[i] = word + ' ' end end -%> <%= wrapped.shift.strip %> <% wrapped.each do |line| -%> <%= (' ' * (padding + 4) ) + line.strip %> <% end end end -%> <% if face.respond_to? :indirection -%> TERMINI: <%= face.class.terminus_classes(face.indirection.name).join(", ") %> <% end -%> See 'puppet man <%= face.name %>' or 'man puppet-<%= face.name %>' for full help. diff --git a/lib/puppet/face/help/man.erb b/lib/puppet/face/help/man.erb index 5a3aa2cbe..254c6f03a 100644 --- a/lib/puppet/face/help/man.erb +++ b/lib/puppet/face/help/man.erb @@ -1,136 +1,154 @@ puppet-<%= face.name %>(8) -- <%= face.summary || "Undocumented subcommand." %> <%= '=' * (_erbout.length - 1) %> <% if face.synopsis -%> SYNOPSIS -------- <%= face.synopsis %> <% end if face.description -%> DESCRIPTION ----------- <%= face.description.strip %> <% end -%> OPTIONS ------- Note that any configuration parameter that's valid in the configuration file is also a valid long argument, although it may or may not be relevant to the present action. For example, `server` is a valid configuration parameter, so you can specify `--server ` as an argument. See the configuration file documentation at for the full list of acceptable parameters. A commented list of all configuration options can also be generated by running puppet with `--genconfig`. * --mode MODE: The run mode to use for the current action. Valid modes are `user`, `agent`, and `master`. * --render-as FORMAT: The format in which to render output. The most common formats are `json`, `s` (string), `yaml`, and `console`, but other options such as `dot` are sometimes available. * --verbose: Whether to log verbosely. * --debug: Whether to log debug information. +<% unless face.display_global_options.empty? + face.display_global_options.uniq.sort.each do |name| + option = name + desc = Puppet.settings.setting(option).desc + type = Puppet.settings.setting(option).default + type ||= Puppet.settings.setting(option).type.to_s.upcase -%> +<%= "* --#{option} #{type}" %>: +<%= desc.gsub(/^/, ' ') || ' undocumented setting' %> +<% end + end -%> <% unless face.options.empty? face.options.sort.each do |name| option = face.get_option name -%> <%= "* " + option.optparse.join(" | " ) %>: <%= option.description.gsub(/^/, ' ') || ' ' + option.summary %> <% end end -%> ACTIONS ------- <% face.actions.each do |actionname| action = face.get_action(actionname) -%> * `<%= action.name.to_s %>` - <%= action.summary %>: <% if action.synopsis -%> `SYNOPSIS` <%= action.synopsis %> <% end -%> `DESCRIPTION` <% if action.description -%> <%= action.description.gsub(/^/, ' ') %> <% else -%> <%= action.summary || "Undocumented action." %> <% end -%> <% unique_options = action.options - face.options - unless unique_options.empty? -%> + unique_display_global_options = action.display_global_options - face.display_global_options + unless unique_options.empty? and unique_display_global_options.empty? -%> `OPTIONS` - +<% unique_display_global_options.uniq.sort.each do |name| + option = name + desc = Puppet.settings.setting(option).desc + type = Puppet.settings.setting(option).default + type ||= Puppet.settings.setting(option).type.to_s.upcase -%> + <%= "<--#{option} #{type}>" %> - +<%= desc.gsub(/^/, ' ') %> +<% end -%> <% unique_options.sort.each do |name| option = action.get_option name text = (option.description || option.summary).chomp + "\n" -%> <%= '<' + option.optparse.join("> | <") + '>' %> - <%= text.gsub(/^/, ' ') %> <% end -%> <% end -%> <% if action.returns -%> `RETURNS` <%= action.returns.gsub(/^/, ' ') %> <% end if action.notes -%> `NOTES` <%= action.notes.gsub(/^/, ' ') %> <% end end if face.examples or face.actions.any? {|actionname| face.get_action(actionname).examples} -%> EXAMPLES -------- <% end if face.examples -%> <%= face.examples %> <% end face.actions.each do |actionname| action = face.get_action(actionname) if action.examples -%> `<%= action.name.to_s %>` <%= action.examples.strip %> <% end end -%> <% if face.notes or face.respond_to? :indirection -%> NOTES ----- <% if face.notes -%> <%= face.notes.strip %> <% end # notes if face.respond_to? :indirection -%> This subcommand is an indirector face, which exposes `find`, `search`, `save`, and `destroy` actions for an indirected subsystem of Puppet. Valid termini for this face include: * `<%= face.class.terminus_classes(face.indirection.name).join("`\n* `") %>` <% end # indirection end # notes or indirection unless face.authors.empty? -%> AUTHOR ------ <%= face.authors.join("\n").gsub(/^/, ' * ') %> <% end -%> COPYRIGHT AND LICENSE --------------------- <%= face.copyright %> <%= face.license %> diff --git a/lib/puppet/face/module.rb b/lib/puppet/face/module.rb index 87c14a8d1..523982152 100644 --- a/lib/puppet/face/module.rb +++ b/lib/puppet/face/module.rb @@ -1,17 +1,19 @@ require 'puppet/face' require 'puppet/module_tool' require 'puppet/util/colors' Puppet::Face.define(:module, '1.0.0') do extend Puppet::Util::Colors copyright "Puppet Labs", 2012 license "Apache 2 license; see COPYING" summary "Creates, installs and searches for modules on the Puppet Forge." description <<-EOT This subcommand can find, install, and manage modules from the Puppet Forge, a repository of user-contributed Puppet code. It can also generate empty modules, and prepare locally developed modules for release on the Forge. EOT + + display_global_options "environment", "modulepath" end diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb index 5f23f55db..6ebbcb63f 100644 --- a/lib/puppet/interface/action.rb +++ b/lib/puppet/interface/action.rb @@ -1,322 +1,338 @@ require 'puppet/interface' require 'puppet/interface/documentation' require 'prettyprint' class Puppet::Interface::Action extend Puppet::Interface::DocGen include Puppet::Interface::FullDocs def initialize(face, name, attrs = {}) raise "#{name.inspect} is an invalid action name" unless name.to_s =~ /^[a-z]\w*$/ @face = face @name = name.to_sym # The few bits of documentation we actually demand. The default license # is a favour to our end users; if you happen to get that in a core face # report it as a bug, please. --daniel 2011-04-26 @authors = [] @license = 'All Rights Reserved' attrs.each do |k, v| send("#{k}=", v) end # @options collects the added options in the order they're declared. # @options_hash collects the options keyed by alias for quick lookups. @options = [] + @display_global_options = [] @options_hash = {} @when_rendering = {} end # This is not nice, but it is the easiest way to make us behave like the # Ruby Method object rather than UnboundMethod. Duplication is vaguely # annoying, but at least we are a shallow clone. --daniel 2011-04-12 def __dup_and_rebind_to(to) bound_version = self.dup bound_version.instance_variable_set(:@face, to) return bound_version end def to_s() "#{@face}##{@name}" end attr_reader :name attr_reader :face attr_accessor :default def default? !!@default end ######################################################################## # Documentation... attr_doc :returns attr_doc :arguments def synopsis build_synopsis(@face.name, default? ? nil : name, arguments) end ######################################################################## # Support for rendering formats and all. def when_rendering(type) unless type.is_a? Symbol raise ArgumentError, "The rendering format must be a symbol, not #{type.class.name}" end # Do we have a rendering hook for this name? return @when_rendering[type].bind(@face) if @when_rendering.has_key? type # How about by another name? alt = type.to_s.sub(/^to_/, '').to_sym return @when_rendering[alt].bind(@face) if @when_rendering.has_key? alt # Guess not, nothing to run. return nil end def set_rendering_method_for(type, proc) unless proc.is_a? Proc msg = "The second argument to set_rendering_method_for must be a Proc" msg += ", not #{proc.class.name}" unless proc.nil? raise ArgumentError, msg end if proc.arity != 1 and proc.arity != (@positional_arg_count + 1) msg = "the when_rendering method for the #{@face.name} face #{name} action " msg += "takes either just one argument, the result of when_invoked, " msg += "or the result plus the #{@positional_arg_count} arguments passed " msg += "to the when_invoked block, not " if proc.arity < 0 then msg += "a variable number" else msg += proc.arity.to_s end raise ArgumentError, msg end unless type.is_a? Symbol raise ArgumentError, "The rendering format must be a symbol, not #{type.class.name}" end if @when_rendering.has_key? type then raise ArgumentError, "You can't define a rendering method for #{type} twice" end # Now, the ugly bit. We add the method to our interface object, and # retrieve it, to rotate through the dance of getting a suitable method # object out of the whole process. --daniel 2011-04-18 @when_rendering[type] = @face.__send__( :__add_method, __render_method_name_for(type), proc) end def __render_method_name_for(type) :"#{name}_when_rendering_#{type}" end private :__render_method_name_for attr_accessor :render_as def render_as=(value) @render_as = value.to_sym end ######################################################################## # Initially, this was defined to allow the @action.invoke pattern, which is # a very natural way to invoke behaviour given our introspection # capabilities. Heck, our initial plan was to have the faces delegate to # the action object for invocation and all. # # It turns out that we have a binding problem to solve: @face was bound to # the parent class, not the subclass instance, and we don't pass the # appropriate context or change the binding enough to make this work. # # We could hack around it, by either mandating that you pass the context in # to invoke, or try to get the binding right, but that has probably got # subtleties that we don't instantly think of – especially around threads. # # So, we are pulling this method for now, and will return it to life when we # have the time to resolve the problem. For now, you should replace... # # @action = @face.get_action(name) # @action.invoke(arg1, arg2, arg3) # # ...with... # # @action = @face.get_action(name) # @face.send(@action.name, arg1, arg2, arg3) # # I understand that is somewhat cumbersome, but it functions as desired. # --daniel 2011-03-31 # # PS: This code is left present, but commented, to support this chunk of # documentation, for the benefit of the reader. # # def invoke(*args, &block) # @face.send(name, *args, &block) # end # We need to build an instance method as a wrapper, using normal code, to be # able to expose argument defaulting between the caller and definer in the # Ruby API. An extra method is, sadly, required for Ruby 1.8 to work since # it doesn't expose bind on a block. # # Hopefully we can improve this when we finally shuffle off the last of Ruby # 1.8 support, but that looks to be a few "enterprise" release eras away, so # we are pretty stuck with this for now. # # Patches to make this work more nicely with Ruby 1.9 using runtime version # checking and all are welcome, provided that they don't change anything # outside this little ol' bit of code and all. # # Incidentally, we though about vendoring evil-ruby and actually adjusting # the internal C structure implementation details under the hood to make # this stuff work, because it would have been cleaner. Which gives you an # idea how motivated we were to make this cleaner. Sorry. # --daniel 2011-03-31 attr_reader :positional_arg_count attr_accessor :when_invoked def when_invoked=(block) internal_name = "#{@name} implementation, required on Ruby 1.8".to_sym arity = @positional_arg_count = block.arity if arity == 0 then # This will never fire on 1.8.7, which treats no arguments as "*args", # but will on 1.9.2, which treats it as "no arguments". Which bites, # because this just begs for us to wind up in the horrible situation # where a 1.8 vs 1.9 error bites our end users. --daniel 2011-04-19 raise ArgumentError, "when_invoked requires at least one argument (options) for action #{@name}" elsif arity > 0 then range = Range.new(1, arity - 1) decl = range.map { |x| "arg#{x}" } << "options = {}" optn = "" args = "[" + (range.map { |x| "arg#{x}" } << "options").join(", ") + "]" else range = Range.new(1, arity.abs - 1) decl = range.map { |x| "arg#{x}" } << "*rest" optn = "rest << {} unless rest.last.is_a?(Hash)" if arity == -1 then args = "rest" else args = "[" + range.map { |x| "arg#{x}" }.join(", ") + "] + rest" end end file = __FILE__ + "+eval[wrapper]" line = __LINE__ + 2 # <== points to the same line as 'def' in the wrapper. wrapper = <