cri 2.0.2 → 2.1.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.
data/NEWS.md CHANGED
@@ -1,6 +1,12 @@
1
1
  Cri News
2
2
  ========
3
3
 
4
+ 2.1.0
5
+ -----
6
+
7
+ * Added support for runners
8
+ * Split up local/global command options
9
+
4
10
  2.0.2
5
11
  -----
6
12
 
data/README.md CHANGED
@@ -109,6 +109,11 @@ The {Cri::CommandDSL#run} method takes a block with the actual code to
109
109
  execute. This block takes three arguments: the options, any arguments passed
110
110
  to the command, and the command itself.
111
111
 
112
+ Instead of defining a run block, it is possible to declare a class, the
113
+ _command runner_ class ({Cri::CommandRunner}) that will perform the actual
114
+ execution of the command. This makes it easier to break up large run blocks
115
+ into manageable pieces.
116
+
112
117
  Commands can have subcommands. For example, the `git` commandline tool would be represented by a command that has subcommands named `commit`, `add`, and so on. Commands with subcommands do not use a run block; execution will always be dispatched to a subcommand (or none, if no subcommand is found).
113
118
 
114
119
  To add a command as a subcommand to another command, use the {Cri::Command#add_command} method, like this:
data/lib/cri.rb CHANGED
@@ -17,10 +17,11 @@ module Cri
17
17
  end
18
18
 
19
19
  # The current Cri version.
20
- VERSION = '2.0.2'
20
+ VERSION = '2.1.0'
21
21
 
22
22
  autoload 'Command', 'cri/command'
23
23
  autoload 'CommandDSL', 'cri/command_dsl'
24
+ autoload 'CommandRunner', 'cri/command_runner'
24
25
  autoload 'OptionParser', 'cri/option_parser'
25
26
 
26
27
  end
@@ -317,18 +317,23 @@ module Cri
317
317
  end
318
318
 
319
319
  # Append options
320
- defs = global_option_definitions.sort { |x,y| x[:long] <=> y[:long] }
321
- unless defs.empty?
322
- text << "\n"
323
- text << "options:\n"
324
- text << "\n"
325
- length = defs.inject(0) { |m,o| [ m, o[:long].size ].max }
326
- defs.each do |opt_def|
327
- text << sprintf(
328
- " -%1s --%-#{length+4}s %s\n",
329
- opt_def[:short],
330
- opt_def[:long],
331
- opt_def[:desc])
320
+ groups = { 'options' => self.option_definitions }
321
+ if self.supercommand
322
+ groups["options for #{self.supercommand.name}"] = self.supercommand.global_option_definitions
323
+ end
324
+ length = groups.values.inject(&:+).inject(0) { |m,o| [ m, o[:long].size ].max }
325
+ groups.each_pair do |name, defs|
326
+ unless defs.empty?
327
+ text << "\n"
328
+ text << "#{name}:\n"
329
+ text << "\n"
330
+ defs.sort { |x,y| x[:long] <=> y[:long] }.each do |opt_def|
331
+ text << sprintf(
332
+ " -%1s --%-#{length+4}s %s\n",
333
+ opt_def[:short],
334
+ opt_def[:long],
335
+ opt_def[:desc])
336
+ end
332
337
  end
333
338
  end
334
339
 
@@ -149,6 +149,8 @@ module Cri
149
149
 
150
150
  # Sets the run block to the given block. The given block should have two
151
151
  # or three arguments (options, arguments, and optionally the command).
152
+ # Calling this will override existing run block or runner declarations
153
+ # (using {#run} and {#runner}, respectively).
152
154
  #
153
155
  # @return [void]
154
156
  def run(&block)
@@ -160,6 +162,20 @@ module Cri
160
162
  @command.block = block
161
163
  end
162
164
 
165
+ # Defines the runner class for this command. Calling this will override
166
+ # existing run block or runner declarations (using {#run} and {#runner},
167
+ # respectively).
168
+ #
169
+ # @param [Class<CommandRunner>] klass The command runner class (subclass
170
+ # of {CommandRunner}) that is used for executing this command.
171
+ #
172
+ # @return [void]
173
+ def runner(klass)
174
+ run do |opts, args, cmd|
175
+ klass.new(opts, args, cmd).call
176
+ end
177
+ end
178
+
163
179
  protected
164
180
 
165
181
  def add_option(short, long, desc, argument, block)
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ module Cri
4
+
5
+ # A command runner is responsible for the execution of a command. Using it
6
+ # is optional, but it is useful for commands whose execution block is large.
7
+ class CommandRunner
8
+
9
+ # @return [Hash] A hash contain the options and their values
10
+ attr_reader :options
11
+
12
+ # @return [Array] The list of arguments
13
+ attr_reader :arguments
14
+
15
+ # @return [Command] The command
16
+ attr_reader :command
17
+
18
+ # @param [Hash] options A hash contain the options and their values
19
+ #
20
+ # @param [Array] arguments The list of arguments
21
+ #
22
+ # @param [Cri::Command] command The Cri command
23
+ def initialize(options, arguments, command)
24
+ @options = options
25
+ @arguments = arguments
26
+ @command = command
27
+ end
28
+
29
+ # Runs the command. By default, this simply does the actual execution, but
30
+ # subclasses may choose to add error handling around the actual execution.
31
+ #
32
+ # @return [void]
33
+ def call
34
+ self.run
35
+ end
36
+
37
+ # Performs the actual execution of the command.
38
+ #
39
+ # @return [void]
40
+ #
41
+ # @abstract
42
+ def run
43
+ raise NotImplementedError, 'Cri::CommandRunner subclasses must implement #run'
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -75,4 +75,24 @@ class Cri::CommandDSLTestCase < Cri::TestCase
75
75
  assert_equal %w( aah moo ), command.aliases.sort
76
76
  end
77
77
 
78
+ def test_runner
79
+ # Define
80
+ dsl = Cri::CommandDSL.new
81
+ dsl.instance_eval <<-EOS
82
+ class Cri::CommandDSLTestCaseCommandRunner < Cri::CommandRunner
83
+ def run
84
+ $works = arguments[0]
85
+ end
86
+ end
87
+
88
+ runner Cri::CommandDSLTestCaseCommandRunner
89
+ EOS
90
+ command = dsl.command
91
+
92
+ # Check
93
+ $works = false
94
+ command.run(%w( certainly ))
95
+ assert_equal 'certainly', $works
96
+ end
97
+
78
98
  end
metadata CHANGED
@@ -1,30 +1,27 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: cri
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.0
4
5
  prerelease:
5
- version: 2.0.2
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Denis Defreyne
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-07-17 00:00:00 Z
12
+ date: 2012-01-16 00:00:00.000000000 Z
14
13
  dependencies: []
15
-
16
- description: Cri allows building easy-to-use commandline interfaces with support for subcommands.
14
+ description: Cri allows building easy-to-use commandline interfaces with support for
15
+ subcommands.
17
16
  email: denis.defreyne@stoneship.org
18
17
  executables: []
19
-
20
18
  extensions: []
21
-
22
- extra_rdoc_files:
19
+ extra_rdoc_files:
23
20
  - ChangeLog
24
21
  - LICENSE
25
22
  - README.md
26
23
  - NEWS.md
27
- files:
24
+ files:
28
25
  - ChangeLog
29
26
  - LICENSE
30
27
  - NEWS.md
@@ -32,6 +29,7 @@ files:
32
29
  - README.md
33
30
  - lib/cri/command.rb
34
31
  - lib/cri/command_dsl.rb
32
+ - lib/cri/command_runner.rb
35
33
  - lib/cri/commands/basic_help.rb
36
34
  - lib/cri/commands/basic_root.rb
37
35
  - lib/cri/core_ext/string.rb
@@ -49,31 +47,29 @@ files:
49
47
  - .gemtest
50
48
  homepage: http://stoneship.org/software/cri/
51
49
  licenses: []
52
-
53
50
  post_install_message:
54
- rdoc_options:
51
+ rdoc_options:
55
52
  - --main
56
53
  - README.md
57
- require_paths:
54
+ require_paths:
58
55
  - lib
59
- required_ruby_version: !ruby/object:Gem::Requirement
56
+ required_ruby_version: !ruby/object:Gem::Requirement
60
57
  none: false
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- version: "0"
65
- required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
63
  none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: "0"
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
71
68
  requirements: []
72
-
73
69
  rubyforge_project:
74
- rubygems_version: 1.8.5
70
+ rubygems_version: 1.8.10
75
71
  signing_key:
76
72
  specification_version: 3
77
73
  summary: a library for building easy-to-use commandline tools
78
74
  test_files: []
79
-
75
+ has_rdoc: