luban-cli 0.4.0 → 0.4.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
  SHA1:
3
- metadata.gz: c3717edf51bdf2888b4d7d7d378b8ba777e7f2df
4
- data.tar.gz: 23c48793a99823629c9b339a7b65f64f3be9dc27
3
+ metadata.gz: e6a17dc64fcd6ba46e069c6625848100fa33402c
4
+ data.tar.gz: 7415b6f89e06a8aa7ca5a15f0dd8682a75f6a247
5
5
  SHA512:
6
- metadata.gz: d5dd0736e211101a179af588fab6892445fc3e6b77d4203237f383cb14267da9ce61dbdc565c6ffa8cf4a46fbf7feaba43c1347afbfc8cb27afa0b00e42222ab
7
- data.tar.gz: abe9f13478171fd6c9bb4b5163f73919666b2f830c5a43896812da9e09e938da9ed16c23699486657b94752ef68849f902ff5384ffee6dcb6bce5e1026f636fa
6
+ metadata.gz: 9ccd92008a1cd6f97e489b42990c86ca9a6c83e99750787fa7dbcd97d9845f962007e2a1d906c05aaea6cdd3854bb6028b9fed25d12f9e84da40fe998b533ab4
7
+ data.tar.gz: a06c296c1ce5e5564d48d306ac3b13bf0e1f943fca349b89ba025a1cdb0098c79fcbfcd61ec17bbd9694006a71bba8e1055357b0003275d025bbc495bf3f63e4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Change log
2
2
 
3
+ ## Version 0.4.1 (Nov 05, 2015)
4
+
5
+ Minor enhancements:
6
+ * After parsing, added any remaining command line arguments to opts[:__remaining__]
7
+ * This is useful to handle arguments provided after the double hyphen
8
+ * Used Ruby refinements to re-implement camelcase and snakecase handling
9
+
10
+ Bug fixes:
11
+ * Filter empty default values for help message
12
+
3
13
  ## Version 0.4.0 (Oct 14, 2015)
4
14
 
5
15
  New features:
data/README.md CHANGED
@@ -98,9 +98,9 @@ my_app 1.0.0
98
98
  $ ruby my_app.rb john -p mr -s jr
99
99
  Hi, Mr. John Jr.!
100
100
 
101
- ruby examples/hi.rb john -p mr -s jr -V
102
- Options: {:version=>false, :prefix=>"mr", :suffix=>"jr", :verbose=>true, :help=>false}
103
- Arguments: {:name=>"chi"}
101
+ $ ruby my_app.rb john -p mr -s jr -V
102
+ Options: {:version=>false, :prefix=>"mr", :suffix=>"jr", :verbose=>true, :help=>false, :__remaining__=>[]}
103
+ Arguments: {:name=>"john"}
104
104
  Hi, Mr. John Jr.!
105
105
  ```
106
106
 
@@ -387,6 +387,24 @@ end
387
387
 
388
388
  ```
389
389
 
390
+ ## Option Parsing Termination
391
+
392
+ It is a convention that a double hyphen is a signal to stop option interpretation and to read the remaining statements on the command line literally. Luban::CLI supports this convention and stores the remaining statements on the command line, if any, into the keyword argument :opts with a hash key :\__remaining\__, which is passed to the given action handler.
393
+
394
+ The following refers to the simple example shown in the section Usage, which demonstrates the parsing result with remaining statements in option parsing termination.
395
+
396
+ ```ruby
397
+ $ ruby my_app.rb john -p mr -s jr -V
398
+ Options: {:version=>false, :prefix=>"mr", :suffix=>"jr", :verbose=>true, :help=>false, :__remaining__=>[]}
399
+ Arguments: {:name=>"john"}
400
+ Hi, Mr. John Jr.!
401
+
402
+ $ ruby my_app.rb john -p mr -s jr -V -- --test here
403
+ Options: {:version=>false, :prefix=>"mr", :suffix=>"jr", :verbose=>true, :help=>false, :__remaining__=>["--test", "here"]}
404
+ Arguments: {:name=>"john"}
405
+ Hi, Mr. John Jr.!
406
+ ```
407
+
390
408
  ## Commands
391
409
 
392
410
  Luban::CLI supports commands/subcommands. Commands can also be nested. However, all action handlers are preferred to be defined under the the application class; otherwise RuntimeError will be raised.
@@ -1,6 +1,8 @@
1
1
  module Luban
2
2
  module CLI
3
3
  class Base
4
+ using CoreRefinements
5
+
4
6
  def command(cmd, base: Command, **opts, &blk)
5
7
  cmd = cmd.to_sym
6
8
  @commands[cmd] = command_class(cmd, base).new(self, cmd, **opts, &blk)
