optparse2 0.5.2 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3dff76909d0005ee325d16f79bfa00b18d26a65b3b06b018eef33713e40d250
4
- data.tar.gz: 14733f7d9258908dc1df030b475937590089d1f6eb45f0a99f7df8dbcc6a0ded
3
+ metadata.gz: b99d0120a660e1c0831e8bb7498a7628f2e5e77bef266540ffdb7022120190e0
4
+ data.tar.gz: 7a9e744ba67a2f23e0a69fde19f30807867ac259902cbaeff477c5b64b3c3627
5
5
  SHA512:
6
- metadata.gz: e715049dd9d3d3089c6d7ae994666b497eab0436927fe9a98114fcf1d46456798284fb5402fde4edf34ff2dcaf95f5856067fcbcc313fe20ccc46cc0e78635d1
7
- data.tar.gz: 32fc2f6d56246b215845adf065103a60be82d5daf78f3d09d6ae64c1a100664a7521cfdfbc2c362047df74933567f59fe97caa839fce054e2baec97d943e863d
6
+ metadata.gz: f08a3c93c23ca10eb994e63317726c2c2b13407e7b80bdf1cedd095c9066679ac1c8d552c3691f3b87e9596c6c147def64a605a29794388d29766da3a5ccb4e4
7
+ data.tar.gz: 40f819a15e6f05be7dd1e0ce72ac264492405415386840fad30543ef277ff2e93b048e7963693ec682b82bc410e30406d5a85c08a551f2337bcc87d4f3c68684
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class OptParse2
4
- VERSION = "0.5.2"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/optparse2.rb CHANGED
@@ -13,11 +13,15 @@ class OptParse2
13
13
  end
14
14
  self.pos_set_banner = true
15
15
 
16
+ attr_reader :into
17
+
16
18
  def initialize(...)
17
19
  @defaults = Set[]
18
20
  @positional = []
19
21
  @required = Set[]
20
22
  @rest = nil
23
+ @into = nil
24
+ @group = nil
21
25
  self.pos_set_banner = OptParse2.pos_set_banner
22
26
  super
23
27
  end
@@ -31,7 +35,7 @@ class OptParse2
31
35
  attr_writer :switch_name
32
36
  def switch_name; defined?(@switch_name) ? @switch_name : super end
33
37
 
34
- def set_switch_name_possibly_block_value
38
+ def set_switch_name_possibly_block_value(val)
35
39
  if @block.nil? && @arg.nil?
36
40
  q = switch_name.to_sym
37
41
  @block = proc { q }
@@ -64,7 +68,7 @@ class OptParse2
64
68
  end
65
69
 
66
70
  # Update `make_switch` to support OptParse2's keyword arguments
67
- def make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil,
71
+ def make_switch(opts, block, hidden: false, key: @group, default: nodefault=true, default_description: nil,
68
72
  required: false)
69
73
  sw, *rest = super(opts, block)
70
74
 
@@ -102,6 +106,17 @@ class OptParse2
102
106
  on_tail("\n" + msg)
103
107
  end
104
108
 
109
+ def group(name, default: nodefault=true)
110
+ old_group, @group = @group, name
111
+ yield
112
+ if !nodefault && !@defaults.any? { |x| x.switch_name.to_sym == name }
113
+ (orig_default = default; default = proc { orig_default }) unless default.respond_to?(:call)
114
+ @defaults << Struct.new(:switch_name, :default_){ def default = default_.() }.new(name, default) # TODO: This should probably be extracted out into a class lol
115
+ end
116
+ ensure
117
+ @group = old_group
118
+ end
119
+
105
120
  def order!(argv = default_argv, into: nil, **keywords, &nonopt)
106
121
  if into.nil? && !@defaults.empty?
107
122
  raise "cannot call `order!` without an `into:` if there are default values"
@@ -113,6 +128,11 @@ class OptParse2
113
128
  into[key] = value
114
129
  end
115
130
 
131
+ # Really this shouldn't be on the class and should probably be passed to the block of each
132
+ # parameter as requested, but that requires a _significant_ amount of tinkering with `optparse`'s
133
+ # internals, which is not really in scope.
134
+ @into = already_done
135
+
116
136
  non_options = []
117
137
 
118
138
  result = super(argv, into: already_done, **keywords, &non_options.method(:<<))
@@ -156,6 +176,8 @@ class OptParse2
156
176
  end
157
177
 
158
178
  argv2
179
+ ensure
180
+ @into = nil # make sure we unset it when returning
159
181
  end
