args_parser 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ === 0.0.8 2012-09-27
2
+
3
+ * validator and filter tested
4
+
1
5
  === 0.0.7 2012-09-26
2
6
 
3
7
  * add function ArgsParser#argv
@@ -6,8 +6,9 @@ README.rdoc
6
6
  Rakefile
7
7
  lib/args_parser.rb
8
8
  lib/args_parser/config.rb
9
- lib/args_parser/parser.rb
9
+ lib/args_parser/error.rb
10
10
  lib/args_parser/filter.rb
11
+ lib/args_parser/parser.rb
11
12
  lib/args_parser/styles/default.rb
12
13
  lib/args_parser/styles/equal.rb
13
14
  lib/args_parser/validator.rb
@@ -18,4 +19,6 @@ script/destroy
18
19
  script/generate
19
20
  test/test_args_parser.rb
20
21
  test/test_args_parser_style_equal.rb
22
+ test/test_filter.rb
21
23
  test/test_helper.rb
24
+ test/test_validator.rb
@@ -22,7 +22,7 @@ parse ARGV
22
22
  arg :help, 'show help', :alias => :h
23
23
 
24
24
  filter :url do |v|
25
- v.strip
25
+ v.to_s.strip
26
26
  end
27
27
 
28
28
  validate :url, "invalid URL" do |v|
@@ -4,10 +4,11 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'args_parser/parser'
5
5
  require 'args_parser/styles/default'
6
6
  require 'args_parser/styles/equal'
7
+ require 'args_parser/error'
7
8
  require 'args_parser/filter'
8
9
  require 'args_parser/validator'
9
10
  require 'args_parser/config'
10
11
 
11
12
  module ArgsParser
12
- VERSION = '0.0.7'
13
+ VERSION = '0.0.8'
13
14
  end
@@ -0,0 +1,4 @@
1
+ module ArgsParser
2
+ class ValidationError < StandardError
3
+ end
4
+ end
@@ -35,13 +35,22 @@ module ArgsParser
35
35
  @aliases = {}
36
36
  @filter = Filter.new
37
37
  @validator = Validator.new
38
- instance_eval(&block)
38
+
39
39
  filter do |v|
40
40
  (v.kind_of? String and v =~ /^\d+$/) ? v.to_i : v
41
41
  end
42
42
  filter do |v|
43
43
  (v.kind_of? String and v =~ /^\d+\.\d+$/) ? v.to_f : v
44
44
  end
45
+ on_filter_error do |err, name, value|
46
+ raise err
47
+ end
48
+ on_validate_error do |err, name, value|
49
+ STDERR.puts "Error: #{err.message} (--#{name} #{value})"
50
+ exit 1
51
+ end
52
+
53
+ instance_eval(&block)
45
54
  end
46
55
 
47
56
  def arg(name, description, opts={})
@@ -58,12 +67,28 @@ module ArgsParser
58
67
  end
59
68
  end
60
69
 
70
+ def on_filter_error(err=nil, name=nil, value=nil, &block)
71
+ if block_given?
72
+ @on_filter_error = block
73
+ else
74
+ @on_filter_error.call(err, name, value) if @on_filter_error
75
+ end
76
+ end
77
+
61
78
  def validate(name, message, &block)
62
79
  if block_given?
63
80
  @validator.add name, message, block
64
81
  end
65
82
  end
66
83
 
84
+ def on_validate_error(err=nil, name=nil, value=nil, &block)
85
+ if block_given?
86
+ @on_validate_error = block
87
+ else
88
+ @on_validate_error.call(err, name, value) if @on_validate_error
89
+ end
90
+ end
91
+
67
92
  def args
68
93
  params.keys
69
94
  end
@@ -72,11 +97,18 @@ module ArgsParser
72
97
  method("parse_style_#{@config[:style]}".to_sym).call(argv)
73
98
  params.each do |name, param|
74
99
  next if [nil, true].include? param[:value]
75
- param[:value] = @filter.filter name, param[:value]
76
- msg = @validator.validate name, param[:value]
100
+ begin
101
+ param[:value] = @filter.filter name, param[:value]
102
+ rescue => e
103
+ on_filter_error e, name, param[:value]
104
+ end
105
+ begin
106
+ msg = @validator.validate name, param[:value]
107
+ rescue => e
108
+ on_validate_error e, name, param[:value]
109
+ end
77
110
  if msg
78
- STDERR.puts "Error: #{msg} (--#{name} #{param[:value]})"
79
- exit 1
111
+ on_validate_error ValidationError.new(msg), name, param[:value]
80
112
  end
81
113
  end
82
114
  end
@@ -3,6 +3,11 @@ module ArgsParser
3
3
  class Parser
4
4
 
5
5
  def parse_style_equal(argv)
6
+ on_validate_error do |err, name, value|
7
+ STDERR.puts "Error: #{err.message} (--#{name}=#{value})"
8
+ exit 1
9
+ end
10
+
6
11
  is_option = /^-+([^-=\s]+)$/
7
12
  is_param = /^-+([^-=\s]+)=([^=\s]+)$/
8
13
  argv.each_with_index do |arg, i|
@@ -12,7 +12,7 @@ parser = ArgsParser.parse ARGV do
12
12
  arg :help, 'show help', :alias => :h
13
13
 
14
14
  filter :url do |v|
15
- v.strip
15
+ v.to_s.strip
16
16
  end
17
17
 
18
18
  validate :url, "invalid URL" do |v|
@@ -15,7 +15,7 @@ parser = ArgsParser.parse ARGV, :style => :equal do
15
15
  arg :help, 'show help', :alias => :h
16
16
 
17
17
  filter :user do |v|
18
- v.strip
18
+ v.to_s.strip
19
19
  end
