cliqr 2.1.1 → 2.2.0

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: 2892f4cce1901d6018999df394a46e27d591af4b
4
- data.tar.gz: 709e7b21684d0f463382e4c3b1c49fd59b874bb9
3
+ metadata.gz: aa7b636b8c96c46131bce26dc5019a0ab0a1cadc
4
+ data.tar.gz: a7a236dd9d2c144b271e132e8520c75cac921094
5
5
  SHA512:
6
- metadata.gz: e11d059ede7010a84baa7dfe33ccf85b1776534c2f5b9ca87d6840519e376c46feca6c84d48ce69d9b1c86316623e548bf34f054e32521aa2344916a3b3fb588
7
- data.tar.gz: 3e340c1aff3fc1a664345d08406ad8e11b1b5ab7178f9e22fe31ad743aa8da8949b88ead8eda1ed67c326e0dd7b10d06a23e76e58411acca62a428fb6a2af793
6
+ metadata.gz: 64435fe4b4bb4c5eebd23452c091d474f282eea28d997d11d3f81a52404bab76ba12c3b52f4f8b5a551f13704b2285a74a3b1860274891cf7b0c6d9ca57d0e9e
7
+ data.tar.gz: c0134be11cb243f3073277f745719c4376be96b33a876b0c9cfb5d7b99ff1ce7cd49cf41065bdef48b0d7d14e88ddb13da22b80d1e56c6cf831e9acdf9b4c1d1
@@ -5,6 +5,17 @@ item in this nested table for further details.
5
5
  <!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc/generate-toc again -->
6
6
  **Table of Contents**
7
7
 
