File: permitted.rb

package info (click to toggle)
ruby-optimist 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: ruby: 2,912; makefile: 4
file content (29 lines) | stat: -rwxr-xr-x 1,165 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env ruby
require_relative '../lib/optimist'

opts = Optimist::options do
  opt :french, "starts with french", type: String,
      permitted: %w(fries toast),
      permitted_response: "option %{arg} must be something that starts " +
      "with french, e.g. %{permitted} but you gave '%{given}'"
  opt :dog, "starts with dog", permitted: %r/(house|bone|tail)/, type: String
  opt :zipcode, "zipcode", permitted: %r/^[0-9]{5}$/, default: '39759',
      permitted_response: "option %{arg} must be a zipcode, a five-digit number from 00000..99999"
  opt :adult, "adult age", permitted: (18...99), type: Integer
  opt :minor, "minor age", permitted: (0..18), type: Integer
  opt :letter, "a letter", permitted: ('a'...'z'), type: String
end

p opts

# $ ./permitted.rb -z 384949
# Error: option -z must be a zipcode, a five-digit number from 00000..99999.
# Try --help for help.
#
# $ ./permitted.rb --minor 19
# Error: option '--minor' only accepts value in range of: 0..18.
# Try --help for help.
#
# $ ./permitted.rb -f frog
# Error: option -f must be something that starts with french, e.g. ["fries", "toast"] but you gave 'frog'.
# Try --help for help.