Module: Can::ArgParse

Defined in:
lib/can/argparse.rb

Constant Summary collapse

Version =
VERSION

Class Method Summary collapse

Class Method Details

.help_string(mode) ⇒ Object

Returns a mode’s help string



80
81
82
# File 'lib/can/argparse.rb', line 80

def self.help_string(mode)
  ALL_FLAGS[mode][2]
end

.incompatible_opts?(options) ⇒ Boolean

Sees if options has incompatible items

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/can/argparse.rb', line 62

def self.incompatible_opts?(options)
  modes = MODES.keys
  (options & modes).length > 1
end

.init_args(argv) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/can/argparse.rb', line 43

def self.init_args(argv)
  options = Set.new

  OptionParser.new do |opts|
    opts.banner = USAGE

    ALL_FLAGS.each do |mode, _v|
      opts.on(short_opt(mode), long_opt(mode), help_string(mode)) do |_opt|
        options << mode
      end
    end
  end.parse!(argv)

  Error.fatal 'Too many mode arguments' if ArgParse.incompatible_opts?(options)

  return options, argv
end

.long_opt(mode) ⇒ Object



75
76
77
# File 'lib/can/argparse.rb', line 75

def self.long_opt(mode)
  ALL_FLAGS[mode][1]
end

.mode(options) ⇒ Object



67
68
69
# File 'lib/can/argparse.rb', line 67

def self.mode(options)
  (options & MODES.keys).first || :trash
end

.short_opt(mode) ⇒ Object



71
72
73
# File 'lib/can/argparse.rb', line 71

def self.short_opt(mode)
  ALL_FLAGS[mode][0]
end

.valid_opt?(opt) ⇒ Boolean

Returns an options’s corresponding mode, if it is valid

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/can/argparse.rb', line 85

def self.valid_opt?(opt)
  result = MODES.find do |_k, v|
    v[0..2].include? opt
  end
  result&.first
end