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/basic_option.rb

Defined Under Namespace

Classes: BasicOption, Bool, Command, Date, 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)



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

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



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

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

Instance Method Details

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



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

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

#helpObject



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

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

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



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

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

#parse(args = ARGV) ⇒ Object



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

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

#parse!(args = ARGV) ⇒ Object



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

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)
end

#to_sObject



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

def to_s
  help
end

#usage(text = nil) ⇒ Object



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

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