@@ -11,9 +13,9 @@ module Luban
11
13
  end
12
14
 
13
15
  def use_commands(module_name, **opts, &blk)
14
- module_class = Object.const_get(camelcase(module_name.to_s), false)
16
+ module_class = Object.const_get(module_name.camelcase, false)
15
17
  module_class.constants(false).map { |c| module_class.const_get(c, false) }.each do |c|
16
- command(snakecase(c.name), base: c, **opts, &blk) if c < Command
18
+ command(c.name.snakecase, base: c, **opts, &blk) if c < Command
17
19
  end
18
20
  end
19
21
 
@@ -32,7 +34,7 @@ module Luban
32
34
  protected
33
35
 
34
36
  def command_class(cmd, base)
35
- class_name = camelcase(cmd)
37
+ class_name = cmd.camelcase
36
38
  if command_class_defined?(class_name)
37
39
  get_command_class(class_name)
38
40
  else
@@ -51,22 +53,6 @@ module Luban
51
53
  def define_command_class(class_name, base)
52
54
  self.class.send(:define_class, class_name, base: base, namespace: self.class)
53
55
  end
54
-
55
- def camelcase(str)
56
- str = str.to_s.dup
57
- str.gsub!(/(\:|\/)(.?)/){ "::#{$2.upcase}" }
58
- str.gsub!(/(?:_+|-+)([a-z])/){ $1.upcase }
59
- str.gsub!(/(\A|\s)([a-z])/){ $1 + $2.upcase }
60
- str
61
- end
62
-
63
- def snakecase(str)
64
- str.gsub(/::/, ':').
65
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
66
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
67
- tr("-", "_").
68
- downcase
69
- end
70
56
  end
71
57
  end
72
58
  end
@@ -96,7 +96,8 @@ module Luban
96
96
 
97
97
  def undef_singleton_method(method_name)
98
98
  # Undefine methods that are defined in the eigenclass
99
- (class << self; self; end).send(:undef_method, method_name)
99
+ singleton_class.send(:undef_method, method_name)
100
+ #(class << self; self; end).send(:undef_method, method_name)
100
101
  end
101
102
 
102
103
  def configure(&blk)
@@ -260,7 +261,7 @@ module Luban
260
261
  end
261
262
 
262
263
  def add_parser_defaults
263
- add_section("Defaults", options.values.map(&:default_str))
264
+ add_section("Defaults", options.values.map(&:default_str).reject { |d| d.empty? })
264
265
  end
265
266
 
266
267
  def add_parser_commands
@@ -131,6 +131,7 @@ module Luban
131
131
  else
132
132
  validate_required_options
133
133
  validate_required_arguments
134
+ result[:opts][:__remaining__] = argv
134
135
  yield args: result[:args], opts: result[:opts]
135
136
  if has_commands?
136
137
  dispatch_command(cmd: result[:cmd], argv: result[:argv])
@@ -0,0 +1,33 @@
1
+ module Luban
2
+ module CLI
3
+ module CoreRefinements
4
+ refine String do
5
+ def camelcase
6
+ str = dup
7
+ str.gsub!(/(\:|\/)(.?)/){ "::#{$2.upcase}" }
8
+ str.gsub!(/(?:_+|-+)([a-z])/){ $1.upcase }
9
+ str.gsub!(/(\A|\s)([a-z])/){ $1 + $2.upcase }
10
+ str
11
+ end
12
+
13
+ def snakecase
14
+ gsub(/::/, ':').
15
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
16
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
17
+ tr("-", "_").
18
+ downcase
19
+ end
20
+ end
21
+
22
+ refine Symbol do
23
+ def camelcase
24
+ to_s.camelcase
25
+ end
26
+
27
+ def snakecase
28
+ to_s.snakecase
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  module Luban
2
2
  module CLI
3
- VERSION = "0.4.0"
3
+ VERSION = "0.4.2"
4
4
  end
5
5
  end
data/lib/luban/cli.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative "cli/error"
2
2
  require_relative "cli/version"
3
+ require_relative "cli/core_refinements"
3
4
  require_relative "cli/base"
4
5
  require_relative "cli/command"
5
6
  require_relative "cli/application"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luban-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rubyist Chi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-19 00:00:00.000000000 Z
11
+ date: 2015-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -63,6 +63,7 @@ files:
63
63
  - lib/luban/cli/base/parse.rb
64
64
  - lib/luban/cli/base/switch.rb
65
65
  - lib/luban/cli/command.rb
66
+ - lib/luban/cli/core_refinements.rb
66
67
  - lib/luban/cli/error.rb
67
68
  - lib/luban/cli/version.rb
68
69
  - luban-cli.gemspec