160
182
 
161
183
  module Positional
@@ -223,6 +245,14 @@ class OptParse2
223
245
  end
224
246
 
225
247
  def rest(name, *description, key: name.to_s.tr(' ', '-').to_sym, required: 0, &block)
248
+ required = case required
249
+ when true then 1
250
+ when false then 0
251
+ when ->x{ Integer === x && !x.negative? } then required
252
+ else raise TypeError, "invalid type for required: #{required.class} (must be bool or positive integer)"
253
+ end
254
+
255
+ required = 1 if required == true
226
256
  title = "#{'[' if required.zero?}#{name} ...#{']' if required.zero?}"
227
257
  banner.concat " #{title}" if pos_set_banner
228
258
  title += " (#{required} arg minimum)" if required > 0
data/publish ADDED
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # require 'optparse2'
4
+ require_relative 'lib/optparse2'
5
+ require 'gvars'
6
+
7
+ def `(cmd)
8
+ super.tap { raise "cmd failed: #{cmd} (exit: #{$?.exitstatus})" unless $?.success? }
9
+ end
10
+
11
+ ####################################################################################################
12
+ # Parse CLI Opts #
13
+ ####################################################################################################
14
+
15
+ OptParse2.new do |op|
16
+ op.pos 'gem', 'The name of the gem', default: proc { File.basename Dir.pwd }, default_description: 'current dir'
17
+
18
+ op.on '-f', '--force', 'Proceed even with dirty git status'
19
+ op.on '-n', '--dry', 'Do not actually publish the gem'
20
+
21
+ op.on '--version VERSION', 'Set the version string to VERSION'
22
+ op.on '-p', '--patch', 'Bump the patch version up', key: :version
23
+ op.on '-m', '--minor', 'Bump the minor version up (and reset patch to 0)', key: :version
24
+ op.on '--version-file=PATH', 'The file where the new version is stored', default: proc { File.join("lib", $gem, "version.rb") },
25
+ default_description: 'lib/<gem>/version.rb'
26
+
27
+
28
+ # note: no `--major`, as that should be done with `--version`
29
+ op.parse! into: GVars
30
+ end
31
+
32
+ ####################################################################################################
33
+ # Check Git Dirty Status #
34
+ ####################################################################################################
35
+
36
+ unless `git status --porcelain`.chomp.empty?
37
+ if $force
38
+ warn "Proceeding with a dirty git status"
39
+ else
40
+ abort "Not proceeding due to dirty git status; use --force to override"
41
+ end
42
+ end
43
+
44
+ ####################################################################################################
45
+ # Update VERSION, if supplied #
46
+ ####################################################################################################
47
+
48
+ # Fetch the git version
49
+ def get_old_version
50
+ File.read($version_file)[/VERSION\s+=\s+"(\d+\.\d+\.\d+)"/, 1] or fail "cannot fetch old version"
51
+ end
52
+
53
+ def update_version
54
+
55
+ case $version
56
+ when :patch then $version = get_old_version.succ!
57
+ when :minor then $version = get_old_version.sub!(/\A(\d+\.\d+)\.\d*\z/, '\1').succ + '.0'
58
+ when String then # already set
59
+ else raise
60
+ end
61
+
62
+ puts "Using version #$version"
63
+ vf = File.read($version_file)
64
+ vf.sub!(/(VERSION\s+=\s+")\d+\.\d+\.\d+(")/, "\\1#$version\\2") or fail "cannot update version"
65
+ File.write $version_file, vf
66
+
67
+ unless $dry
68
+ system('git', 'add', '--', $version_file, exception: true)
69
+ system('git', 'commit', '-m', "Version #$version", exception: true)
70
+ system('git', 'push', exception: true)
71
+ end
72
+ end
73
+
74
+ update_version unless $version.nil?
75
+
76
+ ####################################################################################################
77
+ # Build and Publish the Gem #
78
+ ####################################################################################################
79
+
80
+ system('gem', 'build', exception: true)
81
+ unless $dry
82
+ system('gem', 'push', "#$gem-#$version.gem", exception: true)
83
+ system('gem', 'install', $gem, exception: true)
84
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optparse2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SamW
@@ -22,6 +22,7 @@ files:
22
22
  - lib/optparse2/fixes.rb
23
23
  - lib/optparse2/pathname.rb
24
24
  - lib/optparse2/version.rb
25
+ - publish
25
26
  - sig/optparse2.rbs
26
27
  licenses:
27
28
  - MIT