shellopts 2.0.4 → 2.0.5
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/lib/shellopts/formatter.rb +3 -2
- data/lib/shellopts/version.rb +1 -1
- data/lib/shellopts.rb +25 -3
- data/main +25 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3a8b18b0a610c14ac4a85f80cd70ff5f85ca7dc868b25615a67a3ad71f737b5
|
4
|
+
data.tar.gz: 2b0442868d8ddf102a5f8ee1b3d3307ecbea17a95512f5164d4a64a9d3fa9ec7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c90ab9e0049ba64e21c2b0fd408b438df84c0265fced52b84d221bdb2b96f92e75624c00ab59526c9caa80f67e2e7d55c5bcf0cb8138b803722b424e08ea9202
|
7
|
+
data.tar.gz: 372e29956c8f3e27f8f5617215cf610403d40a307257c0c63605e94bd39fffaa8bf9cb165e82fe61497fcc8f0519c4298c1bf4480fa26735f5bf5c01e12fe8e2
|
data/lib/shellopts/formatter.rb
CHANGED
@@ -38,16 +38,17 @@ module ShellOpts
|
|
38
38
|
using Ext::Array::Wrap
|
39
39
|
|
40
40
|
def puts_usage(bol: false)
|
41
|
+
width = [Formatter.rest, Formatter::USAGE_MAX_WIDTH].min
|
41
42
|
if descrs.size == 0
|
42
43
|
print (lead = Formatter.command_prefix || "")
|
43
44
|
indent(lead.size, ' ', bol: bol && lead == "") {
|
44
|
-
puts render(:multi,
|
45
|
+
puts render(:multi, width)
|
45
46
|
}
|
46
47
|
else
|
47
48
|
lead = Formatter.command_prefix || ""
|
48
49
|
descrs.each { |descr|
|
49
50
|
print lead
|
50
|
-
puts render(:single,
|
51
|
+
puts render(:single, width, args: [descr.text])
|
51
52
|
}
|
52
53
|
end
|
53
54
|
end
|
data/lib/shellopts/version.rb
CHANGED
data/lib/shellopts.rb
CHANGED
@@ -102,6 +102,9 @@ module ShellOpts
|
|
102
102
|
attr_accessor :stdopts
|
103
103
|
attr_accessor :msgopts
|
104
104
|
|
105
|
+
# Version of client program. This is only used if +stdopts+ is true
|
106
|
+
attr_reader :version
|
107
|
+
|
105
108
|
# Interpreter flags
|
106
109
|
attr_accessor :float
|
107
110
|
|
@@ -116,14 +119,17 @@ module ShellOpts
|
|
116
119
|
attr_reader :tokens
|
117
120
|
alias_method :ast, :grammar # Oops - defined earlier FIXME
|
118
121
|
|
119
|
-
def initialize(name: nil, stdopts: true, msgopts: false, float: true, exception: false)
|
122
|
+
def initialize(name: nil, stdopts: true, version: nil, msgopts: false, float: true, exception: false)
|
120
123
|
@name = name || File.basename($PROGRAM_NAME)
|
121
|
-
@stdopts, @msgopts, @float, @exception = stdopts, msgopts, float, exception
|
124
|
+
@stdopts, @version, @msgopts, @float, @exception = stdopts, version, msgopts, float, exception
|
122
125
|
end
|
123
126
|
|
124
127
|
# Compile source and return grammar object. Also sets #spec and #grammar.
|
125
128
|
# Returns the grammar
|
126
129
|
def compile(spec)
|
130
|
+
if stdopts
|
131
|
+
spec += "\n--version\n Write version number and exit\n-h,help @ Write help text and exit\n Write help text. -h prints a brief text, --help prints a longer man-style description of the command"
|
132
|
+
end
|
127
133
|
handle_exceptions {
|
128
134
|
@oneline = spec.index("\n").nil?
|
129
135
|
@spec = spec.sub(/^\s*\n/, "")
|
@@ -143,6 +149,20 @@ module ShellOpts
|
|
143
149
|
handle_exceptions {
|
144
150
|
@argv = argv.dup
|
145
151
|
@program, @args = Interpreter.interpret(grammar, argv, float: float, exception: exception)
|
152
|
+
if stdopts
|
153
|
+
if @program.version?
|
154
|
+
version or raise ArgumentError, "Version not specified"
|
155
|
+
puts version
|
156
|
+
exit
|
157
|
+
elsif @program.help?
|
158
|
+
if @program[:help].name == "-h"
|
159
|
+
ShellOpts.brief
|
160
|
+
else
|
161
|
+
ShellOpts.help
|
162
|
+
end
|
163
|
+
exit
|
164
|
+
end
|
165
|
+
end
|
146
166
|
}
|
147
167
|
self
|
148
168
|
end
|
@@ -203,9 +223,10 @@ module ShellOpts
|
|
203
223
|
# Print help for the given subject or the full documentation if +subject+
|
204
224
|
# is nil. Clears the screen beforehand if :clear is true
|
205
225
|
#
|
206
|
-
def help(subject = nil, clear:
|
226
|
+
def help(subject = nil, clear: true)
|
207
227
|
node = (subject ? @grammar[subject] : @grammar) or
|
208
228
|
raise ArgumentError, "No such command: '#{subject&.sub(".", " ")}'"
|
229
|
+
print '[H[2J' if clear
|
209
230
|
Formatter.help(node)
|
210
231
|
end
|
211
232
|
|
@@ -310,6 +331,7 @@ module ShellOpts
|
|
310
331
|
def self.instance?() !@instance.nil? end
|
311
332
|
def self.instance() @instance or raise Error, "ShellOpts is not initialized" end
|
312
333
|
def self.instance=(instance) @instance = instance end
|
334
|
+
def self.shellopts() instance end
|
313
335
|
|
314
336
|
forward_self_to :instance, :error, :failure
|
315
337
|
|
data/main
CHANGED
@@ -7,18 +7,31 @@ require 'shellopts'
|
|
7
7
|
|
8
8
|
include ShellOpts
|
9
9
|
|
10
|
+
VERSION = "1.2.3"
|
10
11
|
|
11
|
-
SPEC =
|
12
|
-
|
13
|
-
|
12
|
+
SPEC = "-a"
|
13
|
+
opts, args = ShellOpts::process(SPEC, ARGV, version: VERSION)
|
14
|
+
#ShellOpts::ShellOpts.help
|
14
15
|
|
15
|
-
-b,beta=ARG
|
16
|
-
@ Alternative style of brief comment
|
17
16
|
|
18
|
-
Longer and more elaborate description of the --beta option
|
19
|
-
)
|
20
17
|
|
21
|
-
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
__END__
|
22
|
+
|
23
|
+
|
24
|
+
#SPEC = %(
|
25
|
+
# -a,alpha @ Brief comment for -a and --alpha options
|
26
|
+
# Longer and more elaborate description of the --alpha option
|
27
|
+
#
|
28
|
+
# -b,beta=ARG
|
29
|
+
# @ Alternative style of brief comment
|
30
|
+
#
|
31
|
+
# Longer and more elaborate description of the --beta option
|
32
|
+
#)
|
33
|
+
#
|
34
|
+
#opts, args = ShellOpts.process(SPEC, ARGV)
|
22
35
|
#puts "opts.alpha?: #{opts.alpha?.inspect}"
|
23
36
|
#puts "opts.alpha: #{opts.alpha.inspect}"
|
24
37
|
#puts "opts.beta?: #{opts.beta?.inspect}"
|
@@ -266,8 +279,9 @@ SPEC4 = %(
|
|
266
279
|
|
267
280
|
SPEC5 = "cmd! cmd!"
|
268
281
|
|
269
|
-
shellopts = ShellOpts::ShellOpts.new(exception:
|
282
|
+
shellopts = ShellOpts::ShellOpts.new(exception: true)
|
270
283
|
#shellopts.compile("cmd! cmd!")
|
284
|
+
|
271
285
|
shellopts.compile(SPEC)
|
272
286
|
#shellopts.compile(SPEC2)
|
273
287
|
#shellopts.compile(SPEC3)
|
@@ -277,9 +291,9 @@ shellopts.compile(SPEC)
|
|
277
291
|
#shellopts.tokens.each(&:dump)
|
278
292
|
#exit
|
279
293
|
|
280
|
-
shellopts.usage
|
294
|
+
#shellopts.usage
|
281
295
|
#shellopts.brief
|
282
|
-
|
296
|
+
shellopts.help
|
283
297
|
#shellopts.help("cmd")
|
284
298
|
#shellopts.help(ARGV.first)
|
285
299
|
|