8
+ - [2.2.0 / 2015-07-28](#220--2015-07-28)
9
+ - [Features](#features)
10
+ - [Ability to hook into sawaal for selection](#ability-to-hook-into-sawaal-for-selection)
11
+ - [Ability to specify multiple values for an option](#ability-to-specify-multiple-values-for-an-option)
12
+ - [Compatibility upgrades](#compatibility-upgrades)
13
+ - [Not supporting ruby versions 1.9.3 and jruby-19mode](#not-supporting-ruby-versions-193-and-jruby-19mode)
14
+ - [Bugfix](#bugfix)
15
+ - [Make sure specs are executing in CI](#make-sure-specs-are-executing-in-ci)
16
+ - [Upgrade and fix style issues in rubocop](#upgrade-and-fix-style-issues-in-rubocop)
17
+ - [Minor Improvements](#minor-improvements)
18
+ - [Enable all auto fixable rubocops](#enable-all-auto-fixable-rubocops)
8
19
  - [2.1.1 / 2015-07-15](#211--2015-07-15)
9
20
  - [Bugfix](#bugfix)
10
21
  - [2.1.0 / 2015-07-15](#210--2015-07-15)
@@ -118,6 +129,38 @@ item in this nested table for further details.
118
129
 
119
130
  <!-- markdown-toc end -->
120
131
 
132
+
133
+ 2.2.0 / 2015-07-28
134
+ ==================
135
+
136
+ Some shiny new features coming in this release.
137
+
138
+ ## Features
139
+
140
+ ### Ability to hook into sawaal for selection
141
+
142
+ `[Sawaal](https://github.com/anshulverma/sawaal)` allows us to allow
143
+ users to use arrow keys to make selection in a list of items.
144
+
145
+ ### Ability to specify multiple values for an option
146
+
147
+ Now we can specify multiple values for a option. Simple set
148
+ `multi_valued` property to true for a option to make it multi_valued.
149
+
150
+ ## Compatibility upgrades
151
+
152
+ ### Not supporting ruby versions 1.9.3 and jruby-19mode
153
+
154
+ ## Bugfix
155
+
156
+ ### Make sure specs are executing in CI
157
+
158
+ ### Upgrade and fix style issues in rubocop
159
+
160
+ ## Minor Improvements
161
+
162
+ ### Enable all auto fixable rubocops
163
+
121
164
  2.1.1 / 2015-07-15
122
165
  ==================
123
166
 
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ FileList['tasks/*.rake'].each(&method(:import))
13
13
  desc 'run code coverage checker'
14
14
  task :coverage do
15
15
  ENV['COVERAGE'] = 'true'
16
- Rake::Task['spec'].execute
16
+ Rake::Task['spec'].invoke
17
17
  end
18
18
 
19
19
  desc 'default rake task'
@@ -22,7 +22,7 @@ task default: [:clean, :coverage, :rubocop, :verify_measurements, :yardstick_mea
22
22
  desc 'run CI tasks'
23
23
  task :ci do
24
24
  ENV['CI'] = 'true'
25
- Rake::Task['default'].execute
25
+ Rake::Task['default'].invoke
26
26
  end
27
27
 
28
28
  desc 'Load gem inside irb console'
@@ -18,8 +18,10 @@ module Cliqr
18
18
  # Run all the validators for a option
19
19
  #
20
20
  # @return [Cliqr::ValidationErrors]
21
- def validate(argument, option, errors)
22
- @validators.each { |validator| validator.validate(argument, option, errors) }
21
+ def validate(values, option, errors)
22
+ @validators.each do |validator|
23
+ values.each { |value| validator.validate(value, option, errors) }
24
+ end
23
25
  errors
24
26
  end
25
27
  end
@@ -39,9 +39,9 @@ module Cliqr
39
39
  # Validate a argument for an option and return errors
40
40
  #
41
41
  # @return [Cliqr::ValidationErrors]
42
- def validate_argument(argument, option, errors)
42
+ def validate_argument(values, option, errors)
43
43
  option_validator = get_option_validator(option)
44
- option_validator.validate(argument, option, errors)
44
+ option_validator.validate(values, option, errors)
45
45
  errors
46
46
  end
47
47
 
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'sawaal'
4
+
3
5
  require 'cliqr/command/color'
4
6
  require 'cliqr/command/argument_operator_context'
5
7
  require 'cliqr/events/invoker'
@@ -119,6 +121,13 @@ module Cliqr
119
121
  @executor.call(args, options)
120
122
  end
121
123
 
124
+ # Run the [Sawaal] selector on a set of options
125
+ #
126
+ # @return [Object] Selected key
127
+ def ask(question, options)
128
+ Sawaal.select(question, options)
129
+ end
130
+
122
131
  # Invoke an event
123
132
  #
124
133
  # @return [Boolean] <tt>true</tt> if the event was handled by any event handler
@@ -139,7 +148,7 @@ module Cliqr
139
148
  def get_or_check_option(name)
140
149
  option_name = name.to_s.chomp('?')
141
150
  existence_check = name.to_s.end_with?('?')
142
- existence_check ? option?(option_name) : option(option_name).value \
151
+ existence_check ? option?(option_name) : option(option_name) \
143
152
  if @config.option?(option_name)
144
153
  end
145
154
 
@@ -148,7 +157,7 @@ module Cliqr
148
157
  # @return [Object]
149
158
  def default(name)
150
159
  option_config = @config.option(name)
151
- CommandOption.new([name, option_config.default], option_config)
160
+ CommandOption.new(name, [option_config.default], option_config)
152
161
  end
153
162
 
154
163
  # Check if running in a bash environment
@@ -203,8 +212,8 @@ module Cliqr
203
212
  #
204
213
  # @return [Cliqr::Command::CommandContext] A newly created CommandContext instance
205
214
  def build
206
- option_contexts = @parsed_input.options.map do |option|
207
- CommandOption.new(option, @config.option(option.first))
215
+ option_contexts = @parsed_input.options.map do |option_name, option_values|
216
+ CommandOption.new(option_name, option_values, @config.option(option_name))
208
217
  end
209
218
 
210
219
  CommandContext.new @config,
@@ -239,27 +248,43 @@ module Cliqr
239
248
  # @return [String]
240
249
  attr_accessor :name
241
250
 
242
- # Value for the command line argument's option
251
+ # Value array for the command line argument's option
243
252
  #
244
- # @return [Object]
245
- attr_accessor :value
253
+ # @return [Array]
254
+ attr_accessor :values
246
255
 
247
256
  # Create a new command line option instance
248
257
  #
249
- # @param [Array] option Parsed arguments for creating a command line option
258
+ # @param [String] name Name of the option
259
+ # @param [Array] values Parsed values for this option
250
260
  # @param [Cliqr::Config::Option] option_config Option's config settings
251
261
  #
252
262
  # @return [Cliqr::Command::CommandContext] A new CommandOption object
253
- def initialize(option, option_config)
254
- @value = run_value_operator(option.pop, option_config.operator)
255
- @name = option.pop
263
+ def initialize(name, values, option_config)
264
+ @name = name
265
+ @values = values.map { |value| run_value_operator(value, option_config.operator) }
266
+ end
267
+
268
+ # Get value for this option
269
+ #
270
+ # @return [Object] Joins in a CSV format if multiple
271
+ def value
272
+ return values.first if values.length == 1
273
+ values.join(',')
274
+ end
275
+
276
+ # Get string representation for this option
277
+ #
278
+ # @return [String]
279
+ def to_s
280
+ value.to_s
256
281
  end
257
282
 
258
283
  private
259
284
 
260
- # Run the operator for a named attribute for a value
285
+ # Run the operator for a named attribute for option's values
261
286
  #
262
- # @return [Nothing]
287
+ # @return [Array]
263
288
  def run_value_operator(value, operator)
264
289
  if operator.is_a?(Proc)
265
290
  Command::ArgumentOperatorContext.new(value).instance_eval(&operator)
@@ -110,7 +110,7 @@ module Cliqr
110
110
  @context.puts "unknown action \"#{action_name}\""
111
111
  return Cliqr::Executor::ExitCode.code(nil)
112
112
  end
113
- @context.forward("#{@base_command} #{command}", :environment => @context.environment)
113
+ @context.forward("#{@base_command} #{command}", environment: @context.environment)
114
114
  rescue StandardError => e
115
115
  @context.puts e.message
116
116
  end
@@ -28,9 +28,9 @@ module Cliqr
28
28
 
29
29
  # Default values based on argument type
30
30
  ARGUMENT_DEFAULTS = {
31
- NUMERIC_ARGUMENT_TYPE => 0,
32
- BOOLEAN_ARGUMENT_TYPE => false,
33
- ANY_ARGUMENT_TYPE => nil
31
+ NUMERIC_ARGUMENT_TYPE => 0,
32
+ BOOLEAN_ARGUMENT_TYPE => false,
33
+ ANY_ARGUMENT_TYPE => nil
34
34
  }
35
35
 
36
36
  # Get the passed param value if current attribute is unset
@@ -33,6 +33,13 @@ module Cliqr
33
33
  { type_of: Proc }
34
34
  ]
35
35
 
36
+ # Enable or disable multiple values for this option
37
+ #
38
+ # @return [Boolean]
39
+ attr_accessor :multi_valued
40
+ validates :multi_valued,
41
+ inclusion: [true, false]
42
+
36
43
  # Default value for this option
37
44
  #
38
45
  # @return [Object]
@@ -46,6 +53,7 @@ module Cliqr
46
53
  @type = UNSET
47
54
  @operator = UNSET
48
55
  @default = UNSET
56
+ @multi_valued = UNSET
49
57
  end
50
58
 
51
59
  # Finalize option's config by adding default values for unset values
@@ -59,6 +67,7 @@ module Cliqr
59
67
  @operator = Util.ensure_instance(
60
68
  Config.get_if_unset(@operator, Cliqr::Command::ArgumentOperator.for_type(@type)))
61
69
  @default = Config.get_if_unset(@default, ARGUMENT_DEFAULTS[@type])
70
+ @multi_valued = Config.get_if_unset(@multi_valued, false)
62
71
 
63
72
  self
64
73
  end
@@ -90,6 +99,11 @@ module Cliqr
90
99
  def default?
91
100
  !@default.nil?
92
101
  end
102
+
103
+ # Check if this option supports multiple values
104
+ def multi_valued?
105
+ @multi_valued
106
+ end
93
107
  end
94
108
  end
95
109
  end
@@ -373,17 +373,17 @@ module Cliqr
373
373
 
374
374
  # A hash of validator type id to validator class
375
375
  VALIDATORS = {
376
- :non_empty => NonEmptyValidator,
377
- :non_empty_format => NonEmptyFormatValidator,
378
- :non_empty_nil_ok_format => NonEmptyNilOkFormatValidator,
379
- :format => FormatValidator,
380
- :extend => TypeHierarchyValidator,
381
- :collection => CollectionValidator,
382
- :hash => HashValidator,
383
- :inclusion => InclusionValidator,
384
- :one_of => OneOfValidator,
385
- :type_of => TypeOfValidator,
386
- :child => ChildValidator
376
+ non_empty: NonEmptyValidator,
377
+ non_empty_format: NonEmptyFormatValidator,
378
+ non_empty_nil_ok_format: NonEmptyNilOkFormatValidator,
379
+ format: FormatValidator,
380
+ extend: TypeHierarchyValidator,
381
+ collection: CollectionValidator,
382
+ hash: HashValidator,
383
+ inclusion: InclusionValidator,
384
+ one_of: OneOfValidator,
385
+ type_of: TypeOfValidator,
386
+ child: ChildValidator
387
387
  }
388
388
 
389
389
  # Get a new validator based on the type and config param
@@ -50,9 +50,9 @@ module Cliqr
50
50
  $stderr = old_stderr.is_a?(StringIO) ? old_stderr : StringIO.new('', 'w')
51
51
  yield
52
52
  {
53
- :stdout => $stdout.string,
54
- :stderr => $stderr.string,
55
- :status => 0
53
+ stdout: $stdout.string,
54
+ stderr: $stderr.string,
55
+ status: 0
56
56
  }
57
57
  ensure
58
58
  $stdout = old_stdout
@@ -49,8 +49,8 @@ module Cliqr
49
49
  # @return [Integer] Exit code
50
50
  def execute_internal(args = [], **options)
51
51
  options = {
52
- :output => :default,
53
- :environment => :cli
52
+ output: :default,
53
+ environment: :cli
54
54
  }.merge(options)
55
55
  @runner.execute(args, options)
56
56
  end
@@ -32,8 +32,8 @@ module Cliqr
32
32
  # @return [Hash] A hash of the option name and its value
33
33
  def build
34
34
  {
35
- :name => @name.to_s,
36
- :value => @value
35
+ name: @name.to_s,
36
+ value: @value
37
37
  }
38
38
  end
39
39
 
@@ -23,11 +23,7 @@ module Cliqr
23
23
  # Initialize a new parsed input
24
24
  def initialize(parsed_arguments)
25
25
  @command = parsed_arguments[:command]
26
-
27
- @options = Hash[parsed_arguments[:options].collect \
28
- { |option| [option[:name].to_s, option[:value]] }]\
29
- if parsed_arguments.key?(:options)
30
-
26
+ @options = parsed_arguments[:options]
31
27
  @arguments = parsed_arguments[:arguments]
32
28
  end
33
29
 
@@ -52,14 +52,25 @@ module Cliqr
52
52
  #
53
53
  # @return [Cliqr::Parser::ParsedInput] Parsed arguments wrapper
54
54
  def build
55
- ParsedInput.new(:command => @config.name,
56
- :actions => @actions,
57
- :options => @options,
58
- :arguments => @arguments)
55
+ ParsedInput.new(command: @config.name,
56
+ actions: @actions,
57
+ options: grouped_options,
58
+ arguments: @arguments)
59
59
  end
60
60
 
61
61
  private
62
62
 
63
+ # Group options in a hash of option name to an array of its values
64
+ #
65
+ # @return [Hash]
66
+ def grouped_options
67
+ @options.group_by { |option| option[:name] }.each_with_object({}) do |(name, group), hash|
68
+ hash[name] ||= []
69
+ group.each { |item| hash[name].push(item[:value]) }
70
+ hash
71
+ end
72
+ end
73
+
63
74
  # Add option's name to a list of already added options and fail if duplicate
64
75
  #
65
76
  # @param [Cliqr::CLI::Parser::Token] token A parsed token from command line arguments
@@ -68,8 +79,8 @@ module Cliqr
68
79
  def add_option_name(token)
69
80
  option_config = @action_config.option(token.name)
70
81
  old_config = @option_names.add?(option_config.name)
71
- fail Cliqr::Error::MultipleOptionValues,
72
- "multiple values for option \"#{token.arg}\"" if old_config.nil?
82
+ fail Cliqr::Error::MultipleOptionValues, "multiple values for option \"#{token.arg}\"" \
83
+ if old_config.nil? && !option_config.multi_valued?
73
84
  @option_names.add(option_config.short) if option_config.short?
74
85
  @option_names
75
86
  end
@@ -13,8 +13,8 @@ module Cliqr
13
13
  TEMPLATES_PATH = "#{File.expand_path(File.dirname(__FILE__))}/templates"
14
14
 
15
15
  USAGE_TYPES = {
16
- :cli => "#{TEMPLATES_PATH}/usage/cli.erb",
17
- :shell => "#{TEMPLATES_PATH}/usage/shell.erb"
16
+ cli: "#{TEMPLATES_PATH}/usage/cli.erb",
17
+ shell: "#{TEMPLATES_PATH}/usage/shell.erb"
18
18
  }
19
19
 
20
20
  # Create a new usage builder
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Versioned gem
4
4
  module Cliqr
5
- VERSION = '2.1.1'
5
+ VERSION = '2.2.0'
6
6
  end
@@ -10,6 +10,6 @@ require 'fixtures/test_command'
10
10
  def assert_results(config, args, expected_result, expected_config = nil)
11
11
  expected_config ||= config
12
12
  action_config, actual_parsed_input = Cliqr::Parser.parse(config, args)
13
- expect(actual_parsed_input).to eq(expected_result)
13
+ expect(actual_parsed_input).to(match(expected_result))
14
14
  expect(action_config).to eq(expected_config)
15
15
  end
@@ -140,4 +140,17 @@ describe Cliqr::Config::Option do
140
140
  end.to(raise_error(Cliqr::Error::ValidationError,
141
141
  "invalid Cliqr interface configuration - [option \"option-1\" - invalid type '']"))
142
142
  end
143
+
144
+ it 'only allows boolean values for multi_valued field' do
145
+ def define_interface
146
+ Cliqr.interface do
147
+ name 'my-command'
148
+ option 'test' do
149
+ multi_valued Object
150
+ end
151
+ end
152
+ end
153
+ expect { define_interface }.to(raise_error(Cliqr::Error::ValidationError,
154
+ "invalid Cliqr interface configuration - [option \"test\" - invalid type 'Object']"))
155
+ end
143
156
  end
@@ -247,7 +247,7 @@ Available options:
247
247
  end
248
248
 
249
249
  option 'hash-option' do
250
- default(:key => 'val')
250
+ default(key: 'val')
251
251
  end
252
252
  end
253
253
 
@@ -143,7 +143,7 @@ Time
143
143
  puts "invoked #{event.name} : #{event.command} : #{ch} : #{num}"
144
144
  puts "#{event.name} #{(event.parent? ? "has parent => #{event.parent.name}" : 'does not have parent')}"
145
145
  sleep 1
146
- invoke :baz, 'b', 2, :t => 1
146
+ invoke :baz, 'b', 2, t: 1
147
147
  puts "#{event.name} ending"
148
148
  end
149
149
  on :baz do |event, ch, num, hash|
@@ -152,7 +152,7 @@ Time
152
152
  puts "parent: #{event.parent.name} : #{event.parent.command}"
153
153
  puts "diff => #{event.timestamp.to_i - event.parent.timestamp.to_i}"
154
154
  sleep 1
155
- invoke :foo, 'c', 3, :s => 2
155
+ invoke :foo, 'c', 3, s: 2
156
156
  puts "#{event.name} ending"
157
157
  end
158
158
  handler do
@@ -572,4 +572,61 @@ a question?
572
572
  end
573
573
  end
574
574
  end
575
+
576
+ it 'can get multiple values for an option' do
577
+ cli = Cliqr.interface do
578
+ name 'my-command'
579
+
580
+ option 'foo' do
581
+ multi_valued true
582
+ end
583
+
584
+ handler do
585
+ puts foo
586
+ puts foo.value
587
+ puts foo.values.first
588
+ end
589
+ end
590
+
591
+ result = cli.execute_internal %w(--foo v1 --foo v2 --foo v3), output: :buffer
592
+ expect(result[:stdout]).to eq <<-EOS
593
+ v1,v2,v3
594
+ v1,v2,v3
595
+ v1
596
+ EOS
597
+ end
598
+
599
+ it 'operates on multi option values' do
600
+ cli = Cliqr.interface do
601
+ name 'my-command'
602
+
603
+ option 'foo' do
604
+ multi_valued true
605
+ type :numeric
606
+ end
607
+
608
+ option 'bar' do
609
+ multi_valued true
610
+ type :boolean
611
+ end
612
+
613
+ handler do
614
+ puts foo
615
+ puts foo.values.map(&:class)
616
+ puts bar
617
+ puts bar.values.map(&:class)
618
+ end
619
+ end
620
+
621
+ result = cli.execute_internal %w(--foo 123 --foo 987 --bar --bar --no-bar), output: :buffer
622
+ expect(result[:stdout]).to eq <<-EOS
623
+ 123,987
624
+ Fixnum
625
+ Fixnum
626
+ true,true,false
627
+ TrueClass
628
+ TrueClass
629
+ FalseClass
630
+ EOS
631
+ end
575
632
  end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Cliqr::Command::CommandContext do
6
+ it 'allows selection of an item from a list' do
7
+ allow(Sawaal).to(receive(:select)).and_return(3)
8
+ cli = Cliqr.interface do
9
+ name 'my-command'
10
+ handler do
11
+ food_items = %w(pizza salad coke lasagna apple)
12
+ selected = ask('what would you like?', food_items)
13
+ puts selected
14
+ puts food_items[selected]
15
+ end
16
+ end
17
+ result = cli.execute_internal ['my-command'], output: :buffer
18
+ expect(result[:stdout]).to eq <<-EOS
19
+ 3
20
+ lasagna
21
+ EOS
22
+ end
23
+ end
@@ -4,6 +4,6 @@
4
4
  class TestInvokerEventHandler < Cliqr.event_handler
5
5
  def handle(event, ch, num)
6
6
  puts "invoked #{event.name} : #{event.command} : #{ch} : #{num}"
7
- invoke :foo, 'b', 2, :t => 1
7
+ invoke :foo, 'b', 2, t: 1
8
8
  end
9
9
  end
@@ -13,8 +13,8 @@ describe Cliqr::Parser do
13
13
  end
14
14
  end
15
15
  config = cli.config
16
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
17
- :options => [])
16
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
17
+ options: {})
18
18
  assert_results(config, ['my-action'], parsed_input, config.action('my-action'))
19
19
  end
20
20
 
@@ -36,8 +36,8 @@ describe Cliqr::Parser do
36
36
  end
37
37
  end
38
38
  config = cli.config
39
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
40
- :options => [])
39
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
40
+ options: {})
41
41
  assert_results(config, %w(my-action-1 my-action-2 my-action-3), parsed_input,
42
42
  config.action('my-action-1').action('my-action-2').action('my-action-3'))
43
43
  end
@@ -60,17 +60,11 @@ describe Cliqr::Parser do
60
60
  end
61
61
  end
62
62
  config = cli.config
63
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
64
- :options => [
65
- {
66
- :name => 'test-option-1',
67
- :value => 'abcd'
68
- },
69
- {
70
- :name => 'test-option-2',
71
- :value => 'qwe'
72
- }
73
- ])
63
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
64
+ options: {
65
+ 'test-option-1' => ['abcd'],
66
+ 'test-option-2' => ['qwe']
67
+ })
74
68
  assert_results(config, %w(my-action -t abcd --test-option-2 qwe), parsed_input, config.action('my-action'))
75
69
  end
76
70
 
@@ -96,17 +90,11 @@ describe Cliqr::Parser do
96
90
  end
97
91
  end
98
92
  config = cli.config
99
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
100
- :options => [
101
- {
102
- :name => 'test-option-1',
103
- :value => 'abcd'
104
- },
105
- {
106
- :name => 'test-option-2',
107
- :value => 'qwe'
108
- }
109
- ])
93
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
94
+ options: {
95
+ 'test-option-1' => ['abcd'],
96
+ 'test-option-2' => ['qwe']
97
+ })
110
98
  assert_results(config, %w(my-action-1 -t abcd --test-option-2 qwe my-action-2), parsed_input,
111
99
  config.action('my-action-1').action('my-action-2'))
112
100
  end
@@ -15,32 +15,23 @@ describe Cliqr::Parser do
15
15
  CONFIG = TEST_CLI.config
16
16
 
17
17
  it 'can parse no argument command' do
18
- assert_results(CONFIG, [], Cliqr::Parser::ParsedInput.new(:command => 'my-command', :options => []))
18
+ assert_results(CONFIG, [], Cliqr::Parser::ParsedInput.new(command: 'my-command', options: {}))
19
19
  end
20
20
 
21
21
  it 'can parse command with option using long name' do
22
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
23
- :options => [
24
- {
25
- :name => 'test-option',
26
- :value => 'abcd'
27
- }
28
- ])
22
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
23
+ options: {
24
+ 'test-option' => ['abcd']
25
+ })
29
26
  assert_results(CONFIG, %w(--test-option abcd), parsed_input)
30
27
  end
31
28
 
32
29
  it 'can parse multiple options' do
33
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
34
- :options => [
35
- {
36
- :name => 'test-option-1',
37
- :value => 'abcd'
38
- },
39
- {
40
- :name => 'test-option-2',
41
- :value => 'xyz'
42
- }
43
- ])
30
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
31
+ options: {
32
+ 'test-option-1' => ['abcd'],
33
+ 'test-option-2' => ['xyz']
34
+ })
44
35
  cli = Cliqr.interface do
45
36
  name 'my-command'
46
37
  handler TestCommand
@@ -52,13 +43,10 @@ describe Cliqr::Parser do
52
43
  end
53
44
 
54
45
  it 'can parse command with option using short name' do
55
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
56
- :options => [
57
- {
58
- :name => 'test-option',
59
- :value => 'abcd'
60
- }
61
- ])
46
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
47
+ options: {
48
+ 'test-option' => ['abcd']
49
+ })
62
50
  assert_results(CONFIG, %w(-t abcd), parsed_input)
