Class: Benry::CLI::ActionInfo
- Inherits:
-
Object
- Object
- Benry::CLI::ActionInfo
- Defined in:
- lib/benry/cli.rb
Instance Attribute Summary collapse
-
#action_class ⇒ Object
readonly
Returns the value of attribute action_class.
-
#action_method ⇒ Object
readonly
Returns the value of attribute action_method.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#full_name ⇒ Object
readonly
Returns the value of attribute full_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#option_schemas ⇒ Object
readonly
Returns the value of attribute option_schemas.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #help_message(command) ⇒ Object
-
#initialize(full_name, name, desc, option_schemas, action_class, action_method) ⇒ ActionInfo
constructor
A new instance of ActionInfo.
Constructor Details
#initialize(full_name, name, desc, option_schemas, action_class, action_method) ⇒ ActionInfo
Returns a new instance of ActionInfo.
322 323 324 325 326 327 328 329 |
# File 'lib/benry/cli.rb', line 322 def initialize(full_name, name, desc, option_schemas, action_class, action_method) @full_name = full_name @name = name @desc = desc @option_schemas = option_schemas @action_class = action_class @action_method = action_method end |
Instance Attribute Details
#action_class ⇒ Object (readonly)
Returns the value of attribute action_class.
331 332 333 |
# File 'lib/benry/cli.rb', line 331 def action_class @action_class end |
#action_method ⇒ Object (readonly)
Returns the value of attribute action_method.
331 332 333 |
# File 'lib/benry/cli.rb', line 331 def action_method @action_method end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
331 332 333 |
# File 'lib/benry/cli.rb', line 331 def desc @desc end |
#full_name ⇒ Object (readonly)
Returns the value of attribute full_name.
331 332 333 |
# File 'lib/benry/cli.rb', line 331 def full_name @full_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
331 332 333 |
# File 'lib/benry/cli.rb', line 331 def name @name end |
#option_schemas ⇒ Object (readonly)
Returns the value of attribute option_schemas.
331 332 333 |
# File 'lib/benry/cli.rb', line 331 def option_schemas @option_schemas end |
Instance Method Details
#==(other) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/benry/cli.rb', line 333 def ==(other) return ( self.class == other.class \ && @full_name == other.full_name \ && @name == other.name \ && @desc == other.desc \ && @option_schemas == other.option_schemas \ && @action_class == other.action_class \ && @action_method == other.action_method ) end |
#help_message(command) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/benry/cli.rb', line 345 def (command) #; [!hjq5l] builds help message. meth = @action_class.new.method(@action_method) argstr = "" meth.parameters.each do |kind, name| #; [!7qmnz] replaces '_' in arg names with '-'. #; [!s6p09] converts arg name 'file_or_dir' into 'file|dir'. name_str = name.to_s.gsub('_or_', '|').gsub('_', '-') case kind when :req ; argstr << " <#{name_str}>" when :opt ; argstr << " [<#{name_str}>]" when :rest; argstr << " [<#{name_str}>...]" end end #; [!6m50d] don't show non-described options. pairs = @option_schemas.collect {|opt| [opt.option_string, opt.desc] } pairs = pairs.select {|optstr, desc| desc } # width = pairs.collect {|pair| pair[0].length }.max || 0 width = [width, 20].max width = [width, 35].min # msg = "" #msg << "#{command} #{@full_name} -- #{@desc}\n" msg << "#{@desc}\n" msg << "\n" msg << "Usage:\n" msg << " #{command} #{@full_name} [<options>]#{argstr}\n" msg << "\n" unless pairs.empty? msg << "Options:\n" unless pairs.empty? pairs.each do |option_string, desc| msg << " %-#{width}s : %s\n" % [option_string, desc] end return msg end |