synoption 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/synoption/base_option.rb +132 -0
- data/lib/synoption/boolean_option.rb +19 -0
- data/lib/synoption/doc.rb +78 -0
- data/lib/synoption/exception.rb +7 -0
- data/lib/synoption/fixnum_option.rb +13 -0
- data/lib/synoption/list.rb +43 -0
- data/lib/synoption/match.rb +47 -0
- data/lib/synoption/matchers.rb +22 -0
- data/lib/synoption/option.rb +9 -0
- data/lib/synoption/set.rb +108 -0
- data/test/synoption/base_option_test.rb +180 -0
- data/test/synoption/match_test.rb +59 -0
- data/test/synoption/option_test.rb +13 -0
- data/test/synoption/set_test.rb +56 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fe1c709d5995beaa457aa686b213cce88234b0a1
|
4
|
+
data.tar.gz: 59c59d6d95786ce9e750c0e42418c4665400b229
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 25ac687f69df1bccc08f29136f807af33401fc3643050f6b4959cecd6675608399071d0b25e6a574b7ebcb459cf7ae005292c67e9787e3176ffcf615c24f24d6
|
7
|
+
data.tar.gz: 7d1af4a7d5dea65241177b5eff102fc9de53cd0d3fbae9c865a8efaa7e301a3df11aab09c928d438391c4abbe85dc357da7bcaf5f17d9e73d8ee0946915fdd59
|
@@ -0,0 +1,132 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'logue/loggable'
|
6
|
+
require 'synoption/doc'
|
7
|
+
require 'synoption/match'
|
8
|
+
require 'synoption/matchers'
|
9
|
+
|
10
|
+
module Synoption
|
11
|
+
class BaseOption
|
12
|
+
include Logue::Loggable
|
13
|
+
|
14
|
+
# for as_cmdline_option:
|
15
|
+
NO_CMDLINE_OPTION = Object.new
|
16
|
+
|
17
|
+
attr_reader :name
|
18
|
+
attr_reader :tag
|
19
|
+
attr_reader :description
|
20
|
+
attr_reader :default
|
21
|
+
|
22
|
+
attr_reader :negate
|
23
|
+
attr_reader :regexp
|
24
|
+
|
25
|
+
def initialize name, tag, description, default, options = Hash.new
|
26
|
+
@name = name
|
27
|
+
@tag = tag
|
28
|
+
@description = description
|
29
|
+
|
30
|
+
@value = @default = default
|
31
|
+
|
32
|
+
@matchers = Matchers.new @tag, @name, options[:negate], options[:regexp]
|
33
|
+
|
34
|
+
@negate = options[:negate]
|
35
|
+
@regexp = options[:regexp]
|
36
|
+
|
37
|
+
if options.include? :as_cmdline_option
|
38
|
+
if options[:as_cmdline_option].nil?
|
39
|
+
@as_cmdline_option = NO_CMDLINE_OPTION
|
40
|
+
else
|
41
|
+
@as_cmdline_option = options[:as_cmdline_option]
|
42
|
+
end
|
43
|
+
else
|
44
|
+
@as_cmdline_option = nil
|
45
|
+
end
|
46
|
+
|
47
|
+
@unsets = options[:unsets]
|
48
|
+
end
|
49
|
+
|
50
|
+
def takes_value?
|
51
|
+
true
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_command_line
|
55
|
+
return nil unless value
|
56
|
+
|
57
|
+
if @as_cmdline_option
|
58
|
+
@as_cmdline_option == NO_CMDLINE_OPTION ? nil : @as_cmdline_option
|
59
|
+
else
|
60
|
+
[ tag, value ]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_s
|
65
|
+
[ @name, @tag ].join(", ")
|
66
|
+
end
|
67
|
+
|
68
|
+
def exact_match? arg
|
69
|
+
@matchers.exact.match? arg
|
70
|
+
end
|
71
|
+
|
72
|
+
def negative_match? arg
|
73
|
+
@matchers.negative and @matchers.negative.match? arg
|
74
|
+
end
|
75
|
+
|
76
|
+
def regexp_match? arg
|
77
|
+
@matchers.regexp and @matchers.regexp.match? arg
|
78
|
+
end
|
79
|
+
|
80
|
+
def unset
|
81
|
+
@value = nil
|
82
|
+
end
|
83
|
+
|
84
|
+
def set_value val
|
85
|
+
@value = val
|
86
|
+
end
|
87
|
+
|
88
|
+
def value
|
89
|
+
@value
|
90
|
+
end
|
91
|
+
|
92
|
+
def to_doc io
|
93
|
+
doc = Doc.new self
|
94
|
+
doc.to_doc io
|
95
|
+
end
|
96
|
+
|
97
|
+
def next_argument args
|
98
|
+
raise "ERROR: option #{name} expecting following argument" if args.empty?
|
99
|
+
args.shift
|
100
|
+
end
|
101
|
+
|
102
|
+
def process args
|
103
|
+
if @matchers.exact.match? args[0]
|
104
|
+
args.shift
|
105
|
+
val = takes_value? ? next_argument(args) : true
|
106
|
+
set_value val
|
107
|
+
true
|
108
|
+
elsif @matchers.negative && @matchers.negative.match?(args[0])
|
109
|
+
arg = args.shift
|
110
|
+
set_value false
|
111
|
+
true
|
112
|
+
elsif @matchers.regexp && (md = @matchers.regexp.match?(args[0]))
|
113
|
+
arg = args.shift
|
114
|
+
set_value md[0]
|
115
|
+
true
|
116
|
+
else
|
117
|
+
false
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def post_process option_set, unprocessed
|
122
|
+
resolve_value option_set, unprocessed
|
123
|
+
|
124
|
+
if @unsets
|
125
|
+
option_set.unset @unsets
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def resolve_value option_set, unprocessed
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'synoption/option'
|
5
|
+
|
6
|
+
module Synoption
|
7
|
+
# a boolean option maps to a single tag, not a tag and value. For example,
|
8
|
+
# "-v" (verbose) is a boolean option, but "-r 3444" (revision) is a option
|
9
|
+
# with a value.
|
10
|
+
class BooleanOption < Option
|
11
|
+
def takes_value?
|
12
|
+
false
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_command_line
|
16
|
+
super && [ tag ]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'logue/loggable'
|
6
|
+
|
7
|
+
module Synoption
|
8
|
+
# documentation for an option.
|
9
|
+
class Doc
|
10
|
+
include Logue::Loggable
|
11
|
+
|
12
|
+
def initialize option
|
13
|
+
@option = option
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_doc_tag
|
17
|
+
tagline = "#{@option.tag} [--#{@option.name}]"
|
18
|
+
if @option.takes_value?
|
19
|
+
tagline << " ARG"
|
20
|
+
end
|
21
|
+
tagline
|
22
|
+
end
|
23
|
+
|
24
|
+
# returns an option regexp as a 'cleaner' string
|
25
|
+
def re_to_string re
|
26
|
+
re.source.gsub(%r{\\d\+?}, 'N').gsub(%r{[\^\?\$\\\(\)]}, '')
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_doc_negate
|
30
|
+
doc = nil
|
31
|
+
@option.negate.each do |neg|
|
32
|
+
str = if neg.kind_of? Regexp
|
33
|
+
str = re_to_string neg
|
34
|
+
else
|
35
|
+
str = neg
|
36
|
+
end
|
37
|
+
|
38
|
+
if doc
|
39
|
+
doc << " [#{str}]"
|
40
|
+
else
|
41
|
+
doc = str
|
42
|
+
end
|
43
|
+
end
|
44
|
+
doc
|
45
|
+
end
|
46
|
+
|
47
|
+
# -g [--use-merge-history] : use/display additional information from merge
|
48
|
+
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789
|
49
|
+
# 0 1 2 3 4 5 6
|
50
|
+
|
51
|
+
def to_doc_line lhs, rhs, sep = ""
|
52
|
+
fmt = " %-24s %1s %s"
|
53
|
+
sprintf fmt, lhs, sep, rhs
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_doc io
|
57
|
+
# wrap optdesc?
|
58
|
+
|
59
|
+
[ @option.description ].flatten.each_with_index do |descline, idx|
|
60
|
+
lhs = idx == 0 ? to_doc_tag : ""
|
61
|
+
io.puts to_doc_line lhs, descline, idx == 0 ? ":" : ""
|
62
|
+
end
|
63
|
+
|
64
|
+
if defval = @option.default
|
65
|
+
io.puts to_doc_line "", " default: #{defval}"
|
66
|
+
end
|
67
|
+
|
68
|
+
if re = @option.regexp
|
69
|
+
io.puts to_doc_line re_to_string(re), "same as above", ":"
|
70
|
+
end
|
71
|
+
|
72
|
+
if @option.negate
|
73
|
+
lhs = to_doc_negate
|
74
|
+
io.puts to_doc_line lhs, "", ""
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
module Synoption
|
5
|
+
class OptionList
|
6
|
+
attr_reader :options
|
7
|
+
|
8
|
+
def initialize options = Array.new
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def inspect
|
13
|
+
@options.collect { |opt| opt.inspect }.join("\n")
|
14
|
+
end
|
15
|
+
|
16
|
+
def find_by_name name
|
17
|
+
@options.find { |opt| opt.name == name }
|
18
|
+
end
|
19
|
+
|
20
|
+
def has_option? name
|
21
|
+
find_by_name name
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_command_line
|
25
|
+
cmdline = Array.new
|
26
|
+
@options.each do |opt|
|
27
|
+
if cl = opt.to_command_line
|
28
|
+
cmdline.concat cl
|
29
|
+
end
|
30
|
+
end
|
31
|
+
cmdline
|
32
|
+
end
|
33
|
+
|
34
|
+
def << option
|
35
|
+
add option
|
36
|
+
end
|
37
|
+
|
38
|
+
def add option
|
39
|
+
@options << option
|
40
|
+
option
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'logue/loggable'
|
6
|
+
|
7
|
+
module Synoption
|
8
|
+
class OptionMatch
|
9
|
+
include Logue::Loggable
|
10
|
+
|
11
|
+
def match? arg
|
12
|
+
raise "not implemented"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class OptionExactMatch < OptionMatch
|
17
|
+
def initialize tag, name
|
18
|
+
@tag = tag
|
19
|
+
@name = name
|
20
|
+
end
|
21
|
+
|
22
|
+
def match? arg
|
23
|
+
arg == @tag || arg == '--' + @name.to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class OptionNegativeMatch < OptionMatch
|
28
|
+
def initialize *negopts
|
29
|
+
# in case this gets passed an array as an element:
|
30
|
+
@negopts = Array.new(negopts).flatten
|
31
|
+
end
|
32
|
+
|
33
|
+
def match? arg
|
34
|
+
arg && @negopts.select { |x| arg.index x }.size > 0
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class OptionRegexpMatch < OptionMatch
|
39
|
+
def initialize regexp
|
40
|
+
@regexp = regexp
|
41
|
+
end
|
42
|
+
|
43
|
+
def match? arg
|
44
|
+
arg && @regexp.match(arg)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'logue/loggable'
|
6
|
+
require 'synoption/match'
|
7
|
+
|
8
|
+
module Synoption
|
9
|
+
class Matchers
|
10
|
+
include Logue::Loggable
|
11
|
+
|
12
|
+
attr_reader :exact
|
13
|
+
attr_reader :negative
|
14
|
+
attr_reader :regexp
|
15
|
+
|
16
|
+
def initialize tag, name, negate, regexp
|
17
|
+
@exact = OptionExactMatch.new tag, name
|
18
|
+
@negative = negate && OptionNegativeMatch.new(negate)
|
19
|
+
@regexp = regexp && OptionRegexpMatch.new(regexp)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'logue/loggable'
|
6
|
+
require 'synoption/option'
|
7
|
+
require 'synoption/exception'
|
8
|
+
require 'synoption/list'
|
9
|
+
|
10
|
+
module Synoption
|
11
|
+
class OptionSet < OptionList
|
12
|
+
include Logue::Loggable
|
13
|
+
|
14
|
+
# maps from the option set class to the valid options for that class.
|
15
|
+
@@options_for_class = Hash.new { |h, k| h[k] = Array.new }
|
16
|
+
|
17
|
+
attr_reader :unprocessed
|
18
|
+
|
19
|
+
def self.has_option name, optcls, optargs = Hash.new
|
20
|
+
@@options_for_class[self] << { :name => name, :class => optcls, :args => optargs }
|
21
|
+
|
22
|
+
define_method name do
|
23
|
+
instance_eval do
|
24
|
+
opt = instance_variable_get '@' + name.to_s
|
25
|
+
opt.value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_reader :options
|
31
|
+
|
32
|
+
def initialize options = Array.new
|
33
|
+
super
|
34
|
+
|
35
|
+
cls = self.class
|
36
|
+
while cls != OptionSet
|
37
|
+
add_options_for_class cls
|
38
|
+
cls = cls.superclass
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_options_for_class cls
|
43
|
+
opts = @@options_for_class[cls]
|
44
|
+
|
45
|
+
opts.each do |option|
|
46
|
+
name = option[:name]
|
47
|
+
cls = option[:class]
|
48
|
+
args = option[:args]
|
49
|
+
opt = cls.new(*args)
|
50
|
+
|
51
|
+
add opt
|
52
|
+
instance_variable_set '@' + name.to_s, opt
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def unset key
|
57
|
+
opt = find_by_name key
|
58
|
+
opt && opt.unset
|
59
|
+
end
|
60
|
+
|
61
|
+
def process args
|
62
|
+
options_processed = Array.new
|
63
|
+
|
64
|
+
@unprocessed = args
|
65
|
+
|
66
|
+
aborted = false
|
67
|
+
|
68
|
+
while !@unprocessed.empty?
|
69
|
+
if @unprocessed[0] == '--'
|
70
|
+
@unprocessed.delete_at 0
|
71
|
+
aborted = true
|
72
|
+
break
|
73
|
+
end
|
74
|
+
|
75
|
+
processed = false
|
76
|
+
|
77
|
+
options.each do |opt|
|
78
|
+
if opt.process @unprocessed
|
79
|
+
processed = true
|
80
|
+
options_processed << opt
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
break unless processed
|
85
|
+
end
|
86
|
+
|
87
|
+
unless aborted
|
88
|
+
check_for_valid_options
|
89
|
+
end
|
90
|
+
|
91
|
+
post_process_all options_processed
|
92
|
+
end
|
93
|
+
|
94
|
+
def check_for_valid_options
|
95
|
+
@unprocessed.each do |opt|
|
96
|
+
if opt.start_with? '-'
|
97
|
+
raise OptionException.new "error: option: #{opt} invalid for #{name}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def post_process_all options_processed
|
103
|
+
options_processed.each do |opt|
|
104
|
+
opt.post_process self, @unprocessed
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'tc'
|
5
|
+
require 'synoption/base_option'
|
6
|
+
require 'stringio'
|
7
|
+
|
8
|
+
module Synoption
|
9
|
+
class BaseOptionTestCase < TestCase
|
10
|
+
def test_init_minimal
|
11
|
+
opt = BaseOption.new :limit, '-l', "the number of log entries", nil
|
12
|
+
assert_equal :limit, opt.name
|
13
|
+
assert_equal '-l', opt.tag
|
14
|
+
assert_equal "the number of log entries", opt.description
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_init_default_value
|
18
|
+
opt = BaseOption.new :nombre, '-x', " one two three", 133
|
19
|
+
assert_equal 133, opt.value
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_init_negate
|
23
|
+
opt = BaseOption.new :limit, '-l', "the number of log entries", nil, :negate => [ %r{^--no-?limit} ]
|
24
|
+
assert_equal [ %r{^--no-?limit} ], opt.negate
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_to_doc
|
28
|
+
opt = BaseOption.new :limit, '-l', "the number of log entries", 777, :negate => [ %r{^--no-?limit} ]
|
29
|
+
sio = StringIO.new
|
30
|
+
opt.to_doc sio
|
31
|
+
exp = String.new
|
32
|
+
exp << " -l [--limit] ARG : the number of log entries\n"
|
33
|
+
exp << " default: 777\n"
|
34
|
+
exp << " --no-limit \n"
|
35
|
+
assert_equal exp, sio.string
|
36
|
+
end
|
37
|
+
|
38
|
+
def assert_exact_match exp, opt, val
|
39
|
+
assert_equal exp, opt.exact_match?(val), "value: '" + val + "'"
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_exact_match
|
43
|
+
opt = BaseOption.new :limit, '-l', "the number of log entries", 3
|
44
|
+
[ '-l', '--limit' ].each do |val|
|
45
|
+
assert_exact_match true, opt, val
|
46
|
+
end
|
47
|
+
|
48
|
+
[ '-L', '-x', '--lim', '--liMit' ].each do |val|
|
49
|
+
assert_exact_match false, opt, val
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def assert_negative_match exp, opt, val
|
54
|
+
md = opt.negative_match? val
|
55
|
+
assert_equal exp, !!md, "value: '" + val + "'"
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_negative_match
|
59
|
+
opt = BaseOption.new :limit, '-l', "the number of log entries", 777, :negate => [ '-L', %r{^--no-?limit} ]
|
60
|
+
[ '-L', '--no-limit', '--nolimit' ].each do |val|
|
61
|
+
assert_negative_match true, opt, val
|
62
|
+
end
|
63
|
+
|
64
|
+
[ '-l', '-x', '-nolimit', ' --nolimit' ].each do |val|
|
65
|
+
assert_negative_match false, opt, val
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def assert_regexp_match exp, opt, val
|
70
|
+
md = opt.regexp_match? val
|
71
|
+
assert_equal exp, !!md, "value: '" + val + "'"
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_regexp_match
|
75
|
+
opt = BaseOption.new :revision, '-r', "the revision", nil, :regexp => Regexp.new('^[\-\+]\d+$')
|
76
|
+
[ '-1', '-123', '+99', '+443' ].each do |val|
|
77
|
+
assert_regexp_match true, opt, val
|
78
|
+
end
|
79
|
+
|
80
|
+
[ '-x', '123', '+-x', 'word' ].each do |val|
|
81
|
+
assert_regexp_match false, opt, val
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def assert_to_command_line exp, opt
|
86
|
+
assert_equal exp, opt.to_command_line
|
87
|
+
end
|
88
|
+
|
89
|
+
def make_xyz_option options = Hash.new
|
90
|
+
BaseOption.new :xyz, '-x', "the blah blah blah", nil, options
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_to_command_line_no_cmdline_option
|
94
|
+
opt = make_xyz_option
|
95
|
+
assert_to_command_line nil, opt
|
96
|
+
opt.set_value 1
|
97
|
+
assert_to_command_line [ '-x', 1 ], opt
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_to_command_line_cmdline_option_string
|
101
|
+
opt = make_xyz_option :as_cmdline_option => '--xray'
|
102
|
+
assert_to_command_line nil, opt
|
103
|
+
opt.set_value 1
|
104
|
+
assert_to_command_line '--xray', opt
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_to_command_line_cmdline_option_nil
|
108
|
+
opt = make_xyz_option :as_cmdline_option => nil
|
109
|
+
assert_to_command_line nil, opt
|
110
|
+
opt.set_value 1
|
111
|
+
assert_to_command_line nil, opt
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_takes_value
|
115
|
+
opt = make_xyz_option
|
116
|
+
assert opt.takes_value?
|
117
|
+
end
|
118
|
+
|
119
|
+
def assert_process exp_process, exp_value, exp_remaining_args, opt, args
|
120
|
+
pr = opt.process args
|
121
|
+
assert_equal exp_process, pr
|
122
|
+
assert_equal exp_value, opt.value
|
123
|
+
assert_equal exp_remaining_args, args
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_process_exact_no_match
|
127
|
+
opt = make_xyz_option
|
128
|
+
args = %w{ --baz foo }
|
129
|
+
assert_process false, nil, %w{ --baz foo }, opt, args
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_process_exact_takes_argument
|
133
|
+
opt = make_xyz_option
|
134
|
+
args = %w{ --xyz foo }
|
135
|
+
assert_process true, 'foo', %w{ }, opt, args
|
136
|
+
|
137
|
+
opt = make_xyz_option
|
138
|
+
args = %w{ --xyz foo bar }
|
139
|
+
assert_process true, 'foo', %w{ bar }, opt, args
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_process_exact_takes_missing_argument
|
143
|
+
opt = make_xyz_option
|
144
|
+
args = %w{ --xyz }
|
145
|
+
assert_raises(RuntimeError) do
|
146
|
+
assert_process true, 'foo', %w{ }, opt, args
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_process_negative
|
151
|
+
options = { :negate => [ '-X', %r{^--no-?xyz} ] }
|
152
|
+
%w{ -X --no-xyz --noxyz }.each do |arg|
|
153
|
+
opt = make_xyz_option options
|
154
|
+
args = [ arg ]
|
155
|
+
assert_process true, false, %w{ }, opt, args
|
156
|
+
end
|
157
|
+
|
158
|
+
%w{ -X --no-xyz --noxyz }.each do |arg|
|
159
|
+
opt = make_xyz_option options
|
160
|
+
args = [ arg, '--abc' ]
|
161
|
+
assert_process true, false, %w{ --abc }, opt, args
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_process_regexp
|
166
|
+
options = { :regexp => Regexp.new('^[\-\+]\d+$') }
|
167
|
+
%w{ -1 +123 }.each do |arg|
|
168
|
+
opt = make_xyz_option options
|
169
|
+
args = [ arg ]
|
170
|
+
assert_process true, arg, %w{ }, opt, args
|
171
|
+
end
|
172
|
+
|
173
|
+
%w{ -1 +123 }.each do |arg|
|
174
|
+
opt = make_xyz_option options
|
175
|
+
args = [ arg, '--foo' ]
|
176
|
+
assert_process true, arg, %w{ --foo }, opt, args
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'tc'
|
5
|
+
require 'synoption/match'
|
6
|
+
|
7
|
+
module Synoption
|
8
|
+
class MatchTestCase < TestCase
|
9
|
+
def assert_match exp, matcher, arg
|
10
|
+
assert_equal exp, matcher.match?(arg), "arg: #{arg}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def assert_matches exp, matcher, *args
|
14
|
+
args.each do |arg|
|
15
|
+
assert_match exp, matcher, arg
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_exact
|
20
|
+
matcher = OptionExactMatch.new '-t', 'tagname'
|
21
|
+
assert_matches true, matcher, '-t', '--tagname'
|
22
|
+
assert_matches false, matcher, '-T', '--tag-name', '--no-tagname', '--notagname'
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_negative_string
|
26
|
+
matcher = OptionNegativeMatch.new '-T'
|
27
|
+
assert_matches true, matcher, '-T'
|
28
|
+
assert_matches false, matcher, '-t'
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_negative_regexp
|
32
|
+
matcher = OptionNegativeMatch.new %r{^\-\-no\-?tagname$}
|
33
|
+
assert_matches true, matcher, '--no-tagname', '--notagname'
|
34
|
+
assert_matches false, matcher, '-t', '--non-tagname' '--nontagname'
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_negative_multiple
|
38
|
+
matcher = OptionNegativeMatch.new %r{^\-\-no\-?tagname$}, '-T'
|
39
|
+
assert_matches true, matcher, '-T', '--no-tagname', '--notagname'
|
40
|
+
assert_matches false, matcher, '-t', '--tagname'
|
41
|
+
end
|
42
|
+
|
43
|
+
def assert_match_not_nil matcher, arg
|
44
|
+
assert_not_nil matcher.match?(arg), "arg: #{arg}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def assert_matches_not_nil matcher, *args
|
48
|
+
args.each do |arg|
|
49
|
+
assert_match_not_nil matcher, arg
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_regexp_match
|
54
|
+
matcher = OptionRegexpMatch.new %r{^--tag-?name$}
|
55
|
+
assert_matches_not_nil matcher, '--tagname', '--tag-name'
|
56
|
+
assert_matches nil, matcher, '--tagnames' '--tag--name'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'tc'
|
5
|
+
require 'synoption/set'
|
6
|
+
require 'synoption/base_option'
|
7
|
+
|
8
|
+
module Synoption
|
9
|
+
class SetTestCase < TestCase
|
10
|
+
class TestOptionSet < OptionSet
|
11
|
+
def name
|
12
|
+
'test'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def setup
|
17
|
+
@xyz = BaseOption.new :xyz, '-x', "blah blah xyz", nil
|
18
|
+
@abc = BaseOption.new :abc, '-a', "abc yadda yadda", nil
|
19
|
+
@tnt = BaseOption.new :tnt, '-t', "tnt and so forth", nil
|
20
|
+
|
21
|
+
@optset = TestOptionSet.new [ @xyz, @abc, @tnt ]
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_find_by_name
|
25
|
+
assert_not_nil @optset.find_by_name(:xyz)
|
26
|
+
assert_nil @optset.find_by_name(:bfd)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_process_set
|
30
|
+
@optset.process %w{ -x foo }
|
31
|
+
|
32
|
+
assert_equal 'foo', @xyz.value
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_process_not_set
|
36
|
+
@optset.process %w{ -x foo }
|
37
|
+
|
38
|
+
assert_nil @abc.value
|
39
|
+
assert_nil @tnt.value
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_bad_option
|
43
|
+
assert_raises(OptionException) do
|
44
|
+
@optset.process %w{ -y foo }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_stop_on_double_dash
|
49
|
+
@optset.process %w{ -- -x foo }
|
50
|
+
|
51
|
+
assert_nil @xyz.value
|
52
|
+
assert_nil @abc.value
|
53
|
+
assert_nil @tnt.value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: synoption
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Pace
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: logue
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
27
|
+
description: Another implementation of an option processor.
|
28
|
+
email: jeugenepace@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/synoption/base_option.rb
|
34
|
+
- lib/synoption/boolean_option.rb
|
35
|
+
- lib/synoption/doc.rb
|
36
|
+
- lib/synoption/exception.rb
|
37
|
+
- lib/synoption/fixnum_option.rb
|
38
|
+
- lib/synoption/list.rb
|
39
|
+
- lib/synoption/match.rb
|
40
|
+
- lib/synoption/matchers.rb
|
41
|
+
- lib/synoption/option.rb
|
42
|
+
- lib/synoption/set.rb
|
43
|
+
- test/synoption/base_option_test.rb
|
44
|
+
- test/synoption/match_test.rb
|
45
|
+
- test/synoption/option_test.rb
|
46
|
+
- test/synoption/set_test.rb
|
47
|
+
homepage: http://www.incava.org/projects/synoption
|
48
|
+
licenses: []
|
49
|
+
metadata: {}
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 2.0.6
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Yet another getopt-like library.
|
70
|
+
test_files:
|
71
|
+
- test/synoption/base_option_test.rb
|
72
|
+
- test/synoption/match_test.rb
|
73
|
+
- test/synoption/option_test.rb
|
74
|
+
- test/synoption/set_test.rb
|