cliqr 1.2.0 → 2.0.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.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +95 -0
  3. data/README.md +9 -71
  4. data/examples/numbers +1 -2
  5. data/examples/vagrant +0 -3
  6. data/lib/cliqr.rb +52 -11
  7. data/lib/cliqr/argument_validation/argument_type_validator.rb +2 -2
  8. data/lib/cliqr/argument_validation/validator.rb +3 -3
  9. data/lib/cliqr/{cli → command}/argument_operator.rb +2 -2
  10. data/lib/cliqr/{cli → command}/argument_operator_context.rb +1 -1
  11. data/lib/cliqr/{cli/command.rb → command/base_command.rb} +2 -2
  12. data/lib/cliqr/command/color.rb +174 -0
  13. data/lib/cliqr/{cli → command}/command_context.rb +68 -20
  14. data/lib/cliqr/command/shell_banner_builder.rb +17 -0
  15. data/lib/cliqr/command/shell_command.rb +125 -0
  16. data/lib/cliqr/command/shell_prompt_builder.rb +26 -0
  17. data/lib/cliqr/config/action.rb +226 -0
  18. data/lib/cliqr/config/base.rb +84 -0
  19. data/lib/cliqr/config/command.rb +137 -0
  20. data/lib/cliqr/config/dsl.rb +81 -0
  21. data/lib/cliqr/config/event.rb +43 -0
  22. data/lib/cliqr/config/event_based.rb +78 -0
  23. data/lib/cliqr/config/named.rb +55 -0
  24. data/lib/cliqr/config/option.rb +95 -0
  25. data/lib/cliqr/config/option_based.rb +130 -0
  26. data/lib/cliqr/config/shell.rb +87 -0
  27. data/lib/cliqr/config/validation/validation_set.rb +66 -0
  28. data/lib/cliqr/config/validation/validator_factory.rb +403 -0
  29. data/lib/cliqr/config/validation/verifiable.rb +91 -0
  30. data/lib/cliqr/error.rb +20 -4
  31. data/lib/cliqr/events/event.rb +56 -0
  32. data/lib/cliqr/events/event_context.rb +31 -0
  33. data/lib/cliqr/events/handler.rb +32 -0
  34. data/lib/cliqr/events/invoker.rb +70 -0
  35. data/lib/cliqr/{cli → executor}/command_runner_factory.rb +3 -3
  36. data/lib/cliqr/{cli → executor}/router.rb +4 -4
  37. data/lib/cliqr/{cli/executor.rb → executor/runner.rb} +25 -10
  38. data/lib/cliqr/interface.rb +98 -0
  39. data/lib/cliqr/parser/token_factory.rb +1 -1
  40. data/lib/cliqr/usage/command_usage_context.rb +94 -0
  41. data/lib/cliqr/usage/option_usage_context.rb +86 -0
  42. data/lib/cliqr/usage/templates/partial/action_list.erb +10 -0
  43. data/lib/cliqr/usage/templates/partial/command_name.erb +3 -0
  44. data/lib/cliqr/usage/templates/partial/option_list.erb +18 -0
  45. data/lib/cliqr/usage/templates/partial/usage_info.erb +5 -0
  46. data/lib/cliqr/usage/templates/usage/cli.erb +4 -0
  47. data/lib/cliqr/usage/templates/usage/shell.erb +2 -0
  48. data/lib/cliqr/usage/usage_builder.rb +59 -0
  49. data/lib/cliqr/util.rb +81 -34
  50. data/lib/cliqr/version.rb +1 -1
  51. data/spec/config/action_config_validator_spec.rb +127 -5
  52. data/spec/config/config_finalize_spec.rb +3 -3
  53. data/spec/config/config_validator_spec.rb +120 -17
  54. data/spec/config/option_config_validator_spec.rb +1 -1
  55. data/spec/dsl/interface_spec.rb +2 -2
  56. data/spec/dsl/usage_spec.rb +461 -465
  57. data/spec/executor/action_executor_spec.rb +1 -1
  58. data/spec/executor/color_executor_spec.rb +125 -0
  59. data/spec/executor/command_runner_spec.rb +6 -8
  60. data/spec/executor/event_executor_spec.rb +365 -0
  61. data/spec/executor/executor_spec.rb +49 -11
  62. data/spec/executor/help_executor_spec.rb +107 -103
  63. data/spec/fixtures/action_reader_command.rb +1 -1
  64. data/spec/fixtures/test_arg_printer_event_handler.rb +9 -0
  65. data/spec/fixtures/test_color_shell_prompt.rb +13 -0
  66. data/spec/fixtures/test_empty_event_handler.rb +5 -0
  67. data/spec/fixtures/test_invoker_event_handler.rb +9 -0
  68. data/spec/fixtures/test_shell_banner.rb +8 -0
  69. data/spec/fixtures/test_shell_prompt.rb +13 -0
  70. data/spec/shell/shell_executor_spec.rb +700 -0
  71. data/spec/validation/validation_spec.rb +2 -2
  72. metadata +65 -27
  73. data/lib/cliqr/cli/config.rb +0 -554
  74. data/lib/cliqr/cli/interface.rb +0 -107
  75. data/lib/cliqr/cli/shell_command.rb +0 -69
  76. data/lib/cliqr/cli/usage_builder.rb +0 -185
  77. data/lib/cliqr/config_validation/validation_set.rb +0 -48
  78. data/lib/cliqr/config_validation/validator_factory.rb +0 -319
  79. data/lib/cliqr/config_validation/verifiable.rb +0 -89
  80. data/lib/cliqr/dsl.rb +0 -59
  81. data/spec/executor/shell_executor_spec.rb +0 -233
  82. data/templates/usage.erb +0 -39
