Class: Asciimation::ArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/asciimation/argument_parser.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{animator: "drop", duration: 5.0}

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ArgumentParser

Returns a new instance of ArgumentParser.



7
8
9
# File 'lib/asciimation/argument_parser.rb', line 7

def initialize(args)
  @args = args
end

Instance Method Details

#to_hashObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/asciimation/argument_parser.rb', line 14

def to_hash
  options = DEFAULT_OPTIONS.dup
  hash    = {io: nil, options: options}

  args.options do |parser|
    parser.banner =
      "Usage:  #{File.basename($PROGRAM_NAME)} [OPTIONS] ASCII_ART_PATH"

    parser.separator ""
    parser.separator "Specific Options:"

    parser.on( "--animator NAME", String,
               "The animator to use." ) do |animator|
      options[:animator] = animator
    end
    parser.on( "-o", "--duration SECONDS", Float,
               "The seconds this animation should last." ) do |duration|
      options[:duration] = duration
    end

    parser.separator "Common Options:"

    parser.on( "-h", "--help",
               "Show this message." ) do
      puts parser
      exit
    end

    begin
      path      = args.shift or abort parser.to_s
      hash[:io] = open(path)

      parser.parse!
    rescue OptionParser::ParseError
      abort parser.to_s
    end
  end

  hash
end