Class: OptParseSimple::Option
- Inherits:
-
Object
- Object
- OptParseSimple::Option
- Defined in:
- lib/optparse-simple.rb
Overview
represents a single option, eg. its matchers (-f, –foo) and its block of logic to call, if matched
Instance Attribute Summary collapse
-
#accepts_argument ⇒ Object
(also: #accept_argument?, #accept_arguments?, #accepts_argument?, #accepts_arguments?)
Returns the value of attribute accepts_argument.
-
#long_string ⇒ Object
Returns the value of attribute long_string.
-
#proc ⇒ Object
Returns the value of attribute proc.
-
#short_string ⇒ Object
Returns the value of attribute short_string.
Instance Method Summary collapse
-
#initialize(*args, &block) ⇒ Option
constructor
A new instance of Option.
- #long_string_with_dashes ⇒ Object
- #match(*args) ⇒ Object
- #match!(*args) ⇒ Object
- #parse(*args) ⇒ Object
- #parse!(*args) ⇒ Object
- #short_string_with_dash ⇒ Object
Constructor Details
#initialize(*args, &block) ⇒ Option
Returns a new instance of Option.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/optparse-simple.rb', line 57 def initialize *args, &block set_defaults @proc = block # take arguments (eg. '-f' or '--f' or '--f [foo]') # and set #short_string, #long_string, and #accepts_argument args.each do |arg| case arg when /^-(\w)/ @short_string = $1 when /^--(\w+)/ @long_string = $1 else raise "Don't know how to handle argument: #{ arg.inspect }" end @accepts_argument = true if arg =~ /\[.*\]$/ end end |
Instance Attribute Details
#accepts_argument ⇒ Object Also known as: accept_argument?, accept_arguments?, accepts_argument?, accepts_arguments?
Returns the value of attribute accepts_argument.
55 56 57 |
# File 'lib/optparse-simple.rb', line 55 def accepts_argument @accepts_argument end |
#long_string ⇒ Object
Returns the value of attribute long_string.
55 56 57 |
# File 'lib/optparse-simple.rb', line 55 def long_string @long_string end |
#proc ⇒ Object
Returns the value of attribute proc.
55 56 57 |
# File 'lib/optparse-simple.rb', line 55 def proc @proc end |
#short_string ⇒ Object
Returns the value of attribute short_string.
55 56 57 |
# File 'lib/optparse-simple.rb', line 55 def short_string @short_string end |
Instance Method Details
#long_string_with_dashes ⇒ Object
82 83 84 |
# File 'lib/optparse-simple.rb', line 82 def long_string_with_dashes (long_string) ? "--#{ long_string }" : '' end |
#match(*args) ⇒ Object
96 97 98 99 |
# File 'lib/optparse-simple.rb', line 96 def match *args args = args.first if args.first.is_a?(Array) and args.length == 1 do_parsing args, :run => false, :destructive => false end |
#match!(*args) ⇒ Object
101 102 103 104 |
# File 'lib/optparse-simple.rb', line 101 def match! *args args = args.first if args.first.is_a?(Array) and args.length == 1 do_parsing args, :run => false, :destructive => true end |
#parse(*args) ⇒ Object
86 87 88 89 |
# File 'lib/optparse-simple.rb', line 86 def parse *args args = args.first if args.first.is_a?(Array) and args.length == 1 do_parsing args, :run => true, :destructive => false end |
#parse!(*args) ⇒ Object
91 92 93 94 |
# File 'lib/optparse-simple.rb', line 91 def parse! *args args = args.first if args.first.is_a?(Array) and args.length == 1 do_parsing args, :run => true, :destructive => true end |
#short_string_with_dash ⇒ Object
78 79 80 |
# File 'lib/optparse-simple.rb', line 78 def short_string_with_dash (short_string) ? "-#{ short_string }" : '' end |