clamp 1.1.2 → 1.2.0.beta1

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
  SHA1:
3
- metadata.gz: b66f072d297e47ff393380b7507588265bfb147f
4
- data.tar.gz: d4ab396390d9d8b850bcc74c39663bdd1f808a00
3
+ metadata.gz: aec7a5c34695b2517afb8215a0cbe8e9e4b2c8b9
4
+ data.tar.gz: f20e0d18b9e4be570fc7ae1f349d4d022f0f7644
5
5
  SHA512:
6
- metadata.gz: b37599b12abd509ef7d8f59e6267d624622d15ce34c7abf19c5669fc4d181dd418123aa567cfde22aae521db1363ba0022ca2a3ff4c17ef58823745f38e1d38e
7
- data.tar.gz: 3f9e439caa164fec96a18ccabe438b6cc2f0edda1f7c09c76181c4f56b47c02b084995d36185ab2f702ec9bc0c81a5d1975e3ece7fb6b6f00b9d71273325c64e
6
+ metadata.gz: 554f0e8fefe510adeacd74357333302d2f3ae9a4b98f87e0140956e4b35ff6f60b7b3031260b8cba2c2f96079b201ebc4c6256bed160d52038a079e7538defda
7
+ data.tar.gz: 85d29744e560feb6c06096739df752128bd1df915237da53b1c7ea7150ade1d3516016eb3014c7fc1f2958fdcf2074c35e00d6aa64057a8c8c56c9c82094f879
data/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## PENDING
4
+
5
+ * Add option to `Clamp.allow_options_after_parameters`.
6
+
3
7
  ## 1.1.2 (2017-02-12)
4
8
 
5
9
  * Improve usage help for commands with both parameters and subcommands.
data/README.md CHANGED
@@ -163,6 +163,16 @@ Declaring an option "`:hidden`" will cause it to be hidden from `--help` output.
163
163
  option "--some-option", "VALUE", "Just a little option", :hidden => true