20
20
 
21
21
  validate :user, "invalid twitter screen_name" do |v|
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestArgsParserFilter < Test::Unit::TestCase
4
+ def setup
5
+ @argv = ['--count', '35']
6
+ @@err = nil
7
+ @@name = nil
8
+ @@value = nil
9
+ @parser = ArgsParser.parse @argv do
10
+ arg :count, 'number'
11
+
12
+ filter :count do |v|
13
+ raise NoMethodError, 'error!!'
14
+ end
15
+
16
+ on_filter_error do |err, name, value|
17
+ @@err = err
18
+ @@name = name
19
+ @@value = value
20
+ end
21
+ end
22
+ end
23
+
24
+ def test_filter_error
25
+ assert @@name == :count
26
+ assert @@value == '35'
27
+ assert @@err.class == NoMethodError
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestArgsParserValidator < Test::Unit::TestCase
4
+ def setup
5
+ @argv = ['--url', 'hptt://shokai.org']
6
+ @@err = nil
7
+ @@name = nil
8
+ @@value = nil
9
+ @parser = ArgsParser.parse @argv do
10
+ arg :url, 'URL'
11
+
12
+ validate :url, 'invalid URL' do |v|
13
+ v =~ Regexp.new("^https?://.+$")
14
+ end
15
+
16
+ on_validate_error do |err, name, value|
17
+ @@err = err
18
+ @@name = name
19
+ @@value = value
20
+ end
21
+ end
22
+ end
23
+
24
+ def test_validate_error
25
+ assert @@name == :url
26
+ assert @@value == 'hptt://shokai.org'
27
+ assert @@err.class == ArgsParser::ValidationError
28
+ end
29
+ end
metadata CHANGED
@@ -1,74 +1,62 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: args_parser
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.7
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 0.0.8
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Sho Hashimoto
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-26 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
12
+
13
+ date: 2012-09-26 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
15
16
  name: rdoc
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '3.10'
22
- type: :development
23
17
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
18
+ requirement: &id001 !ruby/object:Gem::Requirement
25
19
  none: false
26
- requirements:
20
+ requirements:
27
21
  - - ~>
28
- - !ruby/object:Gem::Version
29
- version: '3.10'
30
- - !ruby/object:Gem::Dependency
31
- name: newgem
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: 1.5.3
22
+ - !ruby/object:Gem::Version
23
+ version: "3.10"
38
24
  type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: newgem
39
28
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
29
+ requirement: &id002 !ruby/object:Gem::Requirement
41
30
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
45
34
  version: 1.5.3
46
- - !ruby/object:Gem::Dependency
47
- name: hoe
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: '3.1'
54
35
  type: :development
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: hoe
55
39
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
40
+ requirement: &id003 !ruby/object:Gem::Requirement
57
41
  none: false
58
- requirements:
42
+ requirements:
59
43
  - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '3.1'
44
+ - !ruby/object:Gem::Version
45
+ version: "3.1"
46
+ type: :development
47
+ version_requirements: *id003
62
48
  description: ARGV parser - Parse args from command line.
63
- email:
49
+ email:
64
50
  - hashimoto@shokai.org
65
51
  executables: []
52
+
66
53
  extensions: []
67
- extra_rdoc_files:
54
+
55
+ extra_rdoc_files:
68
56
  - History.txt
69
57
  - Manifest.txt
70
58
  - README.rdoc
71
- files:
59
+ files:
72
60
  - Gemfile
73
61
  - Gemfile.lock
74
62
  - History.txt
@@ -77,8 +65,9 @@ files:
77
65
  - Rakefile
78
66
  - lib/args_parser.rb
79
67
  - lib/args_parser/config.rb
80
- - lib/args_parser/parser.rb
68
+ - lib/args_parser/error.rb
81
69
  - lib/args_parser/filter.rb
70
+ - lib/args_parser/parser.rb
82
71
  - lib/args_parser/styles/default.rb
83
72
  - lib/args_parser/styles/equal.rb
84
73
  - lib/args_parser/validator.rb
@@ -89,38 +78,41 @@ files:
89
78
  - script/generate
90
79
  - test/test_args_parser.rb
91
80
  - test/test_args_parser_style_equal.rb
81
+ - test/test_filter.rb
92
82
  - test/test_helper.rb
83
+ - test/test_validator.rb
93
84
  - .gemtest
94
85
  homepage: http://github.com/shokai/args_parser
95
86
  licenses: []
87
+
96
88
  post_install_message:
97
- rdoc_options:
89
+ rdoc_options:
98
90
  - --main
99
91
  - README.rdoc
100
- require_paths:
92
+ require_paths:
101
93
  - lib
102
- required_ruby_version: !ruby/object:Gem::Requirement
94
+ required_ruby_version: !ruby/object:Gem::Requirement
103
95
  none: false
104
- requirements:
105
- - - ! '>='
106
- - !ruby/object:Gem::Version
107
- version: '0'
108
- segments:
109
- - 0
110
- hash: -3917309846175452703
111
- required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
101
  none: false
113
- requirements:
114
- - - ! '>='
115
- - !ruby/object:Gem::Version
116
- version: '0'
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: "0"
117
106
  requirements: []
107
+
118
108
  rubyforge_project: args_parser
119
- rubygems_version: 1.8.24
109
+ rubygems_version: 1.8.17
120
110
  signing_key:
121
111
  specification_version: 3
122
112
  summary: ARGV parser - Parse args from command line.
123
- test_files:
113
+ test_files:
124
114
  - test/test_args_parser.rb
125
115
  - test/test_args_parser_style_equal.rb
116
+ - test/test_filter.rb
126
117
  - test/test_helper.rb
118
+ - test/test_validator.rb