clasp-ruby 0.19.1.1 → 0.20.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.
- checksums.yaml +4 -4
- data/examples/flag_and_option_specifications.md +6 -5
- data/examples/flag_and_option_specifications.rb +2 -2
- data/lib/clasp/arguments.rb +8 -1
- data/lib/clasp/cli.rb +39 -24
- data/lib/clasp/specifications.rb +1 -1
- data/lib/clasp/version.rb +1 -1
- data/test/unit/tc_default_value.rb +133 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e7715986763c851922433b19a5b08fd761f700bf9b65dfaf74c338e105d3188
|
4
|
+
data.tar.gz: 998e0fe0365c4ca7b11d2ae29269ea5d84fbf3cb65bab70cbe590072a339250c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea17e88ca07b892f2e20083e63c2973eb8c3d11a66a0d02f338b57dde312e807189178cc603fd1d4965fc53189656734ea024ade3d6229c7f4c438cfa1168de9
|
7
|
+
data.tar.gz: 5ca6e41aa2757ae84a8824737aae0d0baac6654c0c7b026f7c4159d0ad2edc8766643216d1e9448f53f6a2c10f5b9759988d289d803911889c8534e4ac34a0b0
|
@@ -23,14 +23,14 @@ InfoLines = [
|
|
23
23
|
|
24
24
|
'CLASP.Ruby examples',
|
25
25
|
:version,
|
26
|
-
"Illustrates use of CLASP.Ruby's use of flags, options, and
|
26
|
+
"Illustrates use of CLASP.Ruby's use of flags, options, and aliases",
|
27
27
|
'',
|
28
28
|
]
|
29
29
|
|
30
30
|
# Specify specifications, parse, and checking standard flags
|
31
31
|
|
32
32
|
Flag_Debug = CLASP.Flag('--debug', alias: '-d', help: 'runs in Debug mode')
|
33
|
-
Option_Verbosity = CLASP.Option('--verbosity', alias: '-v', help: 'specifies the verbosity', values: [ 'terse', 'quiet', 'silent', 'chatty' ])
|
33
|
+
Option_Verbosity = CLASP.Option('--verbosity', alias: '-v', help: 'specifies the verbosity', values: [ 'terse', 'quiet', 'silent', 'chatty' ], default_value: 'terse')
|
34
34
|
Flag_Chatty = CLASP.Flag('--verbosity=chatty', alias: '-c')
|
35
35
|
|
36
36
|
Specifications = [
|
@@ -47,7 +47,7 @@ args = CLASP::Arguments.new ARGV, Specifications
|
|
47
47
|
|
48
48
|
if args.flags.include?(CLASP::FlagSpecification.Help)
|
49
49
|
|
50
|
-
CLASP.show_usage(Specifications, exit_code: 0, version: ProgramVersion, stream: $stdout, info_lines: InfoLines)
|
50
|
+
CLASP.show_usage(Specifications, exit_code: 0, version: ProgramVersion, stream: $stdout, info_lines: InfoLines, default_indicator: '*default*')
|
51
51
|
end
|
52
52
|
|
53
53
|
if args.flags.include?('--version')
|
@@ -76,7 +76,7 @@ if (unused = args.find_first_unknown())
|
|
76
76
|
|
77
77
|
$stderr.puts "#{args.program_name}: unrecognised flag/option: #{unused}"
|
78
78
|
|
79
|
-
|
79
|
+
exit 1
|
80
80
|
end
|
81
81
|
```
|
82
82
|
|
@@ -129,7 +129,7 @@ flags/options:
|
|
129
129
|
--verbosity=<value>
|
130
130
|
specifies the verbosity
|
131
131
|
where <value> one of:
|
132
|
-
terse
|
132
|
+
terse *default*
|
133
133
|
quiet
|
134
134
|
silent
|
135
135
|
chatty
|
@@ -201,3 +201,4 @@ verbosity is specified as: chatty
|
|
201
201
|
Debug mode is specified
|
202
202
|
```
|
203
203
|
|
204
|
+
|
@@ -21,7 +21,7 @@ InfoLines = [
|
|
21
21
|
# Specify specifications, parse, and checking standard flags
|
22
22
|
|
23
23
|
Flag_Debug = CLASP.Flag('--debug', alias: '-d', help: 'runs in Debug mode')
|
24
|
-
Option_Verbosity = CLASP.Option('--verbosity', alias: '-v', help: 'specifies the verbosity', values: [ 'terse', 'quiet', 'silent', 'chatty' ])
|
24
|
+
Option_Verbosity = CLASP.Option('--verbosity', alias: '-v', help: 'specifies the verbosity', values: [ 'terse', 'quiet', 'silent', 'chatty' ], default_value: 'terse')
|
25
25
|
Flag_Chatty = CLASP.Flag('--verbosity=chatty', alias: '-c')
|
26
26
|
|
27
27
|
Specifications = [
|
@@ -38,7 +38,7 @@ args = CLASP::Arguments.new ARGV, Specifications
|
|
38
38
|
|
39
39
|
if args.flags.include?(CLASP::FlagSpecification.Help)
|
40
40
|
|
41
|
-
CLASP.show_usage(Specifications, exit_code: 0, version: ProgramVersion, stream: $stdout, info_lines: InfoLines)
|
41
|
+
CLASP.show_usage(Specifications, exit_code: 0, version: ProgramVersion, stream: $stdout, info_lines: InfoLines, default_indicator: '*default*')
|
42
42
|
end
|
43
43
|
|
44
44
|
if args.flags.include?('--version')
|
data/lib/clasp/arguments.rb
CHANGED
@@ -140,13 +140,20 @@ class Arguments
|
|
140
140
|
# should not be called directly from application code
|
141
141
|
def initialize(arg, given_index, given_name, resolved_name, argument_spec, given_hyphens, given_label, value, extras) # :nodoc:
|
142
142
|
|
143
|
+
actual_value = value
|
144
|
+
|
145
|
+
if value.nil? && argument_spec
|
146
|
+
|
147
|
+
actual_value = argument_spec.default_value
|
148
|
+
end
|
149
|
+
|
143
150
|
@arg = arg
|
144
151
|
@given_index = given_index
|
145
152
|
@given_name = given_name
|
146
153
|
@argument_specification = argument_spec
|
147
154
|
@given_hyphens = given_hyphens
|
148
155
|
@given_label = given_label
|
149
|
-
@value =
|
156
|
+
@value = actual_value
|
150
157
|
@name = resolved_name || given_name
|
151
158
|
@extras = extras.nil? ? {} : extras
|
152
159
|
end
|
data/lib/clasp/cli.rb
CHANGED
@@ -125,6 +125,7 @@ end # module CLI_helpers_
|
|
125
125
|
# - +:values+ appends this string to USAGE line if specified.
|
126
126
|
# - +:flags_and_options+ inserts a custom string instead of the default string <tt>'[ ... flags and options ... ]'</tt>.
|
127
127
|
# - +:info_lines+ inserts 0+ information lines prior to the usage.
|
128
|
+
# - +:default_indicator+ (String) a string placed after the matching value in the listing of an option's range of values. Defaults to "(default)". If +nil+ default is used. If empty string no indication given
|
128
129
|
def self.show_usage specifications, options={}
|
129
130
|
|
130
131
|
options ||= {}
|
@@ -133,10 +134,10 @@ def self.show_usage specifications, options={}
|
|
133
134
|
raise TypeError, "specifications must be an array or must respond to each, reject and select" unless ::Array === specifications || (specifications.respond_to?(:each) && specifications.respond_to?(:reject) && specifications.respond_to?(:select))
|
134
135
|
|
135
136
|
constants = CLI_helpers_::Constants
|
136
|
-
specifications.each { |
|
137
|
+
specifications.each { |s| raise ::TypeError, "each element in specifications array must be one of the types #{constants::VALID_ALIAS_TYPES_STRING}" unless constants::VALID_ALIAS_TYPES.any? { |c| c === s } }
|
137
138
|
|
138
139
|
alias_dups = {}
|
139
|
-
specifications.each { |
|
140
|
+
specifications.each { |s| s.aliases.each { |aa| warn "WARNING: alias '#{aa}' is already used for specification '#{s}'" if alias_dups.has_key? aa; alias_dups[aa] = s; } }
|
140
141
|
|
141
142
|
suppress_blanks = options[:suppress_blank_lines_between_options] || ENV['SUPPRESS_BLANK_LINES_BETWEEN_OPTIONS']
|
142
143
|
|
@@ -173,28 +174,31 @@ def self.show_usage specifications, options={}
|
|
173
174
|
flags_and_options = options[:flags_and_options] || ' [ ... flags and options ... ]'
|
174
175
|
flags_and_options = " #{flags_and_options}" if !flags_and_options.empty? && ' ' != flags_and_options[0]
|
175
176
|
|
177
|
+
default_indicator = options[:default_indicator] || '(default)'
|
178
|
+
default_indicator = nil if default_indicator.empty?
|
179
|
+
|
176
180
|
# sift the specifications to sort out which are value-option
|
177
181
|
# specifications (VOAs)
|
178
182
|
|
179
183
|
voas = {}
|
180
184
|
|
181
|
-
specifications.select { |
|
185
|
+
specifications.select { |s| s.name =~ /^-+[a-zA-Z0-3_-]+[=:].+/ }.each do |s|
|
182
186
|
|
183
|
-
|
187
|
+
s.name =~ /^(-+[a-zA-Z0-3_-]+)[=:](.+)$/
|
184
188
|
|
185
189
|
voas[$1] = [] unless voas.has_key? $1
|
186
|
-
voas[$1] << [
|
190
|
+
voas[$1] << [ s, $2 ]
|
187
191
|
end
|
188
192
|
|
189
193
|
fas = {}
|
190
194
|
|
191
|
-
specifications.select { |
|
195
|
+
specifications.select { |s| AliasSpecification === s }.each do |s|
|
192
196
|
|
193
|
-
fas[
|
194
|
-
fas[
|
197
|
+
fas[s.name] = [] unless fas.has_key? $1
|
198
|
+
fas[s.name] << s
|
195
199
|
end
|
196
200
|
|
197
|
-
specifications = specifications.reject { |
|
201
|
+
specifications = specifications.reject { |s| s.name =~ /^-+[a-zA-Z0-3_-]+[=:].+/ }
|
198
202
|
|
199
203
|
info_lines.each { |info_line| stream.puts info_line } unless info_lines.empty?
|
200
204
|
|
@@ -205,40 +209,51 @@ def self.show_usage specifications, options={}
|
|
205
209
|
|
206
210
|
stream.puts "flags/options:"
|
207
211
|
stream.puts
|
208
|
-
specifications.each do |
|
212
|
+
specifications.each do |s|
|
209
213
|
|
210
|
-
case
|
214
|
+
case s
|
211
215
|
when AliasSpecification
|
212
216
|
|
213
217
|
next
|
214
218
|
when FlagSpecification
|
215
219
|
|
216
|
-
if fas.has_key?
|
220
|
+
if fas.has_key? s.name
|
217
221
|
|
218
|
-
fas[
|
222
|
+
fas[s.name].each do |fa|
|
219
223
|
|
220
224
|
fa.aliases.each { |al| stream.puts "\t#{al}" }
|
221
225
|
end
|
222
226
|
end
|
223
|
-
|
224
|
-
stream.puts "\t#{
|
225
|
-
stream.puts "\t\t#{
|
227
|
+
s.aliases.each { |al| stream.puts "\t#{al}" }
|
228
|
+
stream.puts "\t#{s.name}"
|
229
|
+
stream.puts "\t\t#{s.help}"
|
226
230
|
when OptionSpecification
|
227
231
|
|
228
|
-
if voas.has_key?
|
232
|
+
if voas.has_key? s.name
|
229
233
|
|
230
|
-
voas[
|
234
|
+
voas[s.name].each do |ar|
|
231
235
|
|
232
236
|
ar[0].aliases.each { |al| stream.puts "\t#{al} #{ar[0].name}" }
|
233
237
|
end
|
234
238
|
end
|
235
|
-
|
236
|
-
stream.puts "\t#{
|
237
|
-
stream.puts "\t\t#{
|
238
|
-
unless
|
239
|
+
s.aliases.each { |al| stream.puts "\t#{al} <value>" }
|
240
|
+
stream.puts "\t#{s.name}=<value>"
|
241
|
+
stream.puts "\t\t#{s.help}"
|
242
|
+
unless s.values_range.empty?
|
243
|
+
|
244
|
+
d = s.default_value
|
239
245
|
|
240
246
|
stream.puts "\t\twhere <value> one of:"
|
241
|
-
|
247
|
+
s.values_range.each do |v|
|
248
|
+
|
249
|
+
if default_indicator && v == d
|
250
|
+
|
251
|
+
stream.puts "\t\t\t#{v}\t#{default_indicator}"
|
252
|
+
else
|
253
|
+
|
254
|
+
stream.puts "\t\t\t#{v}"
|
255
|
+
end
|
256
|
+
end
|
242
257
|
end
|
243
258
|
end
|
244
259
|
stream.puts unless suppress_blanks
|
@@ -276,7 +291,7 @@ def self.show_version specifications, options = {}
|
|
276
291
|
raise TypeError, "specifications must be an array or must respond to each, reject and select" unless ::Array === specifications || (specifications.respond_to?(:each) && specifications.respond_to?(:reject) && specifications.respond_to?(:select))
|
277
292
|
|
278
293
|
constants = CLI_helpers_::Constants
|
279
|
-
specifications.each { |
|
294
|
+
specifications.each { |s| raise ::TypeError, "each element in specifications array must be one of the types #{constants::VALID_ALIAS_TYPES_STRING}" unless constants::VALID_ALIAS_TYPES.any? { |c| c === s } }
|
280
295
|
|
281
296
|
stream = options[:stream] || $stdout
|
282
297
|
|
data/lib/clasp/specifications.rb
CHANGED
@@ -165,7 +165,7 @@ class OptionSpecification
|
|
165
165
|
# - +aliases+ (+Array+) 0 or more strings specifying short-form or option-value aliases
|
166
166
|
# - +help+ (+String+) The help string, which may be +nil+
|
167
167
|
# - +values_range+ (+Array+) 0 or more strings specifying values supported by the option
|
168
|
-
# - +default_value+ (+String+) The default value of the option. May be +nil+
|
168
|
+
# - +default_value+ (+String+) The default value of the option, which will be used in the case where an option is specified without a value. May be +nil+
|
169
169
|
# - +required+ (boolean) Whether the option is required. May be +nil+
|
170
170
|
# - +required_message+ (::String) Message to be used when reporting that a required option is missing. May be +nil+ in which case a message of the form "<option-name> not specified; use --help for usage". If begins with the nul character ("\0"), then is used in the place of the <option-name> and placed into the rest of the standard form message
|
171
171
|
# - +extras+ An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+
|
data/lib/clasp/version.rb
CHANGED
@@ -0,0 +1,133 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
|
4
|
+
|
5
|
+
require 'clasp'
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
|
9
|
+
require 'xqsr3/extensions/test/unit'
|
10
|
+
|
11
|
+
class Test_DefaultValue < Test::Unit::TestCase
|
12
|
+
|
13
|
+
def test_long_form_without_default
|
14
|
+
|
15
|
+
specifications = [
|
16
|
+
|
17
|
+
CLASP.Option('--verbosity', values: [ 'silent', 'terse', 'normal', 'chatty', 'verbose' ])
|
18
|
+
]
|
19
|
+
|
20
|
+
argv = [ '--verbosity' ]
|
21
|
+
args = CLASP::Arguments.new(argv, specifications)
|
22
|
+
|
23
|
+
assert_equal 0, args.flags.size
|
24
|
+
assert_equal 1, args.options.size
|
25
|
+
assert_equal 0, args.values.size
|
26
|
+
|
27
|
+
option0 = args.options[0]
|
28
|
+
|
29
|
+
assert_equal '--verbosity', option0.name
|
30
|
+
assert_nil option0.value
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_long_form_with_default
|
34
|
+
|
35
|
+
specifications = [
|
36
|
+
|
37
|
+
CLASP.Option('--verbosity', values: [ 'silent', 'terse', 'normal', 'chatty', 'verbose' ], default_value: 'normal')
|
38
|
+
]
|
39
|
+
|
40
|
+
argv = [ '--verbosity' ]
|
41
|
+
args = CLASP::Arguments.new(argv, specifications)
|
42
|
+
|
43
|
+
assert_equal 0, args.flags.size
|
44
|
+
assert_equal 1, args.options.size
|
45
|
+
assert_equal 0, args.values.size
|
46
|
+
|
47
|
+
option0 = args.options[0]
|
48
|
+
|
49
|
+
assert_equal '--verbosity', option0.name
|
50
|
+
assert_equal 'normal', option0.value
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_short_form_without_default
|
54
|
+
|
55
|
+
specifications = [
|
56
|
+
|
57
|
+
CLASP.Option('--verbosity', alias: '-v', values: [ 'silent', 'terse', 'normal', 'chatty', 'verbose' ])
|
58
|
+
]
|
59
|
+
|
60
|
+
argv = [ '-v' ]
|
61
|
+
args = CLASP::Arguments.new(argv, specifications)
|
62
|
+
|
63
|
+
assert_equal 0, args.flags.size
|
64
|
+
assert_equal 1, args.options.size
|
65
|
+
assert_equal 0, args.values.size
|
66
|
+
|
67
|
+
option0 = args.options[0]
|
68
|
+
|
69
|
+
assert_equal '--verbosity', option0.name
|
70
|
+
assert_nil option0.value
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_short_form_with_default
|
74
|
+
|
75
|
+
specifications = [
|
76
|
+
|
77
|
+
CLASP.Option('--verbosity', alias: '-v', values: [ 'silent', 'terse', 'normal', 'chatty', 'verbose' ], default_value: 'normal')
|
78
|
+
]
|
79
|
+
|
80
|
+
argv = [ '-v' ]
|
81
|
+
args = CLASP::Arguments.new(argv, specifications)
|
82
|
+
|
83
|
+
assert_equal 0, args.flags.size
|
84
|
+
assert_equal 1, args.options.size
|
85
|
+
assert_equal 0, args.values.size
|
86
|
+
|
87
|
+
option0 = args.options[0]
|
88
|
+
|
89
|
+
assert_equal '--verbosity', option0.name
|
90
|
+
assert_equal 'normal', option0.value
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_short_form_without_default_and_separator
|
94
|
+
|
95
|
+
specifications = [
|
96
|
+
|
97
|
+
CLASP.Option('--verbosity', alias: '-v', values: [ 'silent', 'terse', 'normal', 'chatty', 'verbose' ])
|
98
|
+
]
|
99
|
+
|
100
|
+
argv = [ '-v', '--', 'some-value' ]
|
101
|
+
args = CLASP::Arguments.new(argv, specifications)
|
102
|
+
|
103
|
+
assert_equal 0, args.flags.size
|
104
|
+
assert_equal 1, args.options.size
|
105
|
+
assert_equal 1, args.values.size
|
106
|
+
|
107
|
+
option0 = args.options[0]
|
108
|
+
|
109
|
+
assert_equal '--verbosity', option0.name
|
110
|
+
assert_nil option0.value
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_short_form_with_default_and_separator
|
114
|
+
|
115
|
+
specifications = [
|
116
|
+
|
117
|
+
CLASP.Option('--verbosity', alias: '-v', values: [ 'silent', 'terse', 'normal', 'chatty', 'verbose' ], default_value: 'normal')
|
118
|
+
]
|
119
|
+
|
120
|
+
argv = [ '-v', '--', 'some-value' ]
|
121
|
+
args = CLASP::Arguments.new(argv, specifications)
|
122
|
+
|
123
|
+
assert_equal 0, args.flags.size
|
124
|
+
assert_equal 1, args.options.size
|
125
|
+
assert_equal 1, args.values.size
|
126
|
+
|
127
|
+
option0 = args.options[0]
|
128
|
+
|
129
|
+
assert_equal '--verbosity', option0.name
|
130
|
+
assert_equal 'normal', option0.value
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clasp-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.20.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Wilson
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- test/unit/tc_arguments_3.rb
|
61
61
|
- test/unit/tc_arguments_inspect.rb
|
62
62
|
- test/unit/tc_cli.rb
|
63
|
+
- test/unit/tc_default_value.rb
|
63
64
|
- test/unit/tc_defaults_1.rb
|
64
65
|
- test/unit/tc_examples_Arguments.rb
|
65
66
|
- test/unit/tc_extras.rb
|