optparse-simple 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/optparsesimple.rb +104 -0
- metadata +55 -0
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'rexml/document'
|
2
|
+
include REXML
|
3
|
+
|
4
|
+
class OptParseSimple
|
5
|
+
|
6
|
+
def initialize(filename, args)
|
7
|
+
doc = Document.new(File.open(filename,'r').read)
|
8
|
+
@options = XPath.match(doc.root, 'records/option[switch!=""]')
|
9
|
+
|
10
|
+
switches = @options.map do |option|
|
11
|
+
switch = option.text('switch')
|
12
|
+
switch[0] == '-' ? switch : nil
|
13
|
+
end
|
14
|
+
|
15
|
+
switches.compact!
|
16
|
+
|
17
|
+
# split the argument switches if grouped e.g. -ltr
|
18
|
+
args.map! do |arg|
|
19
|
+
|
20
|
+
if arg[/^\-[a-zA-Z]+$/] and switches.grep(/#{arg}/).empty? then
|
21
|
+
arg[1..-1].scan(/./).map {|x| '-' + x}
|
22
|
+
else
|
23
|
+
arg
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
args.flatten!
|
28
|
+
|
29
|
+
a1 = options_match(@options[0], args).flatten.each_slice(2).map {|x| x if x[0]}.compact
|
30
|
+
options_remaining = XPath.match(doc.root, 'records/option[switch=""]/name/text()')
|
31
|
+
a2 = args.zip(options_remaining).map(&:reverse)
|
32
|
+
if a2.map(&:first).all? then
|
33
|
+
@h = Hash[*(a1+a2).map{|x,y| [x.to_s.to_sym, y]}.flatten]
|
34
|
+
else
|
35
|
+
invalid_option = a2.detect {|x,y| x.nil? }.last
|
36
|
+
raise "invalid option: %s not recognised" % invalid_option
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_h()
|
41
|
+
@h
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def options_match(option, args)
|
47
|
+
|
48
|
+
switch, switch_alias = option.text('switch'), option.text('alias')
|
49
|
+
switch_pattern = switch_alias ? "(%s|%s)" % [switch, switch_alias] : switch
|
50
|
+
switch_matched, arg_index = args.each_with_index.detect {|x,j| x[/^#{switch_pattern}/]}
|
51
|
+
key, value = nil
|
52
|
+
|
53
|
+
if switch_matched then
|
54
|
+
|
55
|
+
value_pattern = option.text('value')
|
56
|
+
|
57
|
+
if value_pattern then
|
58
|
+
|
59
|
+
# check for equal sign
|
60
|
+
value = switch_matched[/\=(#{value_pattern})/,1]
|
61
|
+
|
62
|
+
# check the next arg
|
63
|
+
if value.nil? and args.length > 0 then
|
64
|
+
|
65
|
+
next_arg = args[arg_index + 1]
|
66
|
+
|
67
|
+
# check to make sure it's not the next switch
|
68
|
+
next_option = @options[1] if @options.length > 1
|
69
|
+
|
70
|
+
if next_arg != next_option then
|
71
|
+
# validate using the regex
|
72
|
+
value = next_arg[/#{value_pattern}/]
|
73
|
+
|
74
|
+
if value then
|
75
|
+
args.delete_at(arg_index + 1)
|
76
|
+
else
|
77
|
+
raise option.text('errors/records/error[last()]')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
else
|
82
|
+
args.delete_at(arg_index)
|
83
|
+
end
|
84
|
+
|
85
|
+
else
|
86
|
+
args.delete_at(arg_index)
|
87
|
+
end
|
88
|
+
|
89
|
+
key = option.text('name')
|
90
|
+
|
91
|
+
elsif option.text('mandatory').downcase == 'true' then
|
92
|
+
|
93
|
+
raise option.text('errors/records/error')
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
pair = [key, value]
|
98
|
+
@options.shift
|
99
|
+
next_pair = options_match(@options[0], args) if @options.length > 0
|
100
|
+
|
101
|
+
[pair, next_pair]
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: optparse-simple
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-12 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/optparsesimple.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage:
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: optparse-simple
|
54
|
+
test_files: []
|
55
|
+
|