shellopts 0.9.1 → 0.9.2

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: f97bd1dadffdc1a8e5eab5635bdb186ddb925bafc2fe60b478c84d3b9590fb63
4
- data.tar.gz: 9909d8069a937593e7a583d6fd8db1989f7eb8e321d9c655e2d0f376caeffd24
3
+ metadata.gz: 710c9651cae2494969fe031b4d8a91bbcebd123c8d395b512bec5b193c50e6ea
4
+ data.tar.gz: e2a4901ea784ec6331ccd65ef4df58daf04aba44c0c395f736da27eee14f8f12
5
5
  SHA512:
6
- metadata.gz: 161c50d8cb2dc9abaa91164c3898f57942a6d51fcaadcb83bfd52cb96115548772be5d244d7e620f71751b523fdd5db0ab8a8a8bc76624be40a94d0341ac53c3
7
- data.tar.gz: abaa5c58dfc5aaa98a553b4cb582c738a4bcd3649e5681c034a32b550bb583d106e4b0240cb0c5225dc0c8f58b3a32af2bd314d5894209b39b72e26446c6f4e3
6
+ metadata.gz: ad85e178173ba17e7cfc0b452cc1bcdd50e7c17374885b8aecddd4495e04a5492c14bdecfdd13445738e63c8bfa2434c3f0957ab1e7d803dc18ba42722726761
7
+ data.tar.gz: be53d32e0fdcbcf4caee01762e5bb5d6b8452ffeaf6ae7f65e9746cf7a222aa65ab38ca8dd4db8de91c6f0507e4fc9df88d30c286e29a83d994d020bae4759a1
data/README.md CHANGED
@@ -257,8 +257,8 @@ system (eg. disk full) and omits the usage summary
257
257
  ```
258
258
 
259
259
  The methods are defined as instance methods on `ShellOpts::ShellOpts` and as
260
- class methods on `ShellOpts`. The class methods stores program name and usage
261
- string in global variables that are reset by `ShellOpts.reset`
260
+ class methods on `ShellOpts`. They can also be included in the global scope by
261
+ `include ShellOpts::Utils`
262
262
 
263
263
  ## Example
264
264
 
data/TODO CHANGED
@@ -1,5 +1,6 @@
1
1
 
2
2
  TODO
3
+ o Consolidate some of the 3 variations of #error and #fail
3
4
  o Add a option flag for solitary options (--help)
4
5
  o Make a #to_yaml
5
6
  o Make an official dump method for debug
@@ -19,7 +19,6 @@ module ShellOpts
19
19
  attr_reader :commands
20
20
 
21
21
  # List of options in declaration order
22
- # order
23
22
  attr_reader :option_list
24
23
 
25
24
  # List of commands in declaration order
@@ -0,0 +1,16 @@
1
+
2
+ module ShellOpts
3
+ # Use `include ShellOpts::Utils` to include ShellOpts utility methods in the
4
+ # global namespace
5
+ module Utils
6
+ # Forwards to `ShellOpts.error`
7
+ def error(*msgs)
8
+ ::ShellOpts.error(*msgs)
9
+ end
10
+
11
+ # Forwards to `ShellOpts.fail`
12
+ def fail(*msgs)
13
+ ::ShellOpts.fail(*msgs)
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Shellopts
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
data/lib/shellopts.rb CHANGED
@@ -7,6 +7,9 @@ require 'shellopts/parser.rb'
7
7
  # library API consists of the methods {ShellOpts.process}, {ShellOpts.error},
8
8
  # and {ShellOpts.fail} and the result class {ShellOpts::ShellOpts}
9
9
  #
10
+ # ShellOpts inject the constant PROGRAM into the global scope. It contains the
11
+ # name of the program
12
+ #
10
13
  module ShellOpts
11
14
  # Process command line options and arguments. #process takes a usage string
12
15
  # defining the options and the array of command line arguments to be parsed
@@ -76,7 +79,7 @@ module ShellOpts
76
79
  # shellopts.args.each { |arg| ... }
77
80
  # shellopts.error("Something went wrong")
78
81
  #
79
- def self.process(usage, argv, program_name: File.basename($0), &block)
82
+ def self.process(usage, argv, program_name: PROGRAM, &block)
80
83
  if !block_given?
81
84
  ShellOpts.new(usage, argv, program_name: program_name)
82
85
  else
@@ -93,15 +96,21 @@ module ShellOpts
93
96
  @shellopts = nil
94
97
  end
95
98
 
96
- # Print error message and usage string and exit with status 1. Can only be
97
- # called after #process. Forwards to {::ShellOpts#error}
99
+ # Print error message and usage string and exit with status 1. It use the
100
+ # current ShellOpts object if defined. This method should be called in
101
+ # response to user-errors (eg. specifying an illegal option)
98
102
  def self.error(*msgs)
99
- @shellopts&.error(*msgs) or raise InternalError, "ShellOpts class variable not initialized"
103
+ program = @shellopts&.program_name || PROGRAM
104
+ usage = @shellopts&.usage || (defined?(USAGE) && USAGE ? Grammar.compile(PROGRAM, USAGE).usage : nil)
105
+ emit_and_exit(program, usage, *msgs)
100
106
  end
101
107
 
102
- # Print error message and exit with status 1. Forwards to {::ShellOpts#fail}
108
+ # Print error message and exit with status 1. It use the current ShellOpts
109
+ # object if defined. This method should not be called in response to
110
+ # user-errors but system errors (like disk full)
103
111
  def self.fail(*msgs)
104
- @shellopts&.fail(*msgs) or raise InternalError, "ShellOpts class variable not initialized"
112
+ program = @shellopts&.program_name || PROGRAM
113
+ emit_and_exit(program, nil, *msgs)
105
114
  end
106
115
 
107
116
  # The compilation object
@@ -162,16 +171,13 @@ module ShellOpts
162
171
  # should be called in response to user-errors (eg. specifying an illegal
163
172
  # option)
164
173
  def error(*msgs)
165
- $stderr.puts "#{program_name}: #{msgs.join}"
166
- $stderr.puts "Usage: #{program_name} #{usage}"
167
- exit 1
174
+ ::ShellOpts.emit_and_exit(program_name, usage, msgs)
168
175
  end
169
176
 
170
177
  # Print error message and exit with status 1. This method should not be
171
178
  # called in response to user-errors but system errors (like disk full)
172
179
  def fail(*msgs)
173
- $stderr.puts "#{program_name}: #{msgs.join}"
174
- exit 1
180
+ ::ShellOpts.emit_and_exit(program_name, nil, msgs)
175
181
  end
176
182
  end
177
183
 
@@ -191,5 +197,12 @@ module ShellOpts
191
197
 
192
198
  private
193
199
  @shellopts = nil
200
+
201
+ def self.emit_and_exit(program, usage, *msgs)
202
+ $stderr.puts "#{program}: #{msgs.join}"
203
+ $stderr.puts "Usage: #{program} #{usage}" if usage
204
+ exit 1
205
+ end
194
206
  end
195
207
 
208
+ PROGRAM = File.basename($PROGRAM_NAME)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shellopts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-20 00:00:00.000000000 Z
11
+ date: 2019-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -115,6 +115,7 @@ files:
115
115
  - lib/shellopts/grammar/option.rb
116
116
  - lib/shellopts/grammar/program.rb
117
117
  - lib/shellopts/parser.rb
118
+ - lib/shellopts/utils.rb
118
119
  - lib/shellopts/version.rb
119
120
  - shellopts.gemspec
120
121
  homepage: http://github.com/clrgit/shellopts