Class: Miniparse::Command
- Inherits:
-
Object
- Object
- Miniparse::Command
- Defined in:
- lib/miniparse/command.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
-
#check(arg) ⇒ Object
True if arg specifies this object.
-
#help_desc ⇒ Object
Text of an option specification and description.
-
#initialize(args, &block) ⇒ Command
constructor
uses args: :spec :desc.
-
#run(*args) ⇒ Object
runs the associated block with specified arguments.
Constructor Details
#initialize(args, &block) ⇒ Command
uses args:
:spec
:desc
17 18 19 20 21 22 23 24 |
# File 'lib/miniparse/command.rb', line 17 def initialize(args, &block) @spec = args.fetch(:spec) @desc = args[:desc] @block = block @name = self.class.spec_to_name(spec) raise SyntaxError, "invalid specification '#{spec}'" if name.nil? post_initialize(args) end |
Instance Attribute Details
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
12 13 14 |
# File 'lib/miniparse/command.rb', line 12 def desc @desc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/miniparse/command.rb', line 12 def name @name end |
Class Method Details
.spec_to_name(spec) ⇒ Object
7 8 9 |
# File 'lib/miniparse/command.rb', line 7 def self.spec_to_name(spec) spec_pattern_to_name(spec, /\A(\w[\w-]*)\z/) end |
.valid_spec(*args) ⇒ Object
10 |
# File 'lib/miniparse/command.rb', line 10 def self.valid_spec(*args); spec_to_name(*args); end |
Instance Method Details
#check(arg) ⇒ Object
35 36 37 |
# File 'lib/miniparse/command.rb', line 35 def check(arg) arg.to_s == name.to_s end |
#help_desc ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/miniparse/command.rb', line 40 def help_desc return nil unless desc separator = ' ' width_indent = Miniparse.control(:width_indent) width_left = Miniparse.control(:width_left) - Miniparse.control(:width_indent) width_right = Miniparse.control(:width_display) - separator.size - Miniparse.control(:width_left) if Miniparse.control(:formatted_help) lines = WordWrap.two_cols_word_wrap_lines( spec.to_s + add_spec, separator, desc + add_desc, width_left, width_right) lines.collect! { |line| " "*width_indent + line } lines.join("\n") else s = "%*s" % [width_indent, separator] s += "%-*s" % [width_left, spec.to_s + add_spec] s += ' ' s += desc + add_desc end end |
#run(*args) ⇒ Object
runs the associated block with specified arguments
29 30 31 |
# File 'lib/miniparse/command.rb', line 29 def run(*args) block.call(*args) if block end |