Class: CommandLine::OptionData
- Inherits:
-
Object
- Object
- CommandLine::OptionData
- Defined in:
- lib/commandline/optionparser/optiondata.rb
Overview
Data resulting from parsing a command line (Array) using a particular OptionParser object
Defined Under Namespace
Classes: InvalidActionError, OptionDataError, UnknownOptionError
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#not_parsed ⇒ Object
readonly
Returns the value of attribute not_parsed.
-
#unknown_options ⇒ Object
readonly
Returns the value of attribute unknown_options.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #has_option?(key) ⇒ Boolean
-
#initialize(argv, opts, unknown_options, args, not_parsed, cmd) ⇒ OptionData
constructor
argv: Original commandline parsed options passed on the commandline? unknown options ?? args found on commandline array of arguments that was not parsed – probably because of ‘–’ the command if in command mode.
-
#method_missing(sym) ⇒ Object
As a convenience, options may be accessed by their name without the dashes.
- #to_h ⇒ Object
Constructor Details
#initialize(argv, opts, unknown_options, args, not_parsed, cmd) ⇒ OptionData
argv: Original commandline parsed options passed on the commandline? unknown options ?? args found on commandline array of arguments that was not parsed – probably because of ‘–’ the command if in command mode
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/commandline/optionparser/optiondata.rb', line 34 def initialize(argv, opts, , args, not_parsed, cmd) @opts = {} opts.each { |k,v| @opts[k] = begin Marshal.load(Marshal.dump(v)) rescue v end } @unknown_options = Marshal.load(Marshal.dump()) @not_parsed = Marshal.load(Marshal.dump(not_parsed)) @argv = Marshal.load(Marshal.dump(argv)) @args = Marshal.load(Marshal.dump(args)) @cmd = Marshal.load(Marshal.dump(cmd)) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym) ⇒ Object
As a convenience, options may be accessed by their name without the dashes. Of course, this won’t work for options with names like ‘–with-shared’. For these names, you must still use option_data
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/commandline/optionparser/optiondata.rb', line 77 def method_missing(sym) k1 = "--#{sym}" k2 = "-#{sym}" if @opts.has_key?(k1) @opts[k1] elsif @opts.has_key?(k2) @opts[k2] else raise UnknownOptionError, "Unknown option (--|-)#{sym}." end end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
22 23 24 |
# File 'lib/commandline/optionparser/optiondata.rb', line 22 def args @args end |
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
22 23 24 |
# File 'lib/commandline/optionparser/optiondata.rb', line 22 def argv @argv end |
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
22 23 24 |
# File 'lib/commandline/optionparser/optiondata.rb', line 22 def cmd @cmd end |
#not_parsed ⇒ Object (readonly)
Returns the value of attribute not_parsed.
22 23 24 |
# File 'lib/commandline/optionparser/optiondata.rb', line 22 def not_parsed @not_parsed end |
#unknown_options ⇒ Object (readonly)
Returns the value of attribute unknown_options.
22 23 24 |
# File 'lib/commandline/optionparser/optiondata.rb', line 22 def @unknown_options end |
Instance Method Details
#[](key) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/commandline/optionparser/optiondata.rb', line 51 def [](key) if @opts.has_key?(key) @opts[key] else raise(UnknownOptionError, "Unknown option '#{key}'.") end end |
#[]=(key, val) ⇒ Object
63 64 65 66 67 |
# File 'lib/commandline/optionparser/optiondata.rb', line 63 def []=(key, val) raise(InvalidActionError, "Cannot modify existing option data: "+ "#{key.inspect} => #{val.inspect}") if @opts.has_key?(key) @opts[key] = val end |
#has_option?(key) ⇒ Boolean
59 60 61 |
# File 'lib/commandline/optionparser/optiondata.rb', line 59 def has_option?(key) @opts.has_key?(key) end |
#to_h ⇒ Object
69 70 71 |
# File 'lib/commandline/optionparser/optiondata.rb', line 69 def to_h Marshal.load(Marshal.dump(@opts)) end |