Class: RDF::CLI::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/cli.rb

Overview

Option description for use within Readers/Writers. See Reader.options and Writer.options for example usage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol: nil, on: nil, datatype: nil, control: nil, description: nil, use: :optional, **options) {|value, options| ... } ⇒ Option

Create a new option with optional callback.

Parameters:

  • symbol (Symbol)
  • on (Array<String>)
  • datatype (String)
  • control (String)
  • description (String)
  • use ([:optional, :disabled, :removed, :required])

Yields:

  • value which may be used within OptionParser#on

Yield Parameters:

  • value (Object)

    The option value as parsed using on argument

  • options (OptionParser)

    (nil) optional OptionParser

Yield Returns:

  • (Object)

    a possibly modified input value

Raises:

  • (ArgumentError)


120
121
122
123
124
125
# File 'lib/rdf/cli.rb', line 120

def initialize(symbol: nil, on: nil, datatype: nil, control: nil,
               description: nil, use: :optional, **options, &block)
  raise ArgumentError, "symbol is a required argument" unless symbol
  raise ArgumentError, "on is a required argument" unless on
  @symbol, @on, @datatype, @control, @description, @use, @callback = symbol.to_sym, Array(on), datatype, control, description, use, block
end

Instance Attribute Details

#control:text, ... (readonly)

Associated HTML form control

Returns:

  • (:text, :textarea, :radio, :checkbox, :select, :url, :url2, :none)


101
102
103
# File 'lib/rdf/cli.rb', line 101

def control
  @control
end

#datatypeClass, Array<String> (readonly)

Potential values (for select or radio) or Ruby datatype

Returns:

  • (Class, Array<String>)


97
98
99
# File 'lib/rdf/cli.rb', line 97

def datatype
  @datatype
end

#descriptionString (readonly)

Description of this option (optional)

Returns:

  • (String)


93
94
95
# File 'lib/rdf/cli.rb', line 93

def description
  @description
end

#onArray<String> (readonly)

Arguments passed to OptionParser#on

Returns:

  • (Array<String>)


89
90
91
# File 'lib/rdf/cli.rb', line 89

def on
  @on
end

#symbolSymbol (readonly)

Symbol used for this option when calling Reader.new

Returns:

  • (Symbol)


85
86
87
# File 'lib/rdf/cli.rb', line 85

def symbol
  @symbol
end

#use:optional, ...

Use of this option

Returns:

  • (:optional, :disabled, :removed, :required)


105
106
107
# File 'lib/rdf/cli.rb', line 105

def use
  @use
end

Instance Method Details

#call(arg, options = {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rdf/cli.rb', line 127

def call(arg, options = {})
  if @callback
    case @callback.arity
    when 0 then @callback.call
    when 1 then @callback.call(arg)
    when 2 then @callback.call(arg, options)
    else arg
    end
  else
    arg
  end
end

#to_hashObject

Return version of commands appropriate for use in JSON



141
142
143
144
145
146
147
148
149
# File 'lib/rdf/cli.rb', line 141

def to_hash
  {
    symbol:       symbol,
    datatype:     (datatype.is_a?(Class) ? datatype.name : datatype),
    control:      control,
    description:  description,
    use:          use
  }
end