cyclops 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c7bfa77550e65e5f43540c3eb45b68c39d8699e
4
- data.tar.gz: 778b46e92d8b22c33eea63f03177459608a4615f
3
+ metadata.gz: d20c8ac18e0f174a287735b1bb22d9f1d4fec432
4
+ data.tar.gz: 88d9a76c70d8430b3ea7310d924465c598f23dc4
5
5
  SHA512:
6
- metadata.gz: 66d0e27a997554bb1bfbaab940b3c7a84f6709f5d7e337a6c5b22679b33e5af4fb51d47951a60d45d697c36a9935dbaf312b8a1aa2643cf0731d2fac27dc87c8
7
- data.tar.gz: ed78fac887806de5c77d631d8aa1099569a50a353756794b454656fcc3cd908e5cfe9cae0a6a374ed6571a0926dbdb069574cd373a2a23901b77a22cae5676c5
6
+ metadata.gz: 7b375b5da84cff2b15b5598813930e84a20e90dca93fd183ed246940daa86e268db1e2721d3110eb30cdbe5f737db583d22cbc3b99947863094fc105be82e11f
7
+ data.tar.gz: 214e3157759ffc2c9c7f5a0c600d9a37df16ef54cf3e64d2fb4ff90d5898379e4454e56f40fd78f8bfb7ab3f82bff76f185558c2b27cf936b9982ef32d378cc0
data/ChangeLog CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  = Revision history for cyclops
4
4
 
5
+ == 0.0.4 [2014-09-01]
6
+
7
+ * Added support for <tt>--[no-]<switch></tt>.
8
+ * <tt>--verbose</tt> also prints program name and version.
9
+ * Print error and usage on parse errors.
10
+
5
11
  == 0.0.3 [2014-04-14]
6
12
 
7
13
  * Always set symbol key in options hash.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to cyclops version 0.0.3
5
+ This documentation refers to cyclops version 0.0.4
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -13,7 +13,7 @@ enough CLI libraries already...
13
13
 
14
14
  == LINKS
15
15
 
16
- Documentation:: https://blackwinter.github.io/cyclops/
16
+ Documentation:: https://blackwinter.github.com/cyclops
17
17
  Source code:: https://github.com/blackwinter/cyclops
18
18
  RubyGem:: https://rubygems.org/gems/cyclops
19
19
 
data/lib/cyclops.rb CHANGED
@@ -95,11 +95,10 @@ class Cyclops
95
95
  run(arguments)
96
96
  rescue => err
97
97
  raise if $VERBOSE
98
- abort "#{err.backtrace.first}: #{err} (#{err.class})"
98
+ abort err.is_a?(OptionParser::ParseError) ?
99
+ "#{err}\n#{usage}" : "#{err.backtrace.first}: #{err} (#{err.class})"
99
100
  ensure
100
- options.each_value { |value|
101
- value.close if value.is_a?(Zlib::GzipWriter)
102
- }
101
+ options.each_value { |value| value.close if value.is_a?(Zlib::GzipWriter) }
103
102
  end
104
103
 
105
104
  def run(arguments)
@@ -168,14 +167,10 @@ class Cyclops
168
167
  end
169
168
 
170
169
  def open_file_or_std(file, write = false)
171
- if file == '-'
172
- write ? stdout : stdin
173
- else
170
+ file == '-' ? write ? stdout : stdin : begin
174
171
  gz = file =~ /\.gz\z/i
175
172
 
176
- if write
177
- gz ? Zlib::GzipWriter.open(file) : File.open(file, 'w')
178
- else
173
+ write ? gz ? Zlib::GzipWriter.open(file) : File.open(file, 'w') : begin
179
174
  quit "No such file: #{file}" unless File.readable?(file)
180
175
  (gz ? Zlib::GzipReader : File).open(file)
181
176
  end
@@ -186,7 +181,7 @@ class Cyclops
186
181
  return unless file
187
182
 
188
183
  if File.readable?(file)
189
- @config = SafeYAML.load_file(file, :deserialize_symbols => true)
184
+ @config = SafeYAML.load_file(file, deserialize_symbols: true)
190
185
  else
191
186
  quit "No such file: #{file}" unless default
192
187
  end
@@ -247,7 +242,9 @@ class Cyclops
247
242
  def verbose_opts(opts)
248
243
  verbose, debug = defaults.key?(:verbose), defaults.key?(:debug)
249
244
 
250
- opts.switch(:verbose, 'Print verbose output') if verbose
245
+ opts.switch(:verbose, 'Print verbose output') {
246
+ warn "#{progname} v#{version}"
247
+ } if verbose
251
248
 
