Class: CLI::Kit::Opts

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Mixin
Defined in:
lib/cli/kit/opts.rb

Defined Under Namespace

Modules: Mixin

Constant Summary collapse

DEFAULT_OPTIONS =
[:helpflag]

Instance Method Summary collapse

Methods included from T::Sig

sig

Methods included from Mixin

#flag, included, #multi_option, #option, #option!, #position, #position!, #rest

Instance Method Details

#[](name) ⇒ Object



250
251
252
253
254
255
256
257
# File 'lib/cli/kit/opts.rb', line 250

def [](name)
  obj = assert_result!
  if obj.opt.respond_to?(name)
    obj.opt.send(name)
  elsif obj.flag.respond_to?(name)
    obj.flag.send(name)
  end
end

#assert_result!Object

Raises:

  • (NotImplementedError)


277
278
279
280
281
# File 'lib/cli/kit/opts.rb', line 277

def assert_result!
  raise(NotImplementedError, 'not implemented') if @obj.is_a?(Args::Definition)

  @obj
end

#define!(defn) ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'lib/cli/kit/opts.rb', line 284

def define!(defn)
  @obj = defn
  T.cast(self.class, Mixin::MixinClassMethods).tracked_methods.each do |m|
    send(m)
  end
  DEFAULT_OPTIONS.each do |m|
    send(m)
  end
end

#each_flag(&block) ⇒ Object



238
239
240
241
242
243
244
245
246
247
# File 'lib/cli/kit/opts.rb', line 238

def each_flag(&block)
  return(enum_for(:each_flag)) unless block_given?

  obj = assert_result!
  obj.defn.flags.each do |flag|
    name = flag.name
    value = obj.flag.send(name)
    yield(name, value)
  end
end

#each_option(&block) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/cli/kit/opts.rb', line 220

def each_option(&block)
  return(enum_for(:each_option)) unless block_given?

  obj = assert_result!
  obj.defn.options.each do |opt|
    name = opt.name
    value = obj.opt.send(name)
    yield(name, value)
  end
end

#evaluate!(ev) ⇒ Object



295
296
297
298
# File 'lib/cli/kit/opts.rb', line 295

def evaluate!(ev)
  @obj = ev
  ev.resolve_positions!
end

#helpflagObject



203
204
205
# File 'lib/cli/kit/opts.rb', line 203

def helpflag
  flag(name: :help, short: '-h', long: '--help', desc: 'Show this help message')
end

#lookup_flag(name) ⇒ Object



269
270
271
272
273
274
# File 'lib/cli/kit/opts.rb', line 269

def lookup_flag(name)
  obj = assert_result!
  obj.flag.send(name)
rescue NoMethodError
  false
end

#lookup_option(name) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/cli/kit/opts.rb', line 260

def lookup_option(name)
  obj = assert_result!
  obj.opt.send(name)
rescue NoMethodError
  # TODO: should we raise a KeyError?
  nil
end

#unparsedObject



208
209
210
211
# File 'lib/cli/kit/opts.rb', line 208

def unparsed
  obj = assert_result!
  obj.unparsed
end