optparse-simple 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/optparse-simple.rb +33 -23
- metadata +33 -11
data/lib/optparse-simple.rb
CHANGED
@@ -3,29 +3,24 @@
|
|
3
3
|
# file: optparse-simple.rb
|
4
4
|
|
5
5
|
require 'rexml/document'
|
6
|
+
require 'polyrex'
|
6
7
|
require 'table-formatter'
|
7
|
-
include REXML
|
8
8
|
|
9
|
-
class OptParseSimple
|
10
9
|
|
10
|
+
class OptParseSimple
|
11
|
+
include REXML
|
12
|
+
|
11
13
|
def initialize(s)
|
12
14
|
super()
|
13
15
|
|
14
|
-
|
15
|
-
buffer = Kernel.open(s, 'UserAgent' => 'Polyrex-Reader').read
|
16
|
-
elsif s[/\</] # xml
|
17
|
-
buffer = s
|
18
|
-
else # local file
|
19
|
-
buffer = File.open(s,'r').read
|
20
|
-
end
|
21
|
-
|
16
|
+
buffer = read(s)
|
22
17
|
@doc = Document.new(buffer)
|
23
18
|
|
24
19
|
end
|
25
20
|
|
26
21
|
def parse(args)
|
27
22
|
|
28
|
-
@options = XPath.match(@doc.root, 'records/
|
23
|
+
@options = XPath.match(@doc.root, 'records/optionx[summary/switch!="n/a"]')
|
29
24
|
|
30
25
|
switches = @options.map do |option|
|
31
26
|
switch = option.text('summary/switch')
|
@@ -47,7 +42,7 @@ class OptParseSimple
|
|
47
42
|
args.flatten!
|
48
43
|
|
49
44
|
# -- bind any loose value to their argument
|
50
|
-
xpath = 'records/
|
45
|
+
xpath = 'records/optionx/summary[switch != "n/a" and value != "n/a"]'
|
51
46
|
options = XPath.match(@doc.root, xpath)\
|
52
47
|
.map {|node| %w(switch alias)\
|
53
48
|
.map{|x| node.text(x)}.compact}\
|
@@ -62,12 +57,12 @@ class OptParseSimple
|
|
62
57
|
|
63
58
|
a1 = []
|
64
59
|
a1 = options_match(@options[0], args).flatten.each_slice(2).map {|x| x if x[0]}.compact unless @options.empty?
|
65
|
-
options_remaining = XPath.match(@doc.root, 'records/
|
66
|
-
mandatory_remaining = XPath.match(@doc.root, 'records/
|
60
|
+
options_remaining = XPath.match(@doc.root, 'records/optionx/summary[switch="n/a"]/name/text()').map(&:to_s)
|
61
|
+
mandatory_remaining = XPath.match(@doc.root, 'records/optionx/summary[switch="n/a" and mandatory="true"]/name/text()').map(&:to_s)
|
67
62
|
if mandatory_remaining.length > args.length then
|
68
63
|
missing_arg = (mandatory_remaining - args).first
|
69
|
-
option = XPath.first(@doc.root, "records/
|
70
|
-
raise option.text('records/
|
64
|
+
option = XPath.first(@doc.root, "records/optionx[summary/name='#{missing_arg}']")
|
65
|
+
raise option.text('records/errorx/summary/msg')
|
71
66
|
end
|
72
67
|
|
73
68
|
a2 = args.zip(options_remaining).map(&:reverse)
|
@@ -85,14 +80,14 @@ class OptParseSimple
|
|
85
80
|
end
|
86
81
|
|
87
82
|
def help
|
88
|
-
a = XPath.match(@doc.root, "records/
|
83
|
+
a = XPath.match(@doc.root, "records/optionx/summary[switch != 'n/a']").map do |summary|
|
89
84
|
%w(switch alias).map {|x| summary.text x}
|
90
85
|
end
|
91
86
|
puts 'options:'
|
92
87
|
puts TableFormatter.new(source: a, border: false).to_s
|
93
88
|
end
|
94
89
|
|
95
|
-
private
|
90
|
+
private
|
96
91
|
|
97
92
|
def options_match(option, args)
|
98
93
|
|
@@ -103,9 +98,9 @@ class OptParseSimple
|
|
103
98
|
|
104
99
|
if switch_matched then
|
105
100
|
|
106
|
-
value_pattern = option.text('summary/value')
|
101
|
+
value_pattern = option.text('summary/value')
|
107
102
|
|
108
|
-
if value_pattern then
|
103
|
+
if value_pattern and value_pattern.downcase != 'n/a' then
|
109
104
|
|
110
105
|
# check for equal sign
|
111
106
|
value = switch_matched[/\=(#{value_pattern})/,1]
|
@@ -125,7 +120,7 @@ class OptParseSimple
|
|
125
120
|
if value then
|
126
121
|
args.delete_at(arg_index + 1)
|
127
122
|
else
|
128
|
-
raise option.text('records/
|
123
|
+
raise option.text('records/errorx[last()]/summary/msg')
|
129
124
|
end
|
130
125
|
end
|
131
126
|
|
@@ -139,9 +134,10 @@ class OptParseSimple
|
|
139
134
|
|
140
135
|
key = option.text('summary/name')
|
141
136
|
|
142
|
-
elsif option.text('summary/mandatory').downcase == 'true' then
|
137
|
+
elsif option.text('summary/mandatory').to_s.downcase == 'true' then
|
143
138
|
|
144
|
-
raise option.text('records/
|
139
|
+
raise option.text('records/errorx/summary/msg')
|
140
|
+
else
|
145
141
|
|
146
142
|
end
|
147
143
|
|
@@ -151,5 +147,19 @@ class OptParseSimple
|
|
151
147
|
|
152
148
|
[pair, next_pair]
|
153
149
|
end
|
150
|
+
|
151
|
+
def read(s)
|
152
|
+
if s[/\s/] then
|
153
|
+
Polyrex.new('options/optionx[name,switch,alias,value,mandatory]/errorx[msg]').parse(s).to_xml
|
154
|
+
elsif s.is_a? Polyrex then
|
155
|
+
s.to_xml
|
156
|
+
elsif s[/^https?:\/\//] then # url
|
157
|
+
Kernel.open(s, 'UserAgent' => 'Polyrex-Reader').read
|
158
|
+
elsif s[/\</] # xml
|
159
|
+
s
|
160
|
+
else # local file
|
161
|
+
File.open(s,'r').read
|
162
|
+
end
|
163
|
+
end
|
154
164
|
|
155
165
|
end
|
metadata
CHANGED
@@ -1,19 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optparse-simple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease:
|
5
|
+
version: 0.4.0
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
-
|
7
|
+
authors:
|
8
|
+
- James Robertson
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date:
|
13
|
+
date: 2011-04-06 00:00:00 +01:00
|
13
14
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: polyrex
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: table-formatter
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
description: OptParse-Simple parses command-line arguments and returns them as a hash.
|
17
39
|
email:
|
18
40
|
executables: []
|
19
41
|
|
@@ -24,7 +46,7 @@ extra_rdoc_files: []
|
|
24
46
|
files:
|
25
47
|
- lib/optparse-simple.rb
|
26
48
|
has_rdoc: true
|
27
|
-
homepage:
|
49
|
+
homepage: https://github.com/jrobertson/optparse-simple
|
28
50
|
licenses: []
|
29
51
|
|
30
52
|
post_install_message:
|
@@ -33,21 +55,21 @@ rdoc_options: []
|
|
33
55
|
require_paths:
|
34
56
|
- lib
|
35
57
|
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
36
59
|
requirements:
|
37
60
|
- - ">="
|
38
61
|
- !ruby/object:Gem::Version
|
39
62
|
version: "0"
|
40
|
-
version:
|
41
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
42
65
|
requirements:
|
43
66
|
- - ">="
|
44
67
|
- !ruby/object:Gem::Version
|
45
68
|
version: "0"
|
46
|
-
version:
|
47
69
|
requirements: []
|
48
70
|
|
49
71
|
rubyforge_project:
|
50
|
-
rubygems_version: 1.
|
72
|
+
rubygems_version: 1.5.2
|
51
73
|
signing_key:
|
52
74
|
specification_version: 3
|
53
75
|
summary: optparse-simple
|