63
51
  end
64
52
 
@@ -96,20 +84,18 @@ describe Cliqr::Parser do
96
84
  short 't'
97
85
  end
98
86
  end
99
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
100
- :options => {},
101
- :arguments => ['value1'])
87
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
88
+ options: {},
89
+ arguments: ['value1'])
102
90
  assert_results(cli.config, ['value1'], parsed_input)
103
91
  end
104
92
 
105
93
  it 'can parse command with one option and one argument' do
106
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
107
- :options => [
108
- {
109
- :name => 'test-option',
110
- :value => 'abcd'
111
- }],
112
- :arguments => ['value1'])
94
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
95
+ options: {
96
+ 'test-option' => ['abcd']
97
+ },
98
+ arguments: ['value1'])
113
99
  cli = Cliqr.interface do
114
100
  name 'my-command'
115
101
  handler TestCommand
@@ -139,20 +125,70 @@ describe Cliqr::Parser do
139
125
  end
140
126
  end
141
127
  config = cli.config
142
- parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
143
- :arguments => %w(value1 value2),
144
- :options => [
145
- {
146
- :name => 'test-option-1',
147
- :value => 'abcd'
148
- },
149
- {
150
- :name => 'test-option-2',
151
- :value => 'qwe'
152
- }
153
- ])
128
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
129
+ arguments: %w(value1 value2),
130
+ options: {
131
+ 'test-option-1' => ['abcd'],
132
+ 'test-option-2' => ['qwe']
133
+ })
154
134
  assert_results(config, %w(-t abcd -p qwe value1 value2), parsed_input)
