class Optplus::NestedParser
define a nested parser to add sub-actions to an action
Useful if you want to group actions together. For example, if you have a command that does things for services (for example) and other things for servers (for example) then you can define a nested parser for each and then define actions ‘services’ and ‘servers’ that use these parsers:
my_command servers list --all my_command services list --all
Optplus
does not allow option switches to be specific to any one action - its an everything and everywhere approach.
Note that NestedParser
inherits everything from Parser
and could therefore nest another parser within itself, ad infinitum. You could also define methods such as options which would be a waste because they would never be called.
Attributes
@!visibility private
Public Class Methods
@!visibility private
# File lib/optplus/nested.rb, line 76 def initialize(parent=nil) @klass = self.class @_parent = parent || @klass._parent self.before_all if self.respond_to?(:before_all) #self.before_actions if self.respond_to?(:before_actions) end
@!visibility private
Optplus::Parser::run!
# File lib/optplus/nested.rb, line 46 def run!(parent) # set class attribute so that the instance # can determine its parent class @_parent = parent super() end
Public Instance Methods
@!visibility private
# File lib/optplus/nested.rb, line 89 def _args _parent._args end
@!visibility private
# File lib/optplus/nested.rb, line 130 def _get_help(indent=0) helps = [] prefix = " " * indent prog_name = File.basename($0, File.extname($0)) helps << "Usage: #{prog_name} #{self.class._banner}" helps << "" self.class._description.each do |line| helps << line end helps << "" flags = 0 self.class._descriptions.each_pair do |action, description| flag = @klass._help && @klass._help.has_key?(action.to_sym) ? '(-h)' : '' flags += 1 unless flag == '' helps << " #{action} - #{description} #{flag}" end if flags > 0 then helps << "" helps << " (-h indicates actions with additional help)" helps << "" end helps << "" helps << "For full details of options etc:" helps << " #{prog_name} -h" helps.each do |hline| puts prefix + hline end end
@!visibility private
# File lib/optplus/nested.rb, line 84 def _parent @_parent end
return all of the remaining args, or an empty array @see Optplus::Parser#next_argument
# File lib/optplus/nested.rb, line 113 def all_arguments _parent.all_arguments end
get the value of the option @see Optplus::Parser#next_argument
# File lib/optplus/nested.rb, line 119 def get_option(key) _parent.get_option(key) end
return the next argument, if there is one or nil otherwise @see Optplus::Parser#next_argument
# File lib/optplus/nested.rb, line 95 def next_argument _parent.next_argument end
return the next argument or the given default @see Optplus::Parser#next_argument
# File lib/optplus/nested.rb, line 101 def next_argument_or(default) _parent.next_argument_or(default) end
return the next argument or raise exception with the given message
@see Optplus::Parser#next_argument
# File lib/optplus/nested.rb, line 107 def next_argument_or_error(message) _parent.next_argument_or_error(message) end
check if the option has been set
@see Optplus::Parser#next_argument
# File lib/optplus/nested.rb, line 125 def option?(key) _parent.option?(key) end