ArgvParser 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/argv_parser.rb +86 -0
- metadata +45 -0
data/lib/argv_parser.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
|
2
|
+
class ArgvParser
|
3
|
+
def initialize
|
4
|
+
@args = Hash.new
|
5
|
+
@aliases = Hash.new
|
6
|
+
@l_short = 0
|
7
|
+
@l_long = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def hook(short, long, type, options)
|
11
|
+
raise ArgumentError.new("short is nil") if not short
|
12
|
+
|
13
|
+
@args[short.downcase] = {
|
14
|
+
:long => long,
|
15
|
+
:type => type,
|
16
|
+
:opts => options,
|
17
|
+
:yield => Proc.new {|v| yield(v) }
|
18
|
+
}
|
19
|
+
@l_short = short.length + 1 if @l_short < short.length + 1
|
20
|
+
@l_long = long.length + 1 if @l_long < long.length + 1
|
21
|
+
end
|
22
|
+
|
23
|
+
def help
|
24
|
+
puts "Help:"
|
25
|
+
@args.each do |k,v|
|
26
|
+
spaces = ""
|
27
|
+
(@l_short-k.length).times { spaces << " " }
|
28
|
+
spaces2 = ""
|
29
|
+
(@l_long-v[:long].length).times { spaces2 << " " }
|
30
|
+
puts "#{k}#{spaces} #{v[:long]}#{spaces2} mandatory: #{v[:opts][:mandatory] == true}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse!(args)
|
35
|
+
begin
|
36
|
+
executor = Array.new
|
37
|
+
i = 0
|
38
|
+
|
39
|
+
@mandatory = Array.new
|
40
|
+
@args.each do |k,v|
|
41
|
+
@mandatory.push k if v[:opts][:mandatory]
|
42
|
+
end
|
43
|
+
|
44
|
+
while i < args.length
|
45
|
+
if v = @args[args[i].downcase]
|
46
|
+
@mandatory.delete args[i].downcase
|
47
|
+
|
48
|
+
n = nil
|
49
|
+
if v[:type]
|
50
|
+
raise ArgumentError.new("argument for #{args[i].downcase} missing") if i+1 >= args.length
|
51
|
+
i += 1
|
52
|
+
case v[:type].to_s
|
53
|
+
when "Array"
|
54
|
+
n = args[i].split ','
|
55
|
+
when "Float"
|
56
|
+
n = args[i].to_f
|
57
|
+
when "Integer"
|
58
|
+
n = args[i].to_i
|
59
|
+
else
|
60
|
+
n = $1 if args[i] =~ /^\"?(.*)\"?$/
|
61
|
+
end
|
62
|
+
end
|
63
|
+
executor.push [v[:yield], n]
|
64
|
+
else
|
65
|
+
raise ArgumentError.new("argument #{args[i].downcase} unknown")
|
66
|
+
end
|
67
|
+
i += 1
|
68
|
+
end
|
69
|
+
|
70
|
+
if not @mandatory.empty?
|
71
|
+
raise ArgumentError.new("mandatory #{@mandatory[0]} is missing")
|
72
|
+
end
|
73
|
+
|
74
|
+
if executor.empty?
|
75
|
+
help
|
76
|
+
else
|
77
|
+
executor.each do |e|
|
78
|
+
e[0].call(e[1])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
rescue Exception => e
|
82
|
+
puts "#{e.class}: #{e.message}\n"
|
83
|
+
help
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ArgvParser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthias Geier
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email:
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/argv_parser.rb
|
21
|
+
homepage: https://github.com/matthias-geier/ArgvParser
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.9.3
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.23
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: A basic parser for command line arguments.
|
45
|
+
test_files: []
|