Module: Aspera::Cli::OptionDeclarator

Included in:
Formatter, Http, Plugins::Base, 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 and Proc on_set callbacks (defaults to self)



61
62
63
64
65
# File 'lib/aspera/cli/option_declarator.rb', line 61

def declare_options(parser, target: self)
  option_specs.each_value do |spec|
    spec.declare_on(parser, target: target) unless parser.option_declared?(spec.name)
  end
end

#option(name, description: nil, short: nil, allowed: nil, default: nil, on_set: nil, shorthand: 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; if nil, derived from schema: title/description

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

    Single-character short form (without leading '-')

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

    Allowed values (see OptionValue)

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

    Default value

  • on_set (Symbol, Proc, #call, nil) (defaults to: nil) —

    on_set callback (see OptionSpec)

  • shorthand (String, nil) (defaults to: nil) —

    For a Hash option: a String value is stored as {shorthand => value}

  • deprecation (Hash, nil) (defaults to: nil) —

    Deprecation {last:, message:} forwarded to options.declare (see Deprecation)

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

    Schema reference (e.g. "opts:components.schemas.Foo"); when description: is nil, the schema title or first description line is used



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

def option(name, description: nil,
  short: nil, allowed: nil, default: nil,
  on_set: nil, shorthand: nil, deprecation: nil, schema: nil)
  register_option_spec(
    OptionSpec.new(
      name:        name,
      description: description,
      short:       short,
      allowed:     allowed,
      default:     default,
      on_set:      on_set,
      shorthand:   shorthand,
      deprecation: deprecation,
      schema:      schema
    )
  )
end

#option_specs ⇒ Hash{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

#register_option_spec(spec) ⇒ Object

Store an OptionSpec in this class's registry.

Parameters:

Raises:

  • (ArgumentError) —

    on duplicate option name



50
51
52
53
# File 'lib/aspera/cli/option_declarator.rb', line 50

def register_option_spec(spec)
  raise ArgumentError, "Duplicate option: #{spec.name.inspect}" if option_specs.key?(spec.name)
  option_specs[spec.name] = spec
end