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



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

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

.incompatible_opts?(options) ⇒ Boolean

Sees if options has incompatible items

Returns:

  • (Boolean)


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

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

.init_argsObject



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

def self.init_args
  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!

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

  options
end

.long_opt(mode) ⇒ Object



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

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

.mode(options) ⇒ Object



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

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

.short_opt(mode) ⇒ Object



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

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)


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

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