155
135
  assert_results(config, %w(value1 -t abcd value2 -p qwe), parsed_input)
156
136
  assert_results(config, %w(-t abcd value1 -p qwe value2), parsed_input)
157
137
  end
138
+
139
+ it 'can parse command with multiple arguments for a option' do
140
+ cli = Cliqr.interface do
141
+ name 'my-command'
142
+ arguments :enable
143
+
144
+ option 'test-option' do
145
+ short 't'
146
+ multi_valued true
147
+ end
148
+
149
+ option 'test-option-2' do
150
+ short 'p'
151
+ multi_valued false
152
+ end
153
+ end
154
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
155
+ arguments: [],
156
+ options: {
157
+ 'test-option' => %w(v1 v2 v3),
158
+ 'test-option-2' => %w(x)
159
+ })
160
+ assert_results(cli.config, %w(--test-option v1 -t v2 --test-option v3 -p x), parsed_input)
161
+ end
162
+
163
+ it 'can parse command with multiple arguments for a numeric option' do
164
+ cli = Cliqr.interface do
165
+ name 'my-command'
166
+ arguments :enable
167
+
168
+ option 'test-option' do
169
+ multi_valued true
170
+ type :numeric
171
+ end
172
+ end
173
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
174
+ options: {},
175
+ arguments: ['value1'])
176
+ assert_results(cli.config, ['value1'], parsed_input)
177
+ end
178
+
179
+ it 'can parse command with multiple arguments for a boolean option' do
180
+ cli = Cliqr.interface do
181
+ name 'my-command'
182
+ arguments :enable
183
+
184
+ option 'test-option' do
185
+ multi_valued true
186
+ type :boolean
187
+ end
188
+ end
189
+ parsed_input = Cliqr::Parser::ParsedInput.new(command: 'my-command',
190
+ options: {},
191
+ arguments: ['value1'])
192
+ assert_results(cli.config, ['value1'], parsed_input)
193
+ end
158
194
  end