@@ -2,10 +2,10 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Cliqr::ConfigValidation do
5
+ describe Cliqr::Config::Validation do
6
6
  it 'does not know how to validate unknown types' do
7
7
  expect do
8
- Cliqr::ConfigValidation::ValidatorFactory.get(:bla, {}).validate(nil, nil, nil)
8
+ Cliqr::Config::Validation::ValidatorFactory.get(:bla, {}).validate(nil, nil, nil)
9
9
  end.to raise_error(Cliqr::Error::UnknownValidatorType, "unknown validation type: 'bla'")
10
10
  end
11
11
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cliqr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anshul Verma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-18 00:00:00.000000000 Z
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: log4r
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.6'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.6'
41
41
  description: |2
@@ -63,22 +63,36 @@ files:
63
63
  - lib/cliqr/argument_validation/argument_type_validator.rb
64
64
  - lib/cliqr/argument_validation/option_validator.rb
65
65
  - lib/cliqr/argument_validation/validator.rb
66
- - lib/cliqr/cli/argument_operator.rb
67
- - lib/cliqr/cli/argument_operator_context.rb
68
- - lib/cliqr/cli/command.rb
69
- - lib/cliqr/cli/command_context.rb
70
- - lib/cliqr/cli/command_runner_factory.rb
71
- - lib/cliqr/cli/config.rb
72
- - lib/cliqr/cli/executor.rb
73
- - lib/cliqr/cli/interface.rb
74
- - lib/cliqr/cli/router.rb
75
- - lib/cliqr/cli/shell_command.rb
76
- - lib/cliqr/cli/usage_builder.rb
77
- - lib/cliqr/config_validation/validation_set.rb
78
- - lib/cliqr/config_validation/validator_factory.rb
79
- - lib/cliqr/config_validation/verifiable.rb
80
- - lib/cliqr/dsl.rb
66
+ - lib/cliqr/command/argument_operator.rb
67
+ - lib/cliqr/command/argument_operator_context.rb
68
+ - lib/cliqr/command/base_command.rb
69
+ - lib/cliqr/command/color.rb
70
+ - lib/cliqr/command/command_context.rb
71
+ - lib/cliqr/command/shell_banner_builder.rb
72
+ - lib/cliqr/command/shell_command.rb
73
+ - lib/cliqr/command/shell_prompt_builder.rb
74
+ - lib/cliqr/config/action.rb
75
+ - lib/cliqr/config/base.rb
76
+ - lib/cliqr/config/command.rb
77
+ - lib/cliqr/config/dsl.rb
78
+ - lib/cliqr/config/event.rb
79
+ - lib/cliqr/config/event_based.rb
80
+ - lib/cliqr/config/named.rb
81
+ - lib/cliqr/config/option.rb
82
+ - lib/cliqr/config/option_based.rb
83
+ - lib/cliqr/config/shell.rb
84
+ - lib/cliqr/config/validation/validation_set.rb
85
+ - lib/cliqr/config/validation/validator_factory.rb
86
+ - lib/cliqr/config/validation/verifiable.rb
81
87
  - lib/cliqr/error.rb
