Class: M2A::CLI::Argparse::Argparser

Inherits:
Object
  • Object
show all
Defined in:
lib/m2a/cli/argparse.rb

Instance Method Summary collapse

Constructor Details

#initializeArgparser

Returns a new instance of Argparser.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/m2a/cli/argparse.rb', line 8

def initialize
  @optparser = OptionParser.new do |opts|
    opts.banner = "Usage: ascii_meme <meme_name>"

    opts.on('-w', '--width WIDTH', Float, "Output WIDTH characters wide") do |width| #{{{
      options[:width] = width.to_i
    end #}}}
    opts.on('-h', '--height HEIGHT', Float, "Output HEIGHT characters high") do |height|#{{{
      options[:height] = height.to_i
    end #}}}

    opts.on('--help', "Print this help screen") do
      puts opts
      exit
    end
  end
end

Instance Method Details

#optionsObject



47
48
49
# File 'lib/m2a/cli/argparse.rb', line 47

def options
  @options ||= Hash.new
end

#optsObject

idempotent



27
28
29
# File 'lib/m2a/cli/argparse.rb', line 27

def opts
  @opts ||= parsed_options!
end

#parsed_options!Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/m2a/cli/argparse.rb', line 31

def parsed_options!
  @optparser.parse!
  if ARGV.empty? == 0
    print_help_and_exit
  else
    options[:meme] = ARGV.join(" ")
  end

  OpenStruct.new(options)
end


42
43
44
45
# File 'lib/m2a/cli/argparse.rb', line 42

def print_help_and_exit
  puts @optparser.help
  exit
end