@@ -1,8 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'tempfile'
4
-
5
3
  require 'spec_helper'
4
+ require 'spec_util'
6
5
 
7
6
  require 'fixtures/test_command'
8
7
  require 'fixtures/test_shell_prompt'
@@ -679,27 +678,3 @@ shell exited with code 0.
679
678
  end
680
679
  end
681
680
  end
682
-
683
- def with_input_output(lines, &block)
684
- old_stdin = $stdin
685
- old_stdout = $stdout
686
- input_file = Tempfile.new('cliqr').tap do |file|
687
- lines.push('exit').each { |line| file.write("#{line}\n") }
688
- end
689
- output_file = Tempfile.new('cliqr')
690
- begin
691
- $stdin = input_file.open
692
- $stdout = output_file.open
693
- output_getter = proc do
694
- IO.read(output_file.path).gsub(/(.*)\n/, "\\1.\n")
695
- end
696
- block.call(output_getter)
697
- ensure
698
- $stdin = old_stdin
699
- $stdout = old_stdout
700
- input_file.close
701
- output_file.close
702
- input_file.unlink
703
- output_file.unlink
704
- end
705
- end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'tempfile'
4
+
5
+ def with_input_output(lines, &block)
6
+ old_stdin = $stdin
7
+ old_stdout = $stdout
8
+ input_file = Tempfile.new('cliqr').tap do |file|
9
+ lines.push('exit').each { |line| file.write("#{line}\n") }
10
+ end
11
+ output_file = Tempfile.new('cliqr')
12
+ begin
13
+ $stdin = input_file.open
14
+ $stdout = output_file.open
15
+ output_getter = proc do
16
+ IO.read(output_file.path).gsub(/(.*)\n/, "\\1.\n")
17
+ end
18
+ block.call(output_getter)
19
+ ensure
20
+ $stdin = old_stdin
21
+ $stdout = old_stdout
22
+ input_file.close
23
+ output_file.close
24
+ input_file.unlink
25
+ output_file.unlink
26
+ end
27
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cliqr
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.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-07-15 00:00:00.000000000 Z
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: log4r
14
+ name: sawaal
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '1.1'
19
+ version: 1.0.0
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
- version: '1.1'
26
+ version: 1.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -131,6 +131,7 @@ files:
131
131
  - spec/executor/event_executor_spec.rb
