Module: Aspera::Cli::Option::Declarator

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)



72
73
74
75
76
# File 'lib/aspera/cli/option/declarator.rb', line 72

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, complete: nil, security: nil, before: 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 Option::Value)

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

    Default value

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

    on_set callback (see Option)

  • 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

  • complete (Symbol, nil) (defaults to: nil) —

    Values proposed by shell completion, one of Complete::KINDS

  • security (Symbol, nil) (defaults to: nil) —

    Most restrictive security level at which the option is available (see Security)

  • before (Symbol, nil) (defaults to: nil) —

    Option of a plugin class to place this one before, e.g. option of a module mounted on another plugin (see Command::Registry#register_option)



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aspera/cli/option/declarator.rb', line 35

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

#option_specs ⇒ Hash{Symbol => Option}

Registry of Option objects defined on this class/module.

Returns:



16
17
18
# File 'lib/aspera/cli/option/declarator.rb', line 16

def option_specs
  @option_specs ||= {}
end

#register_option_spec(spec, before: nil) ⇒ Object

Store an Option in this class's registry.

Parameters:

  • spec (Option)
  • before (nil) (defaults to: nil) —

    Placement: only for the options of plugin classes (see Plugins::Base.register_option_spec)

Raises:

  • (ArgumentError) —

    on duplicate option name



60
61
62
63
64
# File 'lib/aspera/cli/option/declarator.rb', line 60

def register_option_spec(spec, before: nil)
  raise ArgumentError, "Option #{spec.name.inspect}: placement is only for options of plugins" unless before.nil?
  raise ArgumentError, "Duplicate option: #{spec.name.inspect}" if option_specs.key?(spec.name)
  option_specs[spec.name] = spec
end