Class: WifiWand::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wifi_wand/commands/base.rb

Defined Under Namespace

Classes: LazyModel

Constant Summary collapse

DEFAULT_INVOCATION_OPTIONS =
%i[help version verbose].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata: nil, cli: nil, **attributes) ⇒ Base

Returns a new instance of Base.



133
134
135
136
137
# File 'lib/wifi_wand/commands/base.rb', line 133

def initialize(metadata: nil, cli: nil, **attributes)
  @metadata =  || 
  @cli = cli
  assign_attributes(attributes)
end

Instance Attribute Details

#cliObject (readonly)

Returns the value of attribute cli.



131
132
133
# File 'lib/wifi_wand/commands/base.rb', line 131

def cli
  @cli
end

#metadataObject (readonly)

Returns the value of attribute metadata.



131
132
133
# File 'lib/wifi_wand/commands/base.rb', line 131

def 
  @metadata
end

Class Method Details

.allow_invocation_options(*option_names) ⇒ Object



116
117
118
119
# File 'lib/wifi_wand/commands/base.rb', line 116

def allow_invocation_options(*option_names)
  @allowed_invocation_options ||= []
  @allowed_invocation_options |= option_names
end

.allowed_invocation_optionsObject



121
122
123
124
125
126
127
128
# File 'lib/wifi_wand/commands/base.rb', line 121

def allowed_invocation_options
  inherited_options = if superclass.respond_to?(:allowed_invocation_options)
    superclass.allowed_invocation_options
  else
    DEFAULT_INVOCATION_OPTIONS
  end
  inherited_options | (@allowed_invocation_options || [])
end

.binding_sourcesObject

Subclasses inherit their parents' binding declarations so specialized commands can add to the shared binding set instead of replacing it.



111
112
113
114
# File 'lib/wifi_wand/commands/base.rb', line 111

def binding_sources
  inherited_sources = superclass.respond_to?(:binding_sources) ? superclass.binding_sources : {}
  inherited_sources.merge(@binding_sources || {})
end

.binds(*attributes, **mapped_attributes) ⇒ Object

Declare which values a bound command instance should pull from the CLI. binds :model copies cli.model, while binds output: :out_stream maps a command attribute name to a different CLI reader.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/wifi_wand/commands/base.rb', line 95

def binds(*attributes, **mapped_attributes)
  @binding_sources ||= {}

  attributes.each do |attribute|
    @binding_sources[attribute] = attribute
  end

  mapped_attributes.each do |attribute, source|
    @binding_sources[attribute] = source
  end

  attr_reader(*@binding_sources.keys)
end

.command_metadata(short_string:, long_string:, description: nil, usage: nil) ⇒ Object

Standard command classes can declare their names and help text once here instead of repeating the same metadata object construction in initialize.



74
75
76
77
78
79
80
81
# File 'lib/wifi_wand/commands/base.rb', line 74

def (short_string:, long_string:, description: nil, usage: nil)
  @declared_metadata = Metadata.new(
    short_string: short_string,
    long_string:  long_string,
    description:  description,
    usage:        usage
  )
end

.declared_metadataObject

Command subclasses look here first for their metadata declaration; the constant-based fallback stays in place so older or specialized commands can keep working until they are migrated.



86
87
88
89
90
# File 'lib/wifi_wand/commands/base.rb', line 86

def 
  return @declared_metadata if instance_variable_defined?(:@declared_metadata)

  superclass.respond_to?(:declared_metadata) ? superclass. : nil
end

Instance Method Details

#aliasesObject



139
140
141
# File 'lib/wifi_wand/commands/base.rb', line 139

def aliases
  .aliases
end

#bind(cli) ⇒ Object

Turn a registered command definition into the executable command object for this CLI invocation by copying the declared bound attributes from cli.



145
146
147
148
# File 'lib/wifi_wand/commands/base.rb', line 145

def bind(cli)
  attributes = bound_attributes_for(cli)
  self.class.new(metadata: , cli: cli, **attributes)
end

#help_textObject



150
151
152
153
154
155
156
157
158
# File 'lib/wifi_wand/commands/base.rb', line 150

def help_text
  return .usage unless .description

  <<~HELP
    #{.usage}

    #{.description}
    HELP
end

#validate_options(invocation_options:, command_options:, args:, context: nil) ⇒ Object

context: is reserved for command-specific validators that need parser context beyond the already-normalized options and positional arguments.



162
163
164
165
166
# File 'lib/wifi_wand/commands/base.rb', line 162

def validate_options(invocation_options:, command_options:, args:, context: nil)
  invalid_invocation_options(invocation_options).map do |option_name|
    "#{invocation_option_display(option_name)} is not valid for #{.long_string}."
  end
end