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)


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

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)


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

def control
  @control
end

#datatypeClass, Array<String> (readonly)

Potential values (for select or radio) or Ruby datatype

Returns:



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

def datatype
  @datatype
end

#defaultObject (readonly)

Default value for this option

Returns:

  • (Object)


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

def default
  @default
end

#descriptionString (readonly)

Description of this option (optional)

Returns:



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

def description
  @description
end

#onArray<String> (readonly)

Arguments passed to OptionParser#on

Returns:



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

def on
  @on
end

#symbolSymbol (readonly)

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

Returns:

  • (Symbol)


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

def symbol
  @symbol
end

#use:optional, ...

Use of this option

Returns:

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


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

def use
  @use
end

Instance Method Details

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



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

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



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

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