args_parser 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +2 -0
- data/README.rdoc +9 -1
- data/lib/args_parser.rb +3 -1
- data/lib/args_parser/filter.rb +22 -0
- data/lib/args_parser/parser.rb +30 -5
- data/lib/args_parser/validator.rb +22 -0
- data/samples/download_webpage.rb +9 -1
- data/test/test_args_parser.rb +18 -4
- metadata +88 -99
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
data/README.rdoc
CHANGED
@@ -8,7 +8,7 @@ ARGV parser - Parse args from command line.
|
|
8
8
|
|
9
9
|
== SYNOPSIS:
|
10
10
|
|
11
|
-
% ruby
|
11
|
+
% ruby samples/download_webpage.rb -url http://example.com -o out.html
|
12
12
|
|
13
13
|
parse ARGV
|
14
14
|
|
@@ -20,6 +20,14 @@ parse ARGV
|
|
20
20
|
arg :output, 'output file', :alias => :o, :default => 'out.html'
|
21
21
|
arg :verbose, 'verbose mode'
|
22
22
|
arg :help, 'show help', :alias => :h
|
23
|
+
|
24
|
+
filter :url do |v|
|
25
|
+
v.strip
|
26
|
+
end
|
27
|
+
|
28
|
+
validate :url, "invalid URL" do |v|
|
29
|
+
v =~ /^https?:\/\/.+$/
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
33
|
if parser.has_option? :help or !parser.has_param?(:url, :output)
|
data/lib/args_parser.rb
CHANGED
@@ -2,7 +2,9 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
4
|
require 'args_parser/parser'
|
5
|
+
require 'args_parser/filter'
|
6
|
+
require 'args_parser/validator'
|
5
7
|
|
6
8
|
module ArgsParser
|
7
|
-
VERSION = '0.0.
|
9
|
+
VERSION = '0.0.5'
|
8
10
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
module ArgsParser
|
3
|
+
class Filter
|
4
|
+
def filters
|
5
|
+
@filters ||= []
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(name, filter)
|
9
|
+
name = name.to_sym if name
|
10
|
+
filters.push({:name => name, :filter => filter})
|
11
|
+
end
|
12
|
+
|
13
|
+
def filter(name, value)
|
14
|
+
filters.each do |f|
|
15
|
+
if !f[:name] or f[:name] == name
|
16
|
+
value = f[:filter].call(value)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/args_parser/parser.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
|
2
2
|
module ArgsParser
|
3
3
|
def self.parse(argv=[], &block)
|
4
|
-
Parser.new(
|
4
|
+
parser = Parser.new(&block)
|
5
|
+
parser.parse argv
|
6
|
+
parser
|
5
7
|
end
|
6
8
|
|
7
9
|
class Parser
|
@@ -26,12 +28,19 @@ module ArgsParser
|
|
26
28
|
end
|
27
29
|
|
28
30
|
public
|
29
|
-
def initialize(
|
31
|
+
def initialize(&block)
|
30
32
|
unless block_given?
|
31
33
|
raise ArgumentError, 'initialize block was not given'
|
32
34
|
end
|
35
|
+
@filter = Filter.new
|
36
|
+
@validator = Validator.new
|
33
37
|
instance_eval(&block)
|
34
|
-
|
38
|
+
filter do |v|
|
39
|
+
(v.kind_of? String and v =~ /^\d+$/) ? v.to_i : v
|
40
|
+
end
|
41
|
+
filter do |v|
|
42
|
+
(v.kind_of? String and v =~ /^\d+\.\d+$/) ? v.to_f : v
|
43
|
+
end
|
35
44
|
end
|
36
45
|
|
37
46
|
def arg(name, description, opts={})
|
@@ -42,6 +51,18 @@ module ArgsParser
|
|
42
51
|
aliases[opts[:alias]] = name if opts[:alias]
|
43
52
|
end
|
44
53
|
|
54
|
+
def filter(name=nil, &block)
|
55
|
+
if block_given?
|
56
|
+
@filter.add name, block
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def validate(name, message, &block)
|
61
|
+
if block_given?
|
62
|
+
@validator.add name, message, block
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
45
66
|
def args
|
46
67
|
params.keys
|
47
68
|
end
|
@@ -62,8 +83,12 @@ module ArgsParser
|
|
62
83
|
k = arg.scan(/^-+([^-\s]+)$/)[0][0].strip.to_sym
|
63
84
|
k = aliases[k] if aliases[k]
|
64
85
|
else
|
65
|
-
arg =
|
66
|
-
|
86
|
+
arg = @filter.filter k, arg
|
87
|
+
msg = @validator.validate k, arg
|
88
|
+
if msg
|
89
|
+
STDERR.puts "Error: #{msg} (--#{k} #{arg})"
|
90
|
+
exit 1
|
91
|
+
end
|
67
92
|
params[k][:value] = arg
|
68
93
|
k = nil
|
69
94
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
module ArgsParser
|
3
|
+
class Validator
|
4
|
+
def validators
|
5
|
+
@validators ||= []
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(name, message, validator)
|
9
|
+
name = name.to_sym if name
|
10
|
+
validators.push({:name => name, :message => message, :validator => validator})
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate(name, value)
|
14
|
+
validators.each do |f|
|
15
|
+
if !f[:name] or f[:name] == name
|
16
|
+
return f[:message] unless f[:validator].call(value)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/samples/download_webpage.rb
CHANGED
@@ -1,13 +1,21 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
$:.unshift File.expand_path '../lib', File.dirname(__FILE__)
|
2
3
|
require 'rubygems'
|
3
4
|
require 'args_parser'
|
4
|
-
## require File.dirname(__FILE__)+'/../lib/args_parser'
|
5
5
|
|
6
6
|
parser = ArgsParser.parse ARGV do
|
7
7
|
arg :url, 'URL', :alias => :u
|
8
8
|
arg :output, 'output file', :alias => :o, :default => 'out.html'
|
9
9
|
arg :verbose, 'verbose mode'
|
10
10
|
arg :help, 'show help', :alias => :h
|
11
|
+
|
12
|
+
filter :url do |v|
|
13
|
+
v.strip
|
14
|
+
end
|
15
|
+
|
16
|
+
validate :url, "invalid URL" do |v|
|
17
|
+
v =~ /^https?:\/\/.+$/
|
18
|
+
end
|
11
19
|
end
|
12
20
|
|
13
21
|
if parser.has_option? :help or !parser.has_param?(:url, :output)
|
data/test/test_args_parser.rb
CHANGED
@@ -2,11 +2,20 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
2
2
|
|
3
3
|
class TestArgsParser < Test::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
@argv = 'test --input
|
5
|
+
@argv = 'test --input http://shokai.org -a --o ./out -h --depth 030 --pi 3.14 --n ShoKaI'.split(/\s+/)
|
6
6
|
@parser = ArgsParser.parse @argv do
|
7
|
-
arg :input, 'input
|
7
|
+
arg :input, 'input', :alias => :i
|
8
8
|
arg :output, 'output dir', :alias => :o
|
9
|
+
arg :name, 'user name', :alias => :n
|
9
10
|
arg :help, 'show help', :alias => :h
|
11
|
+
|
12
|
+
filter :name do |v|
|
13
|
+
v.downcase
|
14
|
+
end
|
15
|
+
|
16
|
+
validate :input, "input must be valid URL" do |v|
|
17
|
+
v =~ /^https?:\/\/.+/
|
18
|
+
end
|
10
19
|
end
|
11
20
|
end
|
12
21
|
|
@@ -15,7 +24,7 @@ class TestArgsParser < Test::Unit::TestCase
|
|
15
24
|
end
|
16
25
|
|
17
26
|
def test_arg
|
18
|
-
assert @parser[:input] == '
|
27
|
+
assert @parser[:input] == 'http://shokai.org'
|
19
28
|
end
|
20
29
|
|
21
30
|
def test_alias
|
@@ -32,6 +41,10 @@ class TestArgsParser < Test::Unit::TestCase
|
|
32
41
|
assert @parser[:pi].class == Float
|
33
42
|
end
|
34
43
|
|
44
|
+
def test_filter
|
45
|
+
assert @parser[:name] == 'shokai'
|
46
|
+
end
|
47
|
+
|
35
48
|
def test_missing_arg
|
36
49
|
assert @parser[:b] != true
|
37
50
|
end
|
@@ -45,10 +58,11 @@ class TestArgsParser < Test::Unit::TestCase
|
|
45
58
|
assert @parser.has_param? :output
|
46
59
|
assert @parser.has_param? :depth
|
47
60
|
assert @parser.has_param? :pi
|
61
|
+
assert @parser.has_param? :name
|
48
62
|
end
|
49
63
|
|
50
64
|
def test_has_params?
|
51
|
-
assert @parser.has_param? :input, :output, :depth, :pi
|
65
|
+
assert @parser.has_param? :input, :output, :depth, :pi, :name
|
52
66
|
end
|
53
67
|
|
54
68
|
def test_has_not_param?
|
metadata
CHANGED
@@ -1,118 +1,107 @@
|
|
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.4
|
3
|
+
version: !ruby/object:Gem::Version
|
5
4
|
prerelease:
|
5
|
+
version: 0.0.5
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
- Sho Hashimoto
|
7
|
+
authors:
|
8
|
+
- Sho Hashimoto
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ~>
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '3.1'
|
54
|
-
type: :development
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '3.1'
|
12
|
+
|
13
|
+
date: 2012-09-24 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rdoc
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "3.10"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: newgem
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.5.3
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: hoe
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
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:
|
64
|
-
- hashimoto@shokai.org
|
49
|
+
email:
|
50
|
+
- hashimoto@shokai.org
|
65
51
|
executables: []
|
52
|
+
|
66
53
|
extensions: []
|
67
|
-
|
68
|
-
|
69
|
-
-
|
70
|
-
-
|
71
|
-
|
72
|
-
|
73
|
-
- Gemfile
|
74
|
-
-
|
75
|
-
-
|
76
|
-
-
|
77
|
-
-
|
78
|
-
-
|
79
|
-
- lib/args_parser
|
80
|
-
-
|
81
|
-
-
|
82
|
-
-
|
83
|
-
-
|
84
|
-
-
|
85
|
-
-
|
86
|
-
-
|
54
|
+
|
55
|
+
extra_rdoc_files:
|
56
|
+
- History.txt
|
57
|
+
- Manifest.txt
|
58
|
+
- README.rdoc
|
59
|
+
files:
|
60
|
+
- Gemfile
|
61
|
+
- Gemfile.lock
|
62
|
+
- History.txt
|
63
|
+
- Manifest.txt
|
64
|
+
- README.rdoc
|
65
|
+
- Rakefile
|
66
|
+
- lib/args_parser.rb
|
67
|
+
- lib/args_parser/parser.rb
|
68
|
+
- lib/args_parser/filter.rb
|
69
|
+
- lib/args_parser/validator.rb
|
70
|
+
- samples/download_webpage.rb
|
71
|
+
- script/console
|
72
|
+
- script/destroy
|
73
|
+
- script/generate
|
74
|
+
- test/test_args_parser.rb
|
75
|
+
- test/test_helper.rb
|
76
|
+
- .gemtest
|
87
77
|
homepage: http://github.com/shokai/args_parser
|
88
78
|
licenses: []
|
79
|
+
|
89
80
|
post_install_message:
|
90
|
-
rdoc_options:
|
91
|
-
- --main
|
92
|
-
- README.rdoc
|
93
|
-
require_paths:
|
94
|
-
- lib
|
95
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
rdoc_options:
|
82
|
+
- --main
|
83
|
+
- README.rdoc
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
87
|
none: false
|
97
|
-
requirements:
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
- 0
|
103
|
-
hash: -3362374231966488383
|
104
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
93
|
none: false
|
106
|
-
requirements:
|
107
|
-
|
108
|
-
|
109
|
-
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: "0"
|
110
98
|
requirements: []
|
99
|
+
|
111
100
|
rubyforge_project: args_parser
|
112
|
-
rubygems_version: 1.8.
|
101
|
+
rubygems_version: 1.8.15
|
113
102
|
signing_key:
|
114
103
|
specification_version: 3
|
115
104
|
summary: ARGV parser - Parse args from command line.
|
116
|
-
test_files:
|
117
|
-
- test/test_args_parser.rb
|
118
|
-
- test/test_helper.rb
|
105
|
+
test_files:
|
106
|
+
- test/test_args_parser.rb
|
107
|
+
- test/test_helper.rb
|