shellopts 2.0.4 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0170d9ce5144afcf716b90a3904250c0afa5d769b7a64f39db0b8084e77b4fb7
4
- data.tar.gz: 81495a3802c472c65812594d847c43b0f59849ea0ebe093c74c2fb7d3e822658
3
+ metadata.gz: a3a8b18b0a610c14ac4a85f80cd70ff5f85ca7dc868b25615a67a3ad71f737b5
4
+ data.tar.gz: 2b0442868d8ddf102a5f8ee1b3d3307ecbea17a95512f5164d4a64a9d3fa9ec7
5
5
  SHA512:
6
- metadata.gz: 195801ea89b22920fca88216ed4bd7ac426b3370e482b61547dac085d1c7d9f1bd0a2d0edc7c3368bf126cd5f4da815dd882ee3149cd291549ff631d6302e93a
7
- data.tar.gz: 6dcd50871438db9f5b0520e4f670f6df6ad04756685838d309874a9601770ee79a7370ebc11b0469d19a9f2be89868c7cfd12cfe028c0c01719b4d8d0593fc59
6
+ metadata.gz: c90ab9e0049ba64e21c2b0fd408b438df84c0265fced52b84d221bdb2b96f92e75624c00ab59526c9caa80f67e2e7d55c5bcf0cb8138b803722b424e08ea9202
7
+ data.tar.gz: 372e29956c8f3e27f8f5617215cf610403d40a307257c0c63605e94bd39fffaa8bf9cb165e82fe61497fcc8f0519c4298c1bf4480fa26735f5bf5c01e12fe8e2
@@ -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, Formatter::USAGE_MAX_WIDTH)
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, Formatter::USAGE_MAX_WIDTH, args: [descr.text])
51
+ puts render(:single, width, args: [descr.text])
51
52
  }
52
53
  end
53
54
  end
@@ -1,3 +1,3 @@
1
1
  module ShellOpts
2
- VERSION = "2.0.4"
2
+ VERSION = "2.0.5"
3
3
  end
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: false)
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 '' 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
- -a,alpha @ Brief comment for -a and --alpha options
13
- Longer and more elaborate description of the --alpha option
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
- opts, args = ShellOpts.process(SPEC, ARGV)
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: false)
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
- #shellopts.help
296
+ shellopts.help
283
297
  #shellopts.help("cmd")
284
298
  #shellopts.help(ARGV.first)
285
299
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shellopts
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen