rack-app 3.1.0 → 3.2.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
  SHA1:
3
- metadata.gz: 61fb1f8227a3ca529ee22caefa0d7bc8e5b46993
4
- data.tar.gz: 3c7bdc49faff05050c558f98d22919cf14f1ae22
3
+ metadata.gz: cc5f8fe7cb10a7526f5ffaa5d554ee86ec85dd49
4
+ data.tar.gz: 0fff8341fba579752f6c05720b2355564b19e1c1
5
5
  SHA512:
6
- metadata.gz: f081f2de5494f5b2730a85722bc4eeeb2df25dbd16cae9d9ce01842ed5c085877dfd1b258e68898bf452999c212ea80630f25f75adf34647dc7321e7b9bc0944
7
- data.tar.gz: 28684ea25e3c2a78370ebd696622a340a7d0dbb6303f2d051103ea8aa3f1f94f0325c6e8c8c35c2c448fbb055b2a6009aff2a36e8a22359e34c25ebabcef7767
6
+ metadata.gz: 115d44b4a0faa00d8bf099e2224ee2f6af5d133157b331d24b297b0f7c75bd22126765bb0a1c7a491175187df08ecca6504f0f5d259dc22efa693212af760c12
7
+ data.tar.gz: 7920c391d960faee68d9e28ab6fb4784561880a9e8201c97e5d0819ff7fd0e85cfc2921f5e3e0e72b079692b901e113ff24f9d62a1d94a041ae08e3ac9ba23fc
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.0
1
+ 3.2.0
data/lib/rack/app/cli.rb CHANGED
@@ -42,7 +42,7 @@ class Rack::App::CLI
42
42
  def command(name, &block)
43
43
  command_prototype = Class.new(Rack::App::CLI::Command)
44
44
  command_prototype.class_exec(&block)
45
- commands[name.to_s.to_sym]= command_prototype.new(name.to_s.to_sym)
45
+ commands[name.to_s.to_sym]= command_prototype.new
46
46
  end
47
47
 
48
48
  end
@@ -1,10 +1,11 @@
1
1
  class Rack::App::CLI::Command
2
2
 
3
3
  require 'optparse'
4
+ require 'rack/app/cli/command/configurator'
4
5
 
5
6
  class << self
6
7
 
7
- def optparse_options
8
+ def option_definitions
8
9
  @options_parser_options ||= []
9
10
  end
10
11
 
@@ -16,7 +17,7 @@ class Rack::App::CLI::Command
16
17
  alias desc description
17
18
 
18
19
  def option(*args, &block)
19
- optparse_options << {:args => args, :block => block}
20
+ option_definitions << {:args => args, :block => block}
20
21
  end
21
22
 
22
23
  alias on option
@@ -27,67 +28,17 @@ class Rack::App::CLI::Command
27
28
 
28
29
  end
29
30
 
30
- attr_reader :name
31
-
32
- def initialize(name)
33
- @name = name.to_s
34
- @option_parser = OptionParser.new
35
- attach_definitions!
36
- update_banner!
37
- end
38
-
39
- def help_message
40
- @option_parser.help
31
+ def options
32
+ @options ||= {}
41
33
  end
42
34
 
43
- def description
44
- self.class.description
35
+ def action(*argv)
45
36
  end
46
37
 
47
38
  def start(argv)
48
- @option_parser.parse!(argv)
49
39
  action(*argv)
50
40
  rescue ArgumentError => ex
51
41
  $stderr.puts(ex.message)
52
42
  end
53
43
 
54
- def action(*argv)
55
- end
56
-
57
- protected
58
-
59
- def attach_definitions!
60
- self.class.optparse_options.each do |h|
61
- @option_parser.on(*h[:args]) do |*args|
62
- instance_exec(*args,&h[:block])
63
- end
64
- end
65
- end
66
-
67
- def update_banner!
68
-
69
- banner = @option_parser.banner
70
- banner.sub!('[options]', "#{@name} [options]")
71
-
72
- # [[:req, :a], [:opt, :b], [:rest, :c], [:keyreq, :d], [:keyrest, :e]]
73
- (method(:action).parameters rescue []).each do |type, keyword|
74
- case type
75
- when :req
76
- banner.concat(" <#{keyword}>")
77
-
78
- when :opt
79
- banner.concat(" [<#{keyword}>]")
80
-
81
- when :rest, :keyrest
82
- banner.concat(" [<#{keyword}> <#{keyword}> ...]")
83
-
84
- end
85
- end
86
-
87
- banner.concat("\n\n")
88
- banner.concat(description)
89
- banner.concat("\n\n")
90
-
91
- end
92
-
93
44
  end
@@ -0,0 +1,47 @@
1
+ module Rack::App::CLI::Command::Configurator
2
+
3
+ extend self
4
+
5
+ def configure(command, name, options_parser)
6
+
7
+ attach_definitions(command, options_parser, command.class.option_definitions)
8
+ update_banner(command, name, options_parser.banner)
9
+
10
+ end
11
+
12
+ protected
13
+
14
+ def attach_definitions(command, optparse, option_definitions)
15
+ option_definitions.each do |h|
16
+ optparse.on(*h[:args]) do |*args|
17
+ command.instance_exec(*args, &h[:block])
18
+ end
19
+ end
20
+ end
21
+
22
+ def update_banner(command, name, banner)
23
+
24
+ banner.sub!('[options]', "#{name} [options]")
25
+
26
+ # [[:req, :a], [:opt, :b], [:rest, :c], [:keyreq, :d], [:keyrest, :e]]
27
+ (command.method(:action).parameters rescue []).each do |type, keyword|
28
+ case type
29
+ when :req
30
+ banner.concat(" <#{keyword}>")
31
+
32
+ when :opt
33
+ banner.concat(" [<#{keyword}>]")
34
+
35
+ when :rest, :keyrest
36
+ banner.concat(" [<#{keyword}> <#{keyword}> ...]")
37
+
38
+ end
39
+ end
40
+
41
+ banner.concat("\n\n")
42
+ banner.concat(command.class.description)
43
+ banner.concat("\n\n")
44
+
45
+ end
46
+
47
+ end
@@ -12,10 +12,10 @@ module Rack::App::CLI::DefaultCommands::ListCommands
12
12
 
13
13
  puts_collection << [list_command_name.to_s.rjust(rjust), 'list all available command'].join(' ')
14
14
  known_commands.sort_by { |name, _| name.to_s }.each do |name, command|
15
- puts_collection << [name.to_s.rjust(rjust), command.description].join(' ')
15
+ puts_collection << [name.to_s.rjust(rjust), command.class.description].join(' ')
16
16
  end
17
17
 
18
- puts_collection
18
+ puts_collection.join("\n")
19
19
  end
20
20
 
21
21
  protected
@@ -1,5 +1,7 @@
1
1
  class Rack::App::CLI::Runner
2
2
 
3
+ CommandNotFound = Class.new(StandardError)
4
+
3
5
  def initialize(app)
4
6
  @cli = app.respond_to?(:cli) ? app.cli : Rack::App::CLI.new
5
7
  end
@@ -18,29 +20,46 @@ class Rack::App::CLI::Runner
18
20
  def show_help_message(argv)
19
21
  command_name = argv.shift
20
22
  command = command_for(command_name)
21
- command ? $stdout.puts(command.help_message) : show_commands
23
+ options_parser = configure_command(command,command_name)
24
+
25
+ $stdout.puts(options_parser.help)
26
+ rescue CommandNotFound
27
+ show_commands
22
28
  end
23
29
 
24
30
  def start_command_for(command_name, argv)
25
31
  case command_name.to_s
26
32
 
27
33
  when 'commands'
28
- show_commands
34
+ return show_commands
29
35
 
30
36
  when 'help'
31
- show_help_message(argv)
37
+ return show_help_message(argv)
38
+
39
+ else
40
+ command = command_for(command_name)
41
+ return run_command(argv, command, command_name)
32
42
 
33
43
  end
44
+ end
34
45
 
35
- command = command_for(command_name)
46
+ def run_command(argv, command, command_name)
47
+ return if command.nil?
48
+
49
+ option_parser = configure_command(command, command_name)
50
+ option_parser.parse!(argv)
51
+ command.start(argv)
36
52
 
37
- command && command.start(argv)
38
53
  end
39
54
 
40
- def command_for(name)
41
- return if name.nil?
55
+ def configure_command(command, command_name)
56
+ option_parser = OptionParser.new
57
+ Rack::App::CLI::Command::Configurator.configure(command, command_name, option_parser)
58
+ return option_parser
59
+ end
42
60
 
43
- commands[name.to_s.to_sym]
61
+ def command_for(name)
62
+ commands[(name || raise(CommandNotFound)).to_s.to_sym] || raise(CommandNotFound)
44
63
  end
45
64
 
46
65
  def commands
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
@@ -92,6 +92,7 @@ files:
92
92
  - lib/rack/app.rb
93
93
  - lib/rack/app/cli.rb
94
94
  - lib/rack/app/cli/command.rb
95
+ - lib/rack/app/cli/command/configurator.rb
95
96
  - lib/rack/app/cli/default_commands.rb
96
97
  - lib/rack/app/cli/default_commands/list_commands.rb
97
98
  - lib/rack/app/cli/runner.rb