Class: OXO::Optionparser

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

Class Method Summary collapse

Class Method Details

.parse!(argv) ⇒ Object

Parses the command line options from argv. (argv is cleared). Might print out help or version information.

argv - array with the command line options

Returns a hash containing the option parameters.

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/oxo.rb', line 48

def self.parse!(argv)

  options = {
    delay: 0
  }

  opt_parser = OptionParser.new do |opt|
    opt.banner = "Usage: #{PROGNAME} [options]"
    opt.separator <<~CONTENT

      oxo is a command line Tic-tac-toe game.

      Options:
    CONTENT

    # process --version and --help first,
    # exit successfully (GNU Coding Standards)
    opt.on_tail("-h", "--help", "Print a brief help message and exit.") do
      puts opt_parser
      puts "\nReport bugs on the #{PROGNAME} home page: <#{HOMEPAGE}>"
      exit
    end

    opt.on_tail("-v", "--version",
                "Print a brief version information and exit.") do
      puts "#{PROGNAME} #{VERSION}"
      puts COPYRIGHT
      exit
    end

    opt.on("-d", "--delay SECONDS", Float,
           "Set delay time for oxo's moves.") do |d|
      options[:delay] = d
    end

    opt.separator ""
  end
  opt_parser.parse!(argv)

  # nothing should be left in argv
  raise(ArgumentError, "wrong number of arguments")  unless argv.empty?

  options
end