88
+ - lib/cliqr/events/event.rb
89
+ - lib/cliqr/events/event_context.rb
90
+ - lib/cliqr/events/handler.rb
91
+ - lib/cliqr/events/invoker.rb
92
+ - lib/cliqr/executor/command_runner_factory.rb
93
+ - lib/cliqr/executor/router.rb
94
+ - lib/cliqr/executor/runner.rb
95
+ - lib/cliqr/interface.rb
82
96
  - lib/cliqr/parser/action_token.rb
83
97
  - lib/cliqr/parser/argument_parser.rb
84
98
  - lib/cliqr/parser/argument_token.rb
@@ -90,6 +104,15 @@ files:
90
104
  - lib/cliqr/parser/single_valued_option_token.rb
91
105
  - lib/cliqr/parser/token.rb
92
106
  - lib/cliqr/parser/token_factory.rb
107
+ - lib/cliqr/usage/command_usage_context.rb
108
+ - lib/cliqr/usage/option_usage_context.rb
109
+ - lib/cliqr/usage/templates/partial/action_list.erb
110
+ - lib/cliqr/usage/templates/partial/command_name.erb
111
+ - lib/cliqr/usage/templates/partial/option_list.erb
112
+ - lib/cliqr/usage/templates/partial/usage_info.erb
113
+ - lib/cliqr/usage/templates/usage/cli.erb
114
+ - lib/cliqr/usage/templates/usage/shell.erb
115
+ - lib/cliqr/usage/usage_builder.rb
93
116
  - lib/cliqr/util.rb
94
117
  - lib/cliqr/validation_errors.rb
95
118
  - lib/cliqr/version.rb
@@ -101,21 +124,29 @@ files:
101
124
  - spec/dsl/interface_spec.rb
102
125
  - spec/dsl/usage_spec.rb
103
126
  - spec/executor/action_executor_spec.rb
127
+ - spec/executor/color_executor_spec.rb
104
128
  - spec/executor/command_runner_spec.rb
129
+ - spec/executor/event_executor_spec.rb
105
130
  - spec/executor/executor_spec.rb
106
131
  - spec/executor/help_executor_spec.rb
107
- - spec/executor/shell_executor_spec.rb
108
132
  - spec/fixtures/action_reader_command.rb
109
133
  - spec/fixtures/always_error_command.rb
110
134
  - spec/fixtures/argument_reader_command.rb
111
135
  - spec/fixtures/csv_argument_operator.rb
112
136
  - spec/fixtures/option_reader_command.rb
137
+ - spec/fixtures/test_arg_printer_event_handler.rb
138
+ - spec/fixtures/test_color_shell_prompt.rb
113
139
  - spec/fixtures/test_command.rb
140
+ - spec/fixtures/test_empty_event_handler.rb
141
+ - spec/fixtures/test_invoker_event_handler.rb
114
142
  - spec/fixtures/test_option_checker_command.rb
115
143
  - spec/fixtures/test_option_reader_command.rb
116
144
  - spec/fixtures/test_option_type_checker_command.rb
145
+ - spec/fixtures/test_shell_banner.rb
146
+ - spec/fixtures/test_shell_prompt.rb
117
147
  - spec/parser/action_argument_parser_spec.rb
118
148
  - spec/parser/argument_parser_spec.rb
149
+ - spec/shell/shell_executor_spec.rb
119
150
  - spec/spec_helper.rb
120
151
  - spec/validation/action_argument_validator_spec.rb
121
152
  - spec/validation/command_argument_validation_spec.rb
@@ -124,7 +155,6 @@ files:
124
155
  - tasks/rdoc.rake
125
156
  - tasks/rubucop.rake
126
157
  - tasks/yard.rake
127
- - templates/usage.erb
128
158
  homepage: https://github.com/anshulverma/cliqr
129
159
  licenses:
130
160
  - MIT
@@ -135,17 +165,17 @@ require_paths:
135
165
  - lib
136
166
  required_ruby_version: !ruby/object:Gem::Requirement
137
167
  requirements:
138
- - - ">="
168
+ - - '>='
139
169
  - !ruby/object:Gem::Version
140
170
  version: 1.9.3
141
171
  required_rubygems_version: !ruby/object:Gem::Requirement
142
172
  requirements:
143
- - - ">="
173
+ - - '>='
144
174
  - !ruby/object:Gem::Version
145
175
  version: 1.3.6
146
176
  requirements: []
147
177
  rubyforge_project:
148
- rubygems_version: 2.2.2
178
+ rubygems_version: 2.4.6
149
179
  signing_key:
150
180
  specification_version: 4
151
181
  summary: A framework and DSL for defining CLI interface
@@ -158,21 +188,29 @@ test_files:
158
188
  - spec/dsl/interface_spec.rb
159
189
  - spec/dsl/usage_spec.rb
160
190
  - spec/executor/action_executor_spec.rb
191
+ - spec/executor/color_executor_spec.rb
161
192
  - spec/executor/command_runner_spec.rb
193
+ - spec/executor/event_executor_spec.rb
162
194
  - spec/executor/executor_spec.rb
163
195
  - spec/executor/help_executor_spec.rb
164
- - spec/executor/shell_executor_spec.rb
165
196
  - spec/fixtures/action_reader_command.rb
166
197
  - spec/fixtures/always_error_command.rb
167
198
  - spec/fixtures/argument_reader_command.rb
168
199
  - spec/fixtures/csv_argument_operator.rb
169
200
  - spec/fixtures/option_reader_command.rb
201
+ - spec/fixtures/test_arg_printer_event_handler.rb
202
+ - spec/fixtures/test_color_shell_prompt.rb
170
203
  - spec/fixtures/test_command.rb
204
+ - spec/fixtures/test_empty_event_handler.rb
205
+ - spec/fixtures/test_invoker_event_handler.rb
171
206
  - spec/fixtures/test_option_checker_command.rb
172
207
  - spec/fixtures/test_option_reader_command.rb
173
208
  - spec/fixtures/test_option_type_checker_command.rb
209
+ - spec/fixtures/test_shell_banner.rb
210
+ - spec/fixtures/test_shell_prompt.rb
174
211
  - spec/parser/action_argument_parser_spec.rb
175
212
  - spec/parser/argument_parser_spec.rb
213
+ - spec/shell/shell_executor_spec.rb
176
214
  - spec/spec_helper.rb
177
215
  - spec/validation/action_argument_validator_spec.rb
178
216
  - spec/validation/command_argument_validation_spec.rb
@@ -1,554 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'cliqr/dsl'
4
- require 'cliqr/util'
5
- require 'cliqr/config_validation/verifiable'
6
- require 'cliqr/cli/command'
7
- require 'cliqr/cli/argument_operator'
8
-
9
- module Cliqr
10
- # A extension for CLI module to group all config classes
11
- module CLI
12
- # A value to initialize configuration attributes with
13
- UNSET = Object.new
14
-
15
- # Configuration option to enable arguments for a command (default)
16
- ENABLE_CONFIG = :enable
17
-
18
- # Configuration option to disable arguments for a command
19
- DISABLE_CONFIG = :disable
20
-
21
- # Option type for regular options
22
- ANY_ARGUMENT_TYPE = :any
23
-
24
- # Option type for numeric arguments
25
- NUMERIC_ARGUMENT_TYPE = :numeric
26
-
27
- # Option type for boolean arguments
28
- BOOLEAN_ARGUMENT_TYPE = :boolean
29
-
30
- # Default values based on argument type
31
- ARGUMENT_DEFAULTS = {
32
- NUMERIC_ARGUMENT_TYPE => 0,
33
- BOOLEAN_ARGUMENT_TYPE => false,
34
- ANY_ARGUMENT_TYPE => nil
35
- }
36
-
37
- # The configuration setting to build a cli application with its own dsl
38
- #
39
- # @api private
40
- class Config
41
- extend Cliqr::DSL
42
- include Cliqr::ConfigValidation
43
-
44
- # Name of the command
45
- #
46
- # @return [String]
47
- attr_accessor :name
48
- validates :name,
49
- non_empty_format: /^[a-zA-Z0-9_\-]+$/
50
-
51
- # Description for the base command
52
- #
53
- # @return [String]
54
- attr_accessor :description
55
-
56
- # Command handler for the base command
57
- #
58
- # @return [Class<Cliqr::CLI::Command>]
59
- attr_accessor :handler
60
- validates :handler,
61
- one_of: {
62
- extend: Cliqr::CLI::Command,
63
- type_of: Proc
64
- }
65
-
66
- # Dictates whether this command can take arbitrary arguments (optional)
67
- #
68
- # @return [Symbol] Either <tt>#ENABLE_CONFIG</tt> or <tt>#DISABLE_CONFIG</tt>
69
- attr_accessor :arguments
70
- validates :arguments,
71
- inclusion: [ENABLE_CONFIG, DISABLE_CONFIG]
72
-
73
- # Array of options applied to the base command
74
- #
75
- # @return [Array<OptionConfig>]
76
- attr_accessor :options
77
- validates :options,
78
- collection: true
79
-
80
- # Array of children action configs for this action
81
- #
82
- # @return [Array<Cliqr::CLI::Config>]
83
- attr_accessor :actions
84
- validates :actions,
85
- collection: true
86
-
87
- # Enable or disable help command and option
88
- #
89
- # @return [Symbol] Either <tt>#ENABLE_CONFIG</tt> or <tt>#DISABLE_CONFIG</tt>
90
- attr_accessor :help
91
- validates :help,
92
- inclusion: [ENABLE_CONFIG, DISABLE_CONFIG]
93
-
94
- # Enable or disable the shell action for base config
95
- #
96
- # @return [Symbol] Either <tt>#ENABLE_CONFIG</tt> or <tt>#DISABLE_CONFIG</tt>
97
- attr_accessor :shell
98
- validates :shell,
99
- inclusion: [ENABLE_CONFIG, DISABLE_CONFIG]
100
-
101
- # Version tag for this configuration
102
- #
103
- # @return [Stirng]
104
- attr_accessor :version
105
-
106
- # Parent configuration
107
- #
108
- # @return [Cliqr::CLI::Config]
109
- attr_accessor :parent
110
-
111
- # Root config
112
- #
113
- # @return [Cliqr::CLI::Config]
114
- attr_accessor :root
115
-
116
- # New config instance with all attributes set as UNSET
117
- def initialize
118
- @name = UNSET
119
- @description = UNSET
120
- @handler = UNSET
121
- @arguments = UNSET
122
- @help = UNSET
123
- @shell = UNSET
124
- @version = UNSET
125
-
126
- @options = []
127
- @option_index = {}
128
-
129
- @actions = []
130
- @action_index = {}
131
- end
132
-
133
- # Finalize config by adding default values for unset values
134
- #
135
- # @return [Cliqr::CLI::Config]
136
- def finalize
137
- @name = Config.get_if_unset(@name, '')
138
- @description = Config.get_if_unset(@description, '')
139
- @handler = Util.ensure_instance(Config.get_if_unset(@handler, nil))
140
- @arguments = Config.get_if_unset(@arguments, ENABLE_CONFIG)
141
- @help = Config.get_if_unset(@help, ENABLE_CONFIG)
142
- @root = self
143
- @shell = Config.get_if_unset(@shell, shell_default)
144
- @version = Config.get_if_unset(@version, nil)
145
-
146
- self
147
- end
148
-
149
- # Set up default attributes for this configuration
150
- #
151
- # @return [Cliqr::CLI::Config] Update config
152
- def setup_defaults
153
- add_shell
154
- add_version
155
- add_help
156
- @handler = Cliqr::Util.forward_to_help_handler if @handler.nil? && help? && actions?
157
- @actions.each(&:setup_defaults)
158
- end
159
-
160
- # Set value for a config option
161
- #
162
- # @param [Symbol] name Name of the config parameter
163
- # @param [Object] value Value for the config parameter
164
- # @param [Proc] block Function which populates configuration for a sub-attribute
165
- #
166
- # @return [Object] if setting a attribute's value
167
- # @return [Cliqr::CLI::OptionConfig] if adding a new option
168
- # @return [Cliqr::CLI::Config] if adding a new action
169
- def set_config(name, value, &block)
170
- case name
171
- when :option
172
- handle_option value, &block # value is the long name for the option
173
- when :action
174
- handle_action value, &block # value is action's name
175
- else
176
- value = block if block_given?
177
- handle_config name, value
178
- end
179
- end
180
-
181
- # Check if options are set
182
- #
183
- # @return [Boolean] <tt>true</tt> if the CLI config's options have been set
184
- def options?
185
- @options != UNSET
186
- end
187
-
188
- # Check if particular option is set
189
- #
190
- # @param [String] name Name of the option to check
191
- #
192
- # @return [Boolean] <tt>true</tt> if the CLI config's option is set
193
- def option?(name)
194
- @option_index.key?(name.to_s)
195
- end
196
-
197
- # Get value of a option
198
- #
199
- # @param [String] name Name of the option
200
- #
201
- # @return [String] value for the option
202
- def option(name)
203
- @option_index[name.to_s]
204
- end
205
-
206
- # Check if particular action exists
207
- #
208
- # @param [String] name Name of the action to check
209
- #
210
- # @return [Boolean] <tt>true</tt> if the action exists in the configuration
211
- def action?(name)
212
- return false if name.nil?
213
- @action_index.key?(name.to_sym)
214
- end
215
-
216
- # Check if this config has sub actions
217
- #
218
- # @return [Array<Cliqr::CLI::Config]>
219
- def actions?
220
- !@actions.empty?
221
- end
222
-
223
- # Get action config by name
224
- #
225
- # @param [String] name Name of the action
226
- #
227
- # @return [Cliqr::CLI::Config] Configuration of the action
228
- def action(name)
229
- @action_index[name.to_sym]
230
- end
231
-
232
- # Check if arguments are enabled for this configuration
233
- #
234
- # @return [Boolean] <tt>true</tt> if arguments are enabled
235
- def arguments?
236
- @arguments == ENABLE_CONFIG
237
- end
238
-
239
- # Get name of the command along with the action upto this config
240
- #
241
- # @return [String] Serialized command name
242
- def command
243
- return name unless parent?
244
- "#{@parent.command} #{name}"
245
- end
246
-
247
- # Check if this config has a parent config
248
- #
249
- # @return [Boolean] <tt>true</tt> if there exists a parent action for this action
250
- def parent?
251
- !@parent.nil?
252
- end
253
-
254
- # Check if help is enabled for this command
255
- #
256
- # @return [Boolean] <tt>true</tt> if help is enabled
257
- def help?
258
- @help == ENABLE_CONFIG
259
- end
260
-
261
- # Check if version is enabled for this command
262
- #
263
- # @return [Boolean] <tt>true</tt> if help is enabled
264
- def version?
265
- !@version.nil?
266
- end
267
-
268
- # Check if this is the root config
269
- #
270
- # @return [Boolean]
271
- def root?
272
- self == @root
273
- end
274
-
275
- # Check if this configuration has shell action enabled
276
- #
277
- # @return [Boolean]
278
- def shell?
279
- @shell == ENABLE_CONFIG
280
- end
281
-
282
- private
283
-
284
- # Set value for config option without evaluating a block
285
- #
286
- # @param [Symbol] name Name of the config option
287
- # @param [Object] value Value for the config option
288
- #
289
- # @return [Object] Value that was assigned to attribute
290
- def handle_config(name, value)
291
- public_send("#{name}=", value)
292
- value
293
- end
294
-
295
- # Handle configuration for a new option
296
- #
297
- # @param [Symbol] name Long name of the option
298
- # @param [Proc] block Populate the option's config in this funciton block
299
- #
300
- # @return [Cliqr::CLI::OptionConfig] Newly created option's config
301
- def handle_option(name, &block)
302
- option_config = OptionConfig.build(&block)
303
- option_config.name = name
304
- add_option(option_config)
305
- end
306
-
307
- # Add a new option for the command
308
- #
309
- # @return [Cliqr::CLI::OptionConfig] Newly added option's config
310
- def add_option(option_config)
311
- validate_option_name(option_config)
312
-
313
- @options.push(option_config)
314
- @option_index[option_config.name.to_s] = option_config
315
- @option_index[option_config.short.to_s] = option_config if option_config.short?
316
-
317
- option_config
318
- end
319
-
320
- # Handle configuration for a new action
321
- #
322
- # @param [String] name Name of the action
323
- # @param [Function] block The block which configures this action
324
- #
325
- # @return [Cliqr::CLI::Config] The newly configured action
326
- def handle_action(name, &block)
327
- action_config = Config.build(&block)
328
- action_config.name = name
329
- add_action(action_config)
330
- end
331
-
332
- # Add a new action
333
- #
334
- # @return [Cliqr::CLI::Config] The newly added action
335
- def add_action(action_config)
336
- action_config.parent = self
337
- action_config.root = root
338
-
339
- validate_action_name(action_config)
340
-
341
- @actions.push(action_config)
342
- @action_index[action_config.name.to_sym] = action_config \
343
- unless action_config.name.nil?
344
-
345
- action_config
346
- end
347
-
348
- # Make sure that the option's name is unique
349
- #
350
- # @param [Cliqr::CLI::OptionConfig] option_config Config for this particular option
351
- #
352
- # @return [Cliqr::CLI::OptionConfig] Validated OptionConfig instance
353
- def validate_option_name(option_config)
354
- fail Cliqr::Error::DuplicateOptions,
355
- "multiple options with long name \"#{option_config.name}\"" \
356
- if option?(option_config.name)
357
-
358
- fail Cliqr::Error::DuplicateOptions,
359
- "multiple options with short name \"#{option_config.short}\"" \
360
- if option?(option_config.short)
361
-
362
- option_config
363
- end
364
-
365
- # Make sure that the action's name is unique
366
- #
367
- # @param [Cliqr::CLI::Config] action_config Config for this particular action
368
- #
369
- # @return [Cliqr::CLI::Config] Validated action's Config instance
370
- def validate_action_name(action_config)
371
- fail Cliqr::Error::DuplicateActions,
372
- "multiple actions named \"#{action_config.name}\"" \
373
- if action?(action_config.name)
374
-
375
- action_config
376
- end
377
-
378
- # Add help command and option to this config
379
- #
380
- # @return [Cliqr::CLI::Config] Updated config
381
- def add_help
382
- return self unless help?
383
- add_action(Cliqr::Util.build_help_action(self)) unless action?('help')
384
- add_option(Cliqr::Util.build_help_option(self)) unless option?('help')
385
- end
386
-
387
- # Add version command and option to this config
388
- #
389
- # @return [Cliqr::CLI::Config] Updated config
390
- def add_version
391
- return self unless version?
392
- add_action(Cliqr::Util.build_version_action(self)) unless action?('version')
393
- add_option(Cliqr::Util.build_version_option(self)) unless option?('version')
394
- end
395
-
396
- # Add shell command
397
- #
398
- # @return [Cliqr::CLI::Config] Updated config
399
- def add_shell
400
- return self unless shell?
401
- add_action(Cliqr::Util.build_shell_action(self)) unless action?('shell')
402
- end
403
-
404
- # Get default setting for shell attribute
405
- #
406
- # @return [Symbol]
407
- def shell_default
408
- root? && actions? ? ENABLE_CONFIG : DISABLE_CONFIG
409
- end
410
-
411
- # Get the passed param value if current attribute is unset
412
- #
413
- # @return [Object]
414
- def self.get_if_unset(attribute_value, default_value)
415
- attribute_value == UNSET ? default_value : attribute_value
416
- end
417
- end
418
-
419
- # Config attributes for a command's option
420
- #
421
- # @api private
422
- class OptionConfig
423
- extend Cliqr::DSL
424
- include Cliqr::ConfigValidation
425
-
426
- # Long option name
427
- #
428
- # @return [String]
429
- attr_accessor :name
430
- validates :name,
431
- non_empty: true,
432
- format: /^[a-zA-Z0-9_\-]*$/
433
-
434
- # Optional short name for the option
435
- #
436
- # @return [String]
437
- attr_accessor :short
438
- validates :short,
439
- non_empty_nil_ok_format: /^[a-z0-9A-Z]$/
440
-
441
- # A description string for the option
442
- #
443
- # @return [String]
444
- attr_accessor :description
445
-
446
- # Optional field that restricts values of this option to a certain type
447
- #
448
- # @return [Symbol] Type of the option
449
- attr_accessor :type
450
- validates :type,
451
- inclusion: [:any, NUMERIC_ARGUMENT_TYPE, BOOLEAN_ARGUMENT_TYPE]
452
-
453
- # Operation to be applied to the option value after validation
454
- #
455
- # @return [Class<Cliqr::CLI::ArgumentOperator>]
456
- attr_accessor :operator
457
- validates :operator,
458
- one_of: {
459
- extend: Cliqr::CLI::ArgumentOperator,
460
- type_of: Proc
461
- }
462
-
463
- # Default value for this option
464
- #
465
- # @return [Object]
466
- attr_accessor :default
467
-
468
- # Set value for command option's attribute
469
- #
470
- # @param [Symbol] name Name of the attribute
471
- # @param [Object] value Value for the attribute
472
- # @param [Proc] block A anonymous block to initialize the config value
473
- #
474
- # @return [Object] Value that was set for the attribute
475
- def set_config(name, value, &block)
476
- value = block if block_given?
477
- handle_option_config name, value
478
- end
479
-
480
- # Initialize a new config instance for an option with UNSET attribute values
481
- def initialize
482
- @name = UNSET
483
- @short = UNSET
484
- @description = UNSET
485
- @type = UNSET
486
- @operator = UNSET
487
- @default = UNSET
488
- end
489
-
490
- # Finalize option's config by adding default values for unset values
491
- #
492
- # @return [Cliqr::CLI::OptionConfig]
493
- def finalize
494
- @name = Config.get_if_unset(@name, nil)
495
- @short = Config.get_if_unset(@short, nil)
496
- @description = Config.get_if_unset(@description, nil)
497
- @type = Config.get_if_unset(@type, ANY_ARGUMENT_TYPE)
498
- @operator = Util.ensure_instance(Config.get_if_unset(@operator,
499
- ArgumentOperator.for_type(@type)))
500
- @default = Config.get_if_unset(@default, ARGUMENT_DEFAULTS[@type])
501
-
502
- self
503
- end
504
-
505
- # Check if a option's short name is defined
506
- #
507
- # @return [Boolean] <tt>true</tt> if options' short name is not null neither empty
508
- def short?
509
- !(@short.nil? || @short.empty?)
510
- end
511
-
512
- # Check if a option's description is defined
513
- #
514
- # @return [Boolean] <tt>true</tt> if options' description is not null neither empty
515
- def description?
516
- !(@description.nil? || @description.empty?)
517
- end
518
-
519
- # Check if a option's type is defined
520
- #
521
- # @return [Boolean] <tt>true</tt> if options' type is not nil and not equal to <tt>:any</tt>
522
- def type?
523
- !@type.nil? && @type != :any
524
- end
525
-
526
- # Check if a option is of boolean type
527
- #
528
- # @return [Boolean] <tt>true</tt> is the option is of type <tt>:boolean</tt>
529
- def boolean?
530
- @type == :boolean
531
- end
532
-
533
- # Check if a default value setting is defined
534
- #
535
- # @return [Boolean]
536
- def default?
537
- !@default.nil?
538
- end
539
-
540
- private
541
-
542
- # Set value for config option without evaluating a block
543
- #
544
- # @param [Symbol] name Name of the config option
545
- # @param [Object] value Value for the config option
546
- #
547
- # @return [Object]
548
- def handle_option_config(name, value)
549
- public_send("#{name}=", value)
550
- value
551
- end
552
- end
553
- end
554
- end