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 CHANGED
@@ -1,3 +1,11 @@
1
+ === 0.0.7 2012-09-26
2
+
3
+ * add function ArgsParser#argv
4
+
5
+ === 0.0.6 2012-09-26
6
+
7
+ * add equal style parser
8
+
1
9
  === 0.0.5 2012-09-24
2
10
 
3
11
  * add filter and validator
@@ -10,12 +10,20 @@ module ArgsParser
10
10
  end
11
11
 
12
12
  class Parser
13
- attr_reader :first
13
+ attr_reader :argv, :params, :aliases
14
14
 
15
- private
16
- def params
17
- @params ||=
18
- Hash.new{|h,k|
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
- end
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 =~ /^-+[^-\s]+$/
10
- k = arg.scan(/^-+([^-\s]+)$/)[0][0].strip.to_sym
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
- elsif index == 0
13
- @first = arg
13
+ else
14
+ self.argv.push arg
14
15
  end
15
16
  else
16
- if arg =~ /^-+[^-\s]+$/
17
+ if arg =~ is_key
17
18
  params[k][:value] = true
18
- k = arg.scan(/^-+([^-\s]+)$/)[0][0].strip.to_sym
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 =~ /^-+[^-=\s]+$/
8
- k,v = [arg.scan(/^-+([^-=\s]+)$/)[0][0], true]
9
- elsif arg =~ /^-+[^-=\s]+=[^=\s]+$/
10
- k,v = arg.scan(/^-+([^-=\s]+)=([^=\s]+)$/)[0]
11
- elsif i == 0
12
- @first = arg
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
@@ -9,5 +9,5 @@ require 'args_parser/validator'
9
9
  require 'args_parser/config'
10
10
 
11
11
  module ArgsParser
12
- VERSION = '0.0.6'
12
+ VERSION = '0.0.7'
13
13
  end
@@ -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.6
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: 3014351784407155723
110
+ hash: -3917309846175452703
111
111
  required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  none: false
113
113
  requirements: