Class: CommandLine::OptionData

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, unknown_options, 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(unknown_options))
  @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

#argsObject (readonly)

Returns the value of attribute args.



22
23
24
# File 'lib/commandline/optionparser/optiondata.rb', line 22

def args
  @args
end

#argvObject (readonly)

Returns the value of attribute argv.



22
23
24
# File 'lib/commandline/optionparser/optiondata.rb', line 22

def argv
  @argv
end

#cmdObject (readonly)

Returns the value of attribute cmd.



22
23
24
# File 'lib/commandline/optionparser/optiondata.rb', line 22

def cmd
  @cmd
end

#not_parsedObject (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_optionsObject (readonly)

Returns the value of attribute unknown_options.



22
23
24
# File 'lib/commandline/optionparser/optiondata.rb', line 22

def unknown_options
  @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

Raises:



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

Returns:

  • (Boolean)


59
60
61
# File 'lib/commandline/optionparser/optiondata.rb', line 59

def has_option?(key)
  @opts.has_key?(key)
end

#to_hObject



69
70
71
# File 'lib/commandline/optionparser/optiondata.rb', line 69

def to_h
  Marshal.load(Marshal.dump(@opts))
end