252
249
  msg = "; #{debug_message}" if respond_to?(:debug_message, true)
253
250
  opts.switch(:debug, :D, "Print debug output#{msg}") if debug
@@ -30,10 +30,14 @@ class Cyclops
30
30
 
31
31
  attr_accessor :cli
32
32
 
33
+ OPTION_RE = /(\w+)__(\w+)(\?)?\z/
34
+
35
+ SWITCH_RE = /(\w+)(\?)?\z/
36
+
33
37
  KEY_POOL = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
34
38
 
35
39
  def keys
36
- { :used => keys = top.short.keys, :free => KEY_POOL - keys }
40
+ { used: keys = top.short.keys, free: KEY_POOL - keys }
37
41
  end
38
42
 
39
43
  def separator(string = '')
@@ -55,9 +59,9 @@ class Cyclops
55
59
  # the argument name ends with a question mark, the value is marked
56
60
  # as optional.
57
61
  def option(name, *args, &block)
58
- if name =~ /(\w+)__(\w+)(\?)?\z/
59
- sym = name.is_a?(Symbol)
62
+ sym = name.is_a?(Symbol)
60
63
 
64
+ if name =~ OPTION_RE
61
65
  name, arg, opt = $1, $2, !!$3
62
66
  __on_opts(name, args, sym)
63
67
 
@@ -69,7 +73,7 @@ class Cyclops
69
73
  yield value if block_given?
70
74
  }
71
75
  else
72
- on(*__on_opts(name, args), &block)
76
+ on(*__on_opts(name, args, sym), &block)
73
77
  end
74
78
  end
75
79
 
@@ -84,16 +88,35 @@ class Cyclops
84
88
  #
85
89
  # Sets the CLI's +name+ option (as a Symbol) to +true+ and calls
86
90
  # the optional block (with no argument).
91
+ #
92
+ # If +name+ ends with a question mark, installs only the long option
93
+ # and sets the CLI's +name+ option (as a Symbol) to either +true+ or
94
+ # +false+, depending on whether <tt>--name</tt> or <tt>--no-name</tt>
95
+ # was given on the command line.
87
96
  def switch(name, *args)
88
- on(*__on_opts(name, args)) {
89
- cli.options[name.to_sym] = true
90
- yield if block_given?
91
- }
97
+ sym = name.is_a?(Symbol)
98
+
99
+ name, opt = $1, !!$2 if name =~ SWITCH_RE
100
+
101
+ if opt
102
+ __on_opts(name, args, false)
103
+ args.first.insert(2, '[no-]')
104
+
105
+ on(*args) { |value|
106
+ cli.options[name.to_sym] = value
107
+ yield if block_given?
108
+ }
109
+ else
110
+ on(*__on_opts(name, args, sym)) {
111
+ cli.options[name.to_sym] = true
112
+ yield if block_given?
113
+ }
114
+ end
92
115
  end
93
116
 
94
117
  private
95
118
 
96
- def __on_opts(name, args, sym = name.is_a?(Symbol))
119
+ def __on_opts(name, args, sym)
97
120
  args.insert(0, "-#{args[0].is_a?(Symbol) ? args.shift : name[0]}") if sym
98
121
  args.insert(sym ? 1 : 0, "--#{name.to_s.tr('_', '-')}")
99
122
  end
@@ -4,7 +4,7 @@ class Cyclops
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 0
7
- TINY = 3
7
+ TINY = 4
8
8
 
9
9
  class << self
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyclops
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: highline
@@ -88,13 +88,15 @@ licenses:
88
88
  metadata: {}
89
89
  post_install_message: |2+
90
90
 
91
- cyclops-0.0.3 [2014-04-14]:
91
+ cyclops-0.0.4 [2014-09-01]:
92
92
 
93
- * Always set symbol key in options hash.
93
+ * Added support for <tt>--[no-]<switch></tt>.
94
+ * <tt>--verbose</tt> also prints program name and version.
95
+ * Print error and usage on parse errors.
94
96
 
95
97
  rdoc_options:
96
98
  - "--title"
97
- - cyclops Application documentation (v0.0.3)
99
+ - cyclops Application documentation (v0.0.4)
98
100
  - "--charset"
99
101
  - UTF-8
100
102
  - "--line-numbers"
@@ -115,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
117
  version: '0'
116
118
  requirements: []
117
119
  rubyforge_project:
118
- rubygems_version: 2.2.2
120
+ rubygems_version: 2.4.1
119
121
  signing_key:
120
122
  specification_version: 4
121
123
  summary: A command-line option parser based on optparse.