args_parser 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/lib/args_parser/parser.rb +14 -17
- data/lib/args_parser/styles/default.rb +7 -6
- data/lib/args_parser/styles/equal.rb +8 -6
- data/lib/args_parser.rb +1 -1
- data/test/test_args_parser.rb +5 -1
- data/test/test_args_parser_style_equal.rb +5 -1
- metadata +2 -2
data/History.txt
CHANGED
data/lib/args_parser/parser.rb
CHANGED
@@ -10,12 +10,20 @@ module ArgsParser
|
|
10
10
|
end
|
11
11
|
|
12
12
|
class Parser
|
13
|
-
attr_reader :
|
13
|
+
attr_reader :argv, :params, :aliases
|
14
14
|
|
15
|
-
|
16
|
-
def
|
17
|
-
|
18
|
-
|
15
|
+
public
|
16
|
+
def first
|
17
|
+
argv.first
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(config, &block)
|
21
|
+
unless block_given?
|
22
|
+
raise ArgumentError, 'initialize block was not given'
|
23
|
+
end
|
24
|
+
@config = config
|
25
|
+
@argv = []
|
26
|
+
@params = Hash.new{|h,k|
|
19
27
|
h[k] = {
|
20
28
|
:default => nil,
|
21
29
|
:description => nil,
|
@@ -24,18 +32,7 @@ module ArgsParser
|
|
24
32
|
:index => -1
|
25
33
|
}
|
26
34
|
}
|
27
|
-
|
28
|
-
|
29
|
-
def aliases
|
30
|
-
@aliases ||= Hash.new
|
31
|
-
end
|
32
|
-
|
33
|
-
public
|
34
|
-
def initialize(config, &block)
|
35
|
-
@config = config
|
36
|
-
unless block_given?
|
37
|
-
raise ArgumentError, 'initialize block was not given'
|
38
|
-
end
|
35
|
+
@aliases = {}
|
39
36
|
@filter = Filter.new
|
40
37
|
@validator = Validator.new
|
41
38
|
instance_eval(&block)
|
@@ -4,18 +4,19 @@ module ArgsParser
|
|
4
4
|
|
5
5
|
def parse_style_default(argv)
|
6
6
|
k = nil
|
7
|
+
is_key = /^-+([^-\s]+)$/
|
7
8
|
argv.each_with_index do |arg, index|
|
8
9
|
unless k
|
9
|
-
if arg =~
|
10
|
-
k = arg.scan(
|
10
|
+
if arg =~ is_key
|
11
|
+
k = arg.scan(is_key)[0][0].strip.to_sym
|
11
12
|
k = aliases[k] if aliases[k]
|
12
|
-
|
13
|
-
|
13
|
+
else
|
14
|
+
self.argv.push arg
|
14
15
|
end
|
15
16
|
else
|
16
|
-
if arg =~
|
17
|
+
if arg =~ is_key
|
17
18
|
params[k][:value] = true
|
18
|
-
k = arg.scan(
|
19
|
+
k = arg.scan(is_key)[0][0].strip.to_sym
|
19
20
|
k = aliases[k] if aliases[k]
|
20
21
|
else
|
21
22
|
params[k][:value] = arg
|
@@ -3,13 +3,15 @@ module ArgsParser
|
|
3
3
|
class Parser
|
4
4
|
|
5
5
|
def parse_style_equal(argv)
|
6
|
+
is_option = /^-+([^-=\s]+)$/
|
7
|
+
is_param = /^-+([^-=\s]+)=([^=\s]+)$/
|
6
8
|
argv.each_with_index do |arg, i|
|
7
|
-
if arg =~
|
8
|
-
k,v = [arg.scan(
|
9
|
-
elsif arg =~
|
10
|
-
k,v = arg.scan(
|
11
|
-
|
12
|
-
|
9
|
+
if arg =~ is_option
|
10
|
+
k,v = [arg.scan(is_option)[0][0], true]
|
11
|
+
elsif arg =~ is_param
|
12
|
+
k,v = arg.scan(is_param)[0]
|
13
|
+
else
|
14
|
+
self.argv.push arg
|
13
15
|
end
|
14
16
|
if k and v
|
15
17
|
k = k.strip.to_sym
|
data/lib/args_parser.rb
CHANGED
data/test/test_args_parser.rb
CHANGED
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
2
2
|
|
3
3
|
class TestArgsParser < Test::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
@argv = 'test --input http://shokai.org -a --o ./out -h --depth 030 --pi 3.14 --n ShoKaI'.split(/\s+/)
|
5
|
+
@argv = 'test --input http://shokai.org -a --o ./out -h --depth 030 foo bar --pi 3.14 --n ShoKaI'.split(/\s+/)
|
6
6
|
@parser = ArgsParser.parse @argv do
|
7
7
|
arg :input, 'input', :alias => :i
|
8
8
|
arg :output, 'output dir', :alias => :o
|
@@ -23,6 +23,10 @@ class TestArgsParser < Test::Unit::TestCase
|
|
23
23
|
assert @parser.first == 'test'
|
24
24
|
end
|
25
25
|
|
26
|
+
def test_argv
|
27
|
+
assert @parser.argv == ['test', 'foo', 'bar']
|
28
|
+
end
|
29
|
+
|
26
30
|
def test_arg
|
27
31
|
assert @parser[:input] == 'http://shokai.org'
|
28
32
|
end
|
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
2
2
|
|
3
3
|
class TestArgsParserStyleEqual < Test::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
@argv = 'test --input=http://shokai.org --a --o=./out -h --depth=030 --pi=3.14 --n=ShoKaI'.split(/\s+/)
|
5
|
+
@argv = 'test --input=http://shokai.org --a --o=./out -h --depth=030 foo bar --pi=3.14 --n=ShoKaI'.split(/\s+/)
|
6
6
|
@parser = ArgsParser.parse @argv, :style => :equal do
|
7
7
|
arg :input, 'input', :alias => :i
|
8
8
|
arg :output, 'output dir', :alias => :o
|
@@ -23,6 +23,10 @@ class TestArgsParserStyleEqual < Test::Unit::TestCase
|
|
23
23
|
assert @parser.first == 'test'
|
24
24
|
end
|
25
25
|
|
26
|
+
def test_argv
|
27
|
+
assert @parser.argv == ['test', 'foo', 'bar']
|
28
|
+
end
|
29
|
+
|
26
30
|
def test_arg
|
27
31
|
assert @parser[:input] == 'http://shokai.org'
|
28
32
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: args_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -107,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
107
|
version: '0'
|
108
108
|
segments:
|
109
109
|
- 0
|
110
|
-
hash:
|
110
|
+
hash: -3917309846175452703
|
111
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
112
|
none: false
|
113
113
|
requirements:
|