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, default: nil, **options) {|value, options| ... } ⇒ Option

Create a new option with optional callback.

Parameters:

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

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)


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

def initialize(symbol: nil, on: nil, datatype: nil, control: nil,
               description: nil, use: :optional, default: nil, **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, @default, @callback = symbol.to_sym, Array(on), datatype, control, description, use, default, block
end

Instance Attribute Details

#control:text, ... (readonly)

Associated HTML form control

Returns:

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


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

def control
  @control
end

#datatypeClass, Array<String> (readonly)

Potential values (for select or radio) or Ruby datatype

Returns:

  • (Class, Array<String>)


119
120
121
# File 'lib/rdf/cli.rb', line 119

def datatype
  @datatype
end

#defaultObject (readonly)

Default value for this option

Returns:

  • (Object)


115
116
117
# File 'lib/rdf/cli.rb', line 115

def default
  @default
end

#descriptionString (readonly)

Description of this option (optional)

Returns:

  • (String)


111
112
113
# File 'lib/rdf/cli.rb', line 111

def description
  @description
end

#onArray<String> (readonly)

Arguments passed to OptionParser#on

Returns:

  • (Array<String>)


107
108
109
# File 'lib/rdf/cli.rb', line 107

def on
  @on
end

#symbolSymbol (readonly)

Symbol used for this option when calling ‘Reader.new`

Returns:

  • (Symbol)


103
104
105
# File 'lib/rdf/cli.rb', line 103

def symbol
  @symbol
end

#use:optional, ...

Use of this option

Returns:

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


127
128
129
# File 'lib/rdf/cli.rb', line 127

def use
  @use
end

Instance Method Details

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



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rdf/cli.rb', line 150

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



164
165
166
167
168
169
170
171
172
173
# File 'lib/rdf/cli.rb', line 164

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