164
164
  ```
165
165
 
166
+ ### Version option
167
+
168
+ A common idiom is to have an option `--version` that outputs the command version and doesn't run any subcommands. This can be acheived by:
169
+
170
+ ```ruby
171
+ option "--version", :flag, "Show version" do
172
+ puts MyGem::VERSION
173
+ exit(0)
174
+ end
175
+ ```
166
176
 
167
177
  Declaring parameters
168
178
  --------------------
@@ -286,6 +296,18 @@ parameter "[HOST]", "server address", :environment_variable => "MYAPP_HOST"
286
296
 
287
297
  Clamp will check the specified envariables in the absence of values supplied on the command line, before looking for a default value.
288
298
 
299
+ ### Allowing options after parameters
300
+
301
+ Many option-parsing libraries - notably [GNU `getopt(3)`](https://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html) - allow option and parameter arguments to appear in any order on the command-line, e.g.
302
+
303
+ foobar --foo=bar something --fnord=snuffle another-thing
304
+
305
+ By default, Clamp does not allow options and parameters to be "interspersed" in this way. If you want that behaviour, set:
306
+
307
+ ```ruby
308
+ Clamp.allow_options_after_parameters = true
309
+ ```
310
+
289
311
  Declaring Subcommands
290
312
  ---------------------
291
313
 
@@ -1,4 +1,9 @@
1
1
  module Clamp
2
+
3
+ class << self
4
+ attr_accessor :allow_options_after_parameters
5
+ end
6
+
2
7
  module Option
3
8
 
4
9
  module Parsing
@@ -14,7 +19,18 @@ module Clamp
14
19
  private
15
20
 
16
21
  def set_options_from_command_line
17
- while remaining_arguments.first && remaining_arguments.first.start_with?("-")
22
+ argument_buffer = []
23
+ argument_buffer_limit = self.class.parameter_buffer_limit
24
+ until remaining_arguments.empty?
25
+
26
+ unless remaining_arguments.first.start_with?("-")
27
+ if argument_buffer.size < argument_buffer_limit
28
+ argument_buffer << remaining_arguments.shift
29
+ next
30
+ else
31
+ break
32
+ end
33
+ end
18
34
 
19
35
  switch = remaining_arguments.shift
20
36
  break if switch == "--"
@@ -42,6 +58,7 @@ module Clamp
42
58
  end
43
59
 
44
60
  end
61
+ remaining_arguments.unshift(*argument_buffer)
45
62
  end
46
63
 
47
64
  def default_options_from_environment
@@ -74,4 +91,5 @@ module Clamp
74
91
  end
75
92
 
76
93
  end
94
+
77
95
  end
@@ -27,6 +27,12 @@ module Clamp
27
27
  superclass_inheritable_parameters + parameters.select(&:inheritable?)
28
28
  end
29
29
 
30
+ def parameter_buffer_limit
31
+ return 0 unless Clamp.allow_options_after_parameters
32
+ return Float::INFINITY if inheritable_parameters.any?(&:multivalued?)
33
+ inheritable_parameters.size
34
+ end
35
+
30
36
  private
31
37
 
32
38
  def superclass_inheritable_parameters
@@ -1,3 +1,3 @@
1
1
  module Clamp
2
- VERSION = "1.1.2".freeze
2
+ VERSION = "1.2.0.beta1".freeze
3
3
  end
@@ -0,0 +1,56 @@
1
+ require "spec_helper"
2
+
3
+ describe Clamp::Command do
4
+
5
+ extend CommandFactory
6
+ include OutputCapture
7
+
8
+ context "with allow_options_after_parameters enabled" do
9
+
10
+ before do
11
+ Clamp.allow_options_after_parameters = true
12
+ end
13
+
14
+ after do
15
+ Clamp.allow_options_after_parameters = false
16
+ end
17
+
18
+ given_command("cmd") do
19
+
20
+ option ["-v", "--verbose"], :flag, "Be noisy"
21
+
22
+ subcommand "say", "Say something" do
23
+
24
+ option "--loud", :flag, "say it loud"
25
+
26
+ parameter "WORDS ...", "the thing to say", :attribute_name => :words
27
+
28
+ def execute
29
+ message = words.join(" ")
30
+ message = message.upcase if loud?
31
+ message *= 3 if verbose?
32
+ $stdout.puts message
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ it "still works" do
40
+ command.run(%w(say foo))
41
+ expect(stdout).to eql("foo\n")
42
+ end
43
+
44
+ it "honours options after positional arguments" do
45
+ command.run(%w(say blah --verbose))
46
+ expect(stdout).to eql("blahblahblah\n")
47
+ end
48
+
49
+ it "honours options declared on subcommands" do
50
+ command.run(%w(say --loud blah))
51
+ expect(stdout).to eql("BLAH\n")
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -6,6 +6,14 @@ RSpec.configure do |config|
6
6
 
7
7
  config.mock_with :rr
8
8
 
9
+ config.around(:each) do |example|
10
+ begin
11
+ example.run
12
+ rescue SystemExit => e
13
+ fail "Unexpected exit with status #{e.status}"
14
+ end
15
+ end
16
+
9
17
  end
10
18
 
11
19
  module OutputCapture
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-12 00:00:00.000000000 Z
11
+ date: 2017-11-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Clamp provides an object-model for command-line utilities.
@@ -64,6 +64,7 @@ files:
64
64
  - spec/clamp/messages_spec.rb
65
65
  - spec/clamp/option/definition_spec.rb
66
66
  - spec/clamp/option_module_spec.rb
67
+ - spec/clamp/option_reordering_spec.rb
67
68
  - spec/clamp/parameter/definition_spec.rb
68
69
  - spec/spec_helper.rb
69
70
  homepage: http://github.com/mdub/clamp
@@ -81,12 +82,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
82
  version: '0'
82
83
  required_rubygems_version: !ruby/object:Gem::Requirement
83
84
  requirements:
84
- - - ">="
85
+ - - ">"
85
86
  - !ruby/object:Gem::Version
86
- version: '0'
87
+ version: 1.3.1
87
88
  requirements: []
88
89
  rubyforge_project:
89
- rubygems_version: 2.6.10
90
+ rubygems_version: 2.6.13
90
91
  signing_key:
91
92
  specification_version: 4
92
93
  summary: a minimal framework for command-line utilities
@@ -96,5 +97,6 @@ test_files:
96
97
  - spec/clamp/messages_spec.rb
97
98
  - spec/clamp/option/definition_spec.rb
98
99
  - spec/clamp/option_module_spec.rb
100
+ - spec/clamp/option_reordering_spec.rb
99
101
  - spec/clamp/parameter/definition_spec.rb
100
102
  - spec/spec_helper.rb