132
132
  - spec/executor/executor_spec.rb
133
133
  - spec/executor/help_executor_spec.rb
134
+ - spec/executor/selection_spec.rb
134
135
  - spec/fixtures/action_reader_command.rb
135
136
  - spec/fixtures/always_error_command.rb
136
137
  - spec/fixtures/argument_reader_command.rb
@@ -150,6 +151,7 @@ files:
150
151
  - spec/parser/argument_parser_spec.rb
151
152
  - spec/shell/shell_executor_spec.rb
152
153
  - spec/spec_helper.rb
154
+ - spec/spec_util.rb
153
155
  - spec/validation/action_argument_validator_spec.rb
154
156
  - spec/validation/command_argument_validation_spec.rb
155
157
  - spec/validation/error_spec.rb
@@ -195,6 +197,7 @@ test_files:
195
197
  - spec/executor/event_executor_spec.rb
196
198
  - spec/executor/executor_spec.rb
197
199
  - spec/executor/help_executor_spec.rb
200
+ - spec/executor/selection_spec.rb
198
201
  - spec/fixtures/action_reader_command.rb
199
202
  - spec/fixtures/always_error_command.rb
200
203
  - spec/fixtures/argument_reader_command.rb
@@ -214,6 +217,7 @@ test_files:
214
217
  - spec/parser/argument_parser_spec.rb
215
218
  - spec/shell/shell_executor_spec.rb
216
219
  - spec/spec_helper.rb
220
+ - spec/spec_util.rb
217
221
  - spec/validation/action_argument_validator_spec.rb
218
222
  - spec/validation/command_argument_validation_spec.rb
219
223
  - spec/validation/error_spec.rb