Module: Aspera::Cli::OptionDeclarator

Included in:
Formatter, Http, TransferAgent, Wizard
Defined in:
lib/aspera/cli/option_declarator.rb

Overview

Mixin providing declarative CLI option definitions (option :name, ...) and batch declaration onto a Parser instance (declare_options(parser)).

Instance Method Summary collapse

Instance Method Details

#declare_options(parser, target: self) ⇒ void

This method returns an undefined value.

Declare all options registered on this class onto a Parser instance. Skips options already declared on the parser.

Parameters:

  • parser (Aspera::Cli::Parser)
  • target (Object, nil) (defaults to: self)

    default target object for Symbol handlers (defaults to self)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/aspera/cli/option_declarator.rb', line 49

def declare_options(parser, target: self)
  option_specs.each_value do |spec|
    next if parser.option_declared?(spec.name)
    resolved_handler =
      case spec.handler
      when Symbol then {o: target, m: spec.handler}
      when Hash   then spec.handler
      end
    parser.declare(
      spec.name,
      description: spec.description,
      short:       spec.short,
      allowed:     spec.allowed,
      default:     spec.default,
      handler:     resolved_handler,
      deprecation: spec.deprecation,
      schema:      schema_for_spec(spec)
    )
  end
end

#option(name, description: nil, short: nil, allowed: nil, default: nil, handler: nil, deprecation: nil, schema: nil) ⇒ Object

Declare an option in this class's registry.

Parameters:

  • name (Symbol)

    Option name

  • description (String, nil) (defaults to: nil)

    User-facing description

  • short (String, nil) (defaults to: nil)

    Single-character short form

  • allowed (Object, nil) (defaults to: nil)

    Allowed values

  • default (Object, nil) (defaults to: nil)

    Default value

  • handler (Symbol, Hash, nil) (defaults to: nil)

    Handler (Symbol or Hash)

  • deprecation (String, nil) (defaults to: nil)

    Deprecation message

  • schema (String, nil) (defaults to: nil)

    Schema reference

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aspera/cli/option_declarator.rb', line 27

def option(name, description: nil,
  short: nil, allowed: nil, default: nil,
  handler: nil, deprecation: nil, schema: nil)
  raise ArgumentError, "Duplicate option: #{name.inspect}" if option_specs.key?(name)
  option_specs[name] = OptionSpec.new(
    name:        name,
    description: description,
    short:       short,
    allowed:     allowed,
    default:     default,
    handler:     handler,
    deprecation: deprecation,
    schema:      schema
  )
end

#option_specsHash{Symbol => OptionSpec}

Registry of OptionSpec objects defined on this class/module.

Returns:



13
14
15
# File 'lib/aspera/cli/option_declarator.rb', line 13

def option_specs
  @option_specs ||= {}
end