Class: BBLib::OptsParser

Inherits:
Object
  • Object
show all
Includes:
Effortless
Defined in:
lib/bblib/cli/option.rb,
lib/bblib/cli/opts_parser.rb,
lib/bblib/cli/options/bool.rb,
lib/bblib/cli/options/date.rb,
lib/bblib/cli/options/json.rb,
lib/bblib/cli/options/time.rb,
lib/bblib/cli/options/float.rb,
lib/bblib/cli/options/regexp.rb,
lib/bblib/cli/options/string.rb,
lib/bblib/cli/options/symbol.rb,
lib/bblib/cli/options/toggle.rb,
lib/bblib/cli/options/command.rb,
lib/bblib/cli/options/integer.rb,
lib/bblib/cli/options/untoggle.rb,
lib/bblib/cli/options/element_of.rb,
lib/bblib/cli/options/basic_option.rb

Defined Under Namespace

Classes: BasicOption, Bool, Command, Date, ElementOf, Float, Integer, JSON, Option, Regexp, String, Symbol, Time, Toggle, Untoggle

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Effortless

#_attrs, included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



67
68
69
70
71
72
73
74
75
76
# File 'lib/bblib/cli/opts_parser.rb', line 67

def method_missing(method, *args, &block)
  if Option.types.include?(method)
    define_singleton_method(method) do |*flags, **opts, &block|
      on(*flags, **opts.merge(type: method), &block)
    end
    send(method, *args, &block)
  else
    super
  end
end

Class Method Details

.build(&block) ⇒ Object



25
26
27
# File 'lib/bblib/cli/opts_parser.rb', line 25

def self.build(&block)
  new(&block)
end

Instance Method Details

#at(position, **opts, &block) ⇒ Object



34
35
36
# File 'lib/bblib/cli/opts_parser.rb', line 34

def at(position, **opts, &block)
  add_options(opts.merge(type: :at, position: position, processor: block))
end

#helpObject



56
57
58
59
# File 'lib/bblib/cli/opts_parser.rb', line 56

def help
  usage.to_s + "\n\t" +
  options.join("\n\t")
end

#on(*flags, **opts, &block) ⇒ Object



38
39
40
41
# File 'lib/bblib/cli/opts_parser.rb', line 38

def on(*flags, **opts, &block)
  opts[:type] = :string unless opts[:type]
  add_options(opts.merge(flags: flags, processor: block))
end

#parse(args = ARGV) ⇒ Object



43
44
45
# File 'lib/bblib/cli/opts_parser.rb', line 43

def parse(args = ARGV)
  parse!(args.dup)
end

#parse!(args = ARGV) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/bblib/cli/opts_parser.rb', line 47

def parse!(args = ARGV)
  args = [args] unless args.is_a?(Array)
  HashStruct.new.tap do |hash|
    options.sort_by { |opt| opt.position || 10**100 }.each do |option|
      option.retrieve(args, hash)
    end
  end.merge(arguments: args.compact)
end

#to_sObject



61
62
63
# File 'lib/bblib/cli/opts_parser.rb', line 61

def to_s
  help
end

#usage(text = nil) ⇒ Object



29
30
31
32
# File 'lib/bblib/cli/opts_parser.rb', line 29

def usage(text = nil)
  @usage = text unless text.nil?
  @usage
end