cliqr 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dbf6874d54c340fea81a85d5d9c5d495eaf4bf70
4
- data.tar.gz: c3a763238d2cfb47d3cf479d7922a9fa43357656
3
+ metadata.gz: 779fd42a32e0ec2a3d11d5db83bc65c66926d56b
4
+ data.tar.gz: a2c085fa4815bdcd88d04d1c52e6d7e1e2e4e4d2
5
5
  SHA512:
6
- metadata.gz: db2d7b59f422dd286fc244b6ed263ddbef70f9a088b3c95018fda4113daf3525ba53d96b4e6d0d6b970ba1fe52bf4772be34b2acdc60131c08aa3a242a5ddc2b
7
- data.tar.gz: e470554fbb6649e3685173b78bd6559f5d104b6215ef8fd7cdc4265679cf1df2d7a29973c46a0ef009cf3278f3e84ea6b82fe04820989985d9af8bda512655b5
6
+ metadata.gz: f91257c83d83705c218151e96b5c0e68f183ab714df0b661d65287d819ef83b0099c152780e46a25c1aab151a4cb21439252c787f1a6ae99bab3d7f6b4fb68ac
7
+ data.tar.gz: 609e715827bfe3cb93fb6dd9271856c0a9c7c256aa7ba89d070aa0906181452b5a3b34020dd3d4850b26d75483721931f1339fedb9f230f16dd60e841bf3394c
@@ -5,6 +5,12 @@ 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
+ - [1.1.0 / 2015-06-05](#110--2015-06-05)
9
+ - [Features](#features)
10
+ - [Support for arbitrary arguments in a command](#support-for-arbitrary-arguments-in-a-command)
11
+ - [Minor Improvements](#minor-improvements)
12
+ - [Improve readme example](#improve-readme-example)
13
+ - [Reorganize config validation logic](#reorganize-config-validation-logic)
8
14
  - [1.0.0 / 2015-06-04](#100--2015-06-04)
9
15
  - [Features](#features)
10
16
  - [Support for option types](#support-for-option-types)
@@ -61,6 +67,24 @@ item in this nested table for further details.
61
67
 
62
68
  <!-- markdown-toc end -->
63
69
 
70
+ 1.1.0 / 2015-06-05
71
+ ==================
72
+
73
+ A small, yet a very important release. In this release we add support
74
+ for arbitrary arguments.
75
+
76
+ ## Features
77
+
78
+ ### Support for arbitrary arguments in a command
79
+
80
+ For the first time you can invoke a command with a non-option arguments.
81
+
82
+ ## Minor Improvements
83
+
84
+ ### Improve readme example
85
+
86
+ ### Reorganize config validation logic
87
+
64
88
  1.0.0 / 2015-06-04
65
89
  ==================
66
90
 
data/README.md CHANGED
@@ -53,8 +53,8 @@ class MyCommandHandler < Cliqr.command
53
53
  puts "value for option 'count' is '#{context.option('count').value}'"
54
54
  puts "value for option 'single' is '#{context.option('single').value}'"
55
55
  puts "value for option 'test-1' is '#{context.option('test-1').value}'"
56
- puts "has count argument" if context.option?('count')
57
- puts "does not have test argument" unless context.option?('test-2')
56
+ puts "has 'count' argument" if context.option?('count')
57
+ puts "does not have 'test-2' argument" unless context.option?('test-2')
58
58
  end
59
59
  end
60
60
 
@@ -88,7 +88,7 @@ puts cli.usage
88
88
  # my-command -- this is an awesome command...try it out
89
89
  #
90
90
  # USAGE:
91
- # my-command [options]
91
+ # my-command [options] [arguments]
92
92
  #
93
93
  # Available options:
94
94
  #
@@ -108,8 +108,8 @@ cli.execute %w(--an-option qwerty -c 86 --no-single --test-1 some-value)
108
108
  # value for option 'count' is '86'
109
109
  # value for option 'single' is 'false'
110
110
  # value for option 'test-1' is 'some-value'
111
- # has count argument
112
- # does not have test argument
111
+ # has 'count' argument
112
+ # does not have 'test-2' argument
113
113
  ```
114
114
 
115
115
  ## Installation
@@ -12,6 +12,11 @@ module Cliqr
12
12
  # @return [String]
13
13
  attr_accessor :command
14
14
 
15
+ # Command arguments
16
+ #
17
+ # @return [Array<String>] List of arguments
18
+ attr_accessor :arguments
19
+
15
20
  # Build a instance of command context based on the parsed set of arguments
16
21
  #
17
22
  # @param [Cliqr::Parser::ParsedInput] parsed_input Parsed input object
@@ -24,8 +29,10 @@ module Cliqr
24
29
  # Initialize the command context (called by the CommandContextBuilder)
25
30
  #
26
31
  # @return [Cliqr::CLI::CommandContext]
27
- def initialize(command, options)
32
+ def initialize(command, options, arguments)
28
33
  @command = command
34
+ @arguments = arguments
35
+
29
36
  # make option map from array
30
37
  @options = Hash[*options.collect { |option| [option.name, option] }.flatten]
31
38
  end
@@ -78,7 +85,8 @@ module Cliqr
78
85
  # @return [Cliqr::CLI::CommandContext] A newly created CommandContext instance
79
86
  def build
80
87
  CommandContext.new @parsed_input.command,
81
- @parsed_input.options.map { |option| CommandOption.new(option) }
88
+ @parsed_input.options.map { |option| CommandOption.new(option) },
89
+ @parsed_input.arguments
82
90
  end
83
91
  end
84
92
 
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'cliqr/dsl'
4
- require 'cliqr/validation/verifiable'
4
+ require 'cliqr/config_validation/verifiable'
5
5
  require 'cliqr/cli/command'
6
6
 
7
7
  module Cliqr
@@ -15,7 +15,7 @@ module Cliqr
15
15
  # @api private
16
16
  class Config
17
17
  extend Cliqr::DSL
18
- include Cliqr::Validation
18
+ include Cliqr::ConfigValidation
19
19
 
20
20
  # Base name of the command
21
21
  #
@@ -36,6 +36,13 @@ module Cliqr
36
36
  validates :handler,
37
37
  extend: Cliqr::CLI::Command
38
38
 
39
+ # Optional attribute that dictates whether this command can take arbitrary arguments
40
+ #
41
+ # @return [Symbol] Either <tt>:enabled</tt> or <tt>:disabled</tt>
42
+ attr_accessor :arguments
43
+ validates :arguments,
44
+ inclusion: [:enable, :disable]
45
+
39
46
  # Array of options applied to the base command
40
47
  #
41
48
  # @return [Array<OptionConfig>]
@@ -48,6 +55,7 @@ module Cliqr
48
55
  @basename = UNSET
49
56
  @description = UNSET
50
57
  @handler = UNSET
58
+ @arguments = UNSET
51
59
 
52
60
  @options = []
53
61
  @option_index = {}
@@ -60,6 +68,7 @@ module Cliqr
60
68
  @basename = '' if @basename == UNSET
61
69
  @description = '' if @description == UNSET
62
70
  @handler = nil if @handler == UNSET
71
+ @arguments = :enable if @arguments == UNSET
63
72
 
64
73
  self
65
74
  end
@@ -108,6 +117,13 @@ module Cliqr
108
117
  @option_index[name]
109
118
  end
110
119
 
120
+ # Check if arguments are enabled for this configuration
121
+ #
122
+ # @return [Boolean] <tt>true</tt> if arguments are enabled
123
+ def arguments?
124
+ @arguments == :enable
125
+ end
126
+
111
127
  private
112
128
 
113
129
  # Set value for config option without evaluating a block
@@ -165,7 +181,7 @@ module Cliqr
165
181
  # @api private
166
182
  class OptionConfig
167
183
  extend Cliqr::DSL
168
- include Cliqr::Validation
184
+ include Cliqr::ConfigValidation
169
185
 
170
186
  # Long option name
171
187
  #
@@ -1,9 +1,9 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'cliqr/validation/validator_factory'
3
+ require 'cliqr/config_validation/validator_factory'
4
4
 
5
5
  module Cliqr
6
- module Validation
6
+ module ConfigValidation
7
7
  # A collection of configured validators
8
8
  #
9
9
  # @api private
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Cliqr
4
- module Validation
4
+ module ConfigValidation
5
5
  # A factory class to retrieve a attribute validator based on the configuration type
6
6
  #
7
7
  # @api private
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'cliqr/validation/validator_factory'
4
- require 'cliqr/validation/validation_set'
3
+ require 'cliqr/config_validation/validator_factory'
4
+ require 'cliqr/config_validation/validation_set'
5
5
  require 'cliqr/validation_errors'
6
6
 
7
7
  module Cliqr
@@ -11,7 +11,7 @@ module Cliqr
11
11
  # @api private
12
12
  #
13
13
  # @see https://github.com/lotus/validations
14
- module Validation
14
+ module ConfigValidation
15
15
  # If a class includes this module, we add a few useful methods to that class
16
16
  #
17
17
  # @see http://www.ruby-doc.org/core/Module.html#method-i-included
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ module Cliqr
4
+ module Parser
5
+ # Token handler for parsing a arbitrary argument value
6
+ #
7
+ # @api private
8
+ class ArgumentToken < Token
9
+ # Create a new argument token
10
+ def initialize(arg)
11
+ super(arg)
12
+ end
13
+
14
+ # This token is valid if argument is non-empty
15
+ #
16
+ # @return [Boolean] <tt>true</tt> if token's argument is non-empty
17
+ def valid?
18
+ return false if arg.nil?
19
+
20
+ return !arg.empty? if @arg.respond_to?(:empty?)
21
+
22
+ true
23
+ end
24
+ end
25
+ end
26
+ end
@@ -24,27 +24,31 @@ module Cliqr
24
24
  #
25
25
  # @return [Hash] Parsed hash of command line arguments
26
26
  def walk(args)
27
- argument_builder = ParsedInputBuilder.new(@config)
27
+ input_builder = ParsedInputBuilder.new(@config)
28
28
  token_factory = TokenFactory.new(@config)
29
29
  token = token_factory.get_token
30
30
  args.each do |arg|
31
- token = handle_argument(arg, token, token_factory)
32
- argument_builder.add_token(token) unless token.active?
31
+ token = handle_argument(arg, token, token_factory, input_builder)
33
32
  end
34
- token.finalize if token.active?
35
- argument_builder.build
33
+ fail Cliqr::Error::OptionValueMissing, "a value must be defined for argument \"#{token.arg}\"" \
34
+ if token.active?
35
+ input_builder.build
36
36
  end
37
37
 
38
38
  # Handle the next argument in the context of the current token
39
39
  #
40
40
  # @return [Cliqr::CLI::Parser::Token] The new active token in case <tt>current_token</tt>
41
41
  # becomes inactive
42
- def handle_argument(arg, current_token, token_factory)
42
+ def handle_argument(arg, current_token, token_factory, input_builder)
43
43
  if current_token.active?
44
- current_token.append(arg)
44
+ token = current_token.append(arg)
45
45
  else
46
- token_factory.get_token(arg)
46
+ token = token_factory.get_token(arg)
47
47
  end
48
+
49
+ token.collect(input_builder)
50
+
51
+ token
48
52
  end
49
53
  end
50
54
  end
@@ -5,22 +5,12 @@ module Cliqr
5
5
  # Token handler for parsing a boolean option
6
6
  #
7
7
  # @api private
8
- class BooleanOptionToken < Token
8
+ class BooleanOptionToken < OptionToken
9
9
  # Create a new option token to store boolean value
10
10
  def initialize(name, arg)
11
11
  super(name, arg)
12
12
  @value = !arg.to_s.start_with?('--no-')
13
13
  end
14
-
15
- # Get the token representation
16
- #
17
- # @return [Hash] A hash of the option name and its value (<tt>true</tt> or <tt>false</tt>)
18
- def build
19
- {
20
- :name => @name.to_s,
21
- :value => @value
22
- }
23
- end
24
14
  end
25
15
  end
26
16
  end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ module Cliqr
4
+ module Parser
5
+ # Represents a option token
6
+ #
7
+ # @api private
8
+ class OptionToken < Token
9
+ # Name of the option token
10
+ #
11
+ # @return [String]
12
+ attr_accessor :name
13
+
14
+ # Value of the option token
15
+ #
16
+ # @return [String]
17
+ attr_accessor :value
18
+
19
+ # Create a new option token with a name and value
20
+ #
21
+ # @param [String] name Long name of the option
22
+ # @param [String] arg Argument used to parse option name
23
+ def initialize(name, arg)
24
+ super(arg)
25
+
26
+ @name = name
27
+ # @value = value
28
+ end
29
+
30
+ # Get the token representation
31
+ #
32
+ # @return [Hash] A hash of the option name and its value
33
+ def build
34
+ {
35
+ :name => @name.to_s,
36
+ :value => @value
37
+ }
38
+ end
39
+
40
+ # A option token is not valid if it does not have a name
41
+ #
42
+ # @return [Boolean] <tt>false</tt> if the token's name is nil
43
+ def valid?
44
+ !@name.nil?
45
+ end
46
+
47
+ # Collect this token's name and value into a input builder
48
+ #
49
+ # @param [Cliqr::Parser::ParsedInputBuilder] input_builder A builder to prepare parsed arguments
50
+ #
51
+ # @return [Cliqr::Parser::ParsedInputBuilder] The updated input builder instance
52
+ def collect(input_builder)
53
+ input_builder.add_option_token(self) unless active?
54
+ end
55
+ end
56
+ end
57
+ end
@@ -15,13 +15,20 @@ module Cliqr
15
15
  # @return [Hash]
16
16
  attr_accessor :options
17
17
 
18
+ # List of arguments from the command line
19
+ #
20
+ # @return [Array<String>]
21
+ attr_accessor :arguments
22
+
18
23
  # Initialize a new parsed input
19
24
  def initialize(parsed_arguments)
20
25
  @command = parsed_arguments[:command]
21
26
 
22
27
  @options = Hash[parsed_arguments[:options].collect \
23
- { |option| [option[:name], option[:value]] }
24
- ]
28
+ { |option| [option[:name], option[:value]] }]\
29
+ if parsed_arguments.key?(:options)
30
+
31
+ @arguments = parsed_arguments[:arguments]
25
32
  end
26
33
 
27
34
  # Get a value of an option
@@ -18,19 +18,31 @@ module Cliqr
18
18
  @config = config
19
19
  @options = []
20
20
  @option_names = Set.new
21
+ @arguments = []
21
22
  end
22
23
 
23
- # Add a new parsed token from the list of arguments
24
+ # Add a new parsed option token from the list of options
24
25
  #
25
- # @param [Cliqr::CLI::Parser::Token] token A parsed token from command line arguments
26
+ # @param [Cliqr::CLI::Parser::OptionToken] token A parsed option token from command line arguments
26
27
  #
27
- # @return [Boolean] <tt>true</tt> if the token was added
28
- def add_token(token)
29
- return false unless token.valid?
28
+ # @return [Cliqr::Parser::ParsedInputBuilder] Updated input builder
29
+ def add_option_token(token)
30
+ return self unless token.valid?
30
31
 
31
32
  add_option_name(token)
32
33
  @options.push(token.build)
33
- true
34
+
35
+ self
36
+ end
37
+
38
+ # Add a argument to the list of parsed arguments
39
+ #
40
+ # @param [String] arg Argument value
41
+ #
42
+ # @return [Cliqr::Parser::ParsedInputBuilder] Updated input builder
43
+ def add_argument(arg)
44
+ @arguments.push(arg)
45
+ self
34
46
  end
35
47
 
36
48
  # Build the hash of parsed command line arguments
@@ -38,7 +50,8 @@ module Cliqr
38
50
  # @return [Cliqr::Parser::ParsedInput] Parsed arguments wrapper
39
51
  def build
40
52
  ParsedInput.new(:command => @config.basename,
41
- :options => @options)
53
+ :options => @options,
54
+ :arguments => @arguments)
42
55
  end
43
56
 
44
57
  private
@@ -1,23 +1,17 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'cliqr/parser/option_token'
4
+
3
5
  module Cliqr
4
6
  module Parser
5
7
  # Token handler for parsing a option and its value
6
8
  #
7
9
  # @api private
8
- class SingleValuedOptionToken < Token
10
+ class SingleValuedOptionToken < OptionToken
9
11
  # Create a new option token. Initial state will be <tt>active</tt>
10
12
  def initialize(name, arg)
11
13
  super(name, arg)
12
- @value = nil
13
- @active = true
14
- end
15
-
16
- # Check if the token handler is active and needs more arguments
17
- #
18
- # @return [Boolean] <tt>true</tt> if the token handler is active
19
- def active?
20
- @active
14
+ activate
21
15
  end
22
16
 
23
17
  # Append the next argument in the series and set token to inactive
@@ -27,27 +21,9 @@ module Cliqr
27
21
  # @return [Cliqr::Parser::SingleValuedOptionToken]
28
22
  def append(arg)
29
23
  @value = arg
30
- @active = false
24
+ deactivate
31
25
  self
32
26
  end
33
-
34
- # Get the token representation
35
- #
36
- # @return [Hash] Token name and value in a hash
37
- def build
38
- {
39
- :name => @name.to_s,
40
- :value => @value
41
- }
42
- end
43
-
44
- # Called if this token handler was still active once the argument list ends
45
- #
46
- # @return [Cliqr::CLI::Parser::OptionToken] Current instance object
47
- def finalize
48
- # should not be called
49
- fail Cliqr::Error::OptionValueMissing, "a value must be defined for option \"#{@arg}\""
50
- end
51
27
  end
52
28
  end
53
29
  end
@@ -6,39 +6,57 @@ module Cliqr
6
6
  #
7
7
  # @api private
8
8
  class Token
9
- # Name of the option token
10
- #
11
- # @return [String]
12
- attr_accessor :name
13
-
14
- # Argument that was used to parse the option name from
9
+ # Argument that was used to parse this token
15
10
  #
16
11
  # @return [String]
17
12
  attr_accessor :arg
18
13
 
19
- # Create a new option token
14
+ # Create a new token
20
15
  #
21
- # @param [String] name Long name of the option
22
16
  # @param [String] arg Value of the option
23
- #
24
- # @return [Cliqr::CLI::Parser::OptionToken] A new Token instance
25
- def initialize(name = nil, arg = nil)
26
- @name = name
17
+ def initialize(arg = nil)
27
18
  @arg = arg
19
+
20
+ @active = false
28
21
  end
29
22
 
30
- # This token is never active
23
+ # Get activation status of this token
31
24
  #
32
25
  # @return [Boolean] This will always return <tt>false</tt> in this case
33
26
  def active?
34
- false
27
+ @active
35
28
  end
36
29
 
37
- # A token is not valid if it does not have a name
30
+ # This token is never valid
38
31
  #
39
- # @return [Boolean] <tt>false</tt> if the token's name is nil
32
+ # @return [Boolean] Always <tt>false</tt>
40
33
  def valid?
41
- !@name.nil?
34
+ false
35
+ end
36
+
37
+ # Collect this token's argument into a input builder
38
+ #
39
+ # @param [Cliqr::Parser::ParsedInputBuilder] input_builder A builder to prepare parsed arguments
40
+ #
41
+ # @return [Cliqr::Parser::ParsedInputBuilder] The updated input builder instance
42
+ def collect(input_builder)
43
+ input_builder.add_argument(@arg)
44
+ end
45
+
46
+ protected
47
+
48
+ # Activate this token so that it can consume more arguments
49
+ #
50
+ # @return [Boolean] Token activation state
51
+ def activate
52
+ @active = true
53
+ end
54
+
55
+ # Deactivate this token to indicate that it does not need more arguments
56
+ #
57
+ # @return [Boolean] Token activation state
58
+ def deactivate
59
+ @active = false
42
60
  end
43
61
  end
44
62
  end
@@ -3,6 +3,7 @@
3
3
  require 'cliqr/parser/token'
4
4
  require 'cliqr/parser/single_valued_option_token'
5
5
  require 'cliqr/parser/boolean_option_token'
6
+ require 'cliqr/parser/argument_token'
6
7
 
7
8
  module Cliqr
8
9
  module Parser
@@ -34,7 +35,9 @@ module Cliqr
34
35
  option_config = get_option_config(Regexp.last_match(2), arg)
35
36
  build_token(option_config, arg)
36
37
  else
37
- fail Cliqr::Error::InvalidArgumentError, "invalid command argument \"#{arg}\""
38
+ fail Cliqr::Error::InvalidArgumentError, "invalid command argument \"#{arg}\"" \
39
+ unless @config.arguments?
40
+ ArgumentToken.new(arg)
38
41
  end
39
42
  end
40
43
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Versioned gem
4
4
  module Cliqr
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.0'
6
6
  end
@@ -16,6 +16,7 @@ describe Cliqr::CLI::Interface do
16
16
  basename 'my-command'
17
17
  description 'a command used to test cliqr'
18
18
  handler TestCommand
19
+ arguments :disable
19
20
  end
20
21
 
21
22
  expect(cli.usage).to eq <<-EOS
@@ -30,6 +31,7 @@ EOS
30
31
  cli = Cliqr.interface do
31
32
  basename 'my-command'
32
33
  handler TestCommand
34
+ arguments :disable
33
35
  end
34
36
 
35
37
  expect(cli.usage).to eq <<-EOS
@@ -45,6 +47,7 @@ EOS
45
47
  basename 'my-command'
46
48
  description 'a command used to test cliqr'
47
49
  handler TestCommand
50
+ arguments :disable
48
51
 
49
52
  option 'option-1' do
50
53
  short 'p'
@@ -69,6 +72,7 @@ EOS
69
72
  basename 'my-command'
70
73
  description 'a command used to test cliqr'
71
74
  handler TestCommand
75
+ arguments :disable
72
76
 
73
77
  option 'option-1'
74
78
  end
@@ -104,6 +108,7 @@ Available options:
104
108
  basename 'my-command'
105
109
  description 'a command used to test cliqr'
106
110
  handler TestCommand
111
+ arguments :disable
107
112
 
108
113
  option 'option-1' do
109
114
  description 'a numeric option'
@@ -129,6 +134,7 @@ Available options:
129
134
  basename 'my-command'
130
135
  description 'a command used to test cliqr'
131
136
  handler TestCommand
137
+ arguments :disable
132
138
 
133
139
  option 'option-1' do
134
140
  description 'a boolean option'
@@ -148,4 +154,41 @@ Available options:
148
154
  --[no-]option-1, -p : <boolean> a boolean option
149
155
  EOS
150
156
  end
157
+
158
+ it 'allows interface to enable arbitrary argument list parsing without options' do
159
+ cli = Cliqr.interface do
160
+ basename 'my-command'
161
+ handler TestCommand
162
+ arguments :enable
163
+ end
164
+
165
+ expect(cli.usage).to eq <<-EOS
166
+ my-command
167
+
168
+ USAGE:
169
+ my-command [arguments]
170
+ EOS
171
+ end
172
+
173
+ it 'allows interface to enable arbitrary argument list parsing' do
174
+ cli = Cliqr.interface do
175
+ basename 'my-command'
176
+ handler TestCommand
177
+ arguments :enable
178
+
179
+ option 'option-1'
180
+ end
181
+
182
+ expect(cli.usage).to eq <<-EOS
183
+ my-command
184
+
185
+ USAGE:
186
+ my-command [options] [arguments]
187
+
188
+ Available options:
189
+
190
+ --option-1
191
+ EOS
192
+ end
193
+
151
194
  end
@@ -9,6 +9,7 @@ require 'fixtures/always_error_command'
9
9
  require 'fixtures/option_reader_command'
10
10
  require 'fixtures/test_option_reader_command'
11
11
  require 'fixtures/test_option_checker_command'
12
+ require 'fixtures/argument_reader_command'
12
13
 
13
14
  describe Cliqr::CLI::Executor do
14
15
  it 'returns code 0 for default command runner' do
@@ -100,4 +101,21 @@ some-value
100
101
  test-option is defined
101
102
  EOS
102
103
  end
104
+
105
+ it 'allows command to access argument list' do
106
+ cli = Cliqr.interface do
107
+ basename 'my-command'
108
+ handler ArgumentReaderCommand
109
+ arguments :enable
110
+
111
+ option 'test-option'
112
+ end
113
+
114
+ result = cli.execute %w(value1 --test-option qwerty value2 value3), output: :buffer
115
+ expect(result[:stdout]).to eq <<-EOS
116
+ value1
117
+ value2
118
+ value3
119
+ EOS
120
+ end
103
121
  end
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ # A command that echoes the value for option named 'test-option'
4
+ class ArgumentReaderCommand < Cliqr.command
5
+ def execute(context)
6
+ puts context.arguments
7
+ end
8
+ end
@@ -12,6 +12,7 @@ describe Cliqr::Parser do
12
12
  TEST_CLI = Cliqr.interface do
13
13
  basename 'my-command'
14
14
  handler TestCommand
15
+ arguments :disable
15
16
 
16
17
  option 'test-option' do
17
18
  short 't'
@@ -84,11 +85,81 @@ describe Cliqr::Parser do
84
85
 
85
86
  it 'cannot parse option without value if required' do
86
87
  expect { PARSER.parse(CONFIG, %w(--test-option)) }.to(
87
- raise_error(Cliqr::Error::OptionValueMissing, 'a value must be defined for option "--test-option"'))
88
+ raise_error(Cliqr::Error::OptionValueMissing, 'a value must be defined for argument "--test-option"'))
88
89
  end
89
90
 
90
91
  it 'cannot parse option if it has multiple values' do
91
92
  expect { PARSER.parse(CONFIG, %w(--test-option val1 --test-option val2)) }.to(
92
93
  raise_error(Cliqr::Error::MultipleOptionValues, 'multiple values for option "--test-option"'))
93
94
  end
95
+
96
+ it 'can parse command with arguments' do
97
+ cli = Cliqr.interface do
98
+ basename 'my-command'
99
+ handler TestCommand
100
+ arguments :enable
101
+
102
+ option 'test-option' do
103
+ short 't'
104
+ end
105
+ end
106
+ parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
107
+ :options => {},
108
+ :arguments => ['value1'])
109
+ expect(PARSER.parse(cli.config, %w(value1))).to eq(parsed_input)
110
+ end
111
+
112
+ it 'can parse command with one option and one argument' do
113
+ parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
114
+ :options => [
115
+ {
116
+ :name => 'test-option',
117
+ :value => 'abcd'
118
+ }],
119
+ :arguments => ['value1'])
120
+ cli = Cliqr.interface do
121
+ basename 'my-command'
122
+ handler TestCommand
123
+ arguments :enable
124
+
125
+ option 'test-option' do
126
+ short 't'
127
+ end
128
+ end
129
+
130
+ expect(PARSER.parse(cli.config, %w(-t abcd value1))).to eq(parsed_input)
131
+ expect(PARSER.parse(cli.config, %w(value1 -t abcd))).to eq(parsed_input)
132
+ end
133
+
134
+ it 'can parse command with a mix of options and arguments' do
135
+ cli = Cliqr.interface do
136
+ basename 'my-command'
137
+ handler TestCommand
138
+ arguments :enable
139
+
140
+ option 'test-option-1' do
141
+ short 't'
142
+ end
143
+
144
+ option 'test-option-2' do
145
+ short 'p'
146
+ end
147
+ end
148
+ config = cli.config
149
+ parsed_input = Cliqr::Parser::ParsedInput.new(:command => 'my-command',
150
+ :arguments => %w(value1 value2),
151
+ :options => [
152
+ {
153
+ :name => 'test-option-1',
154
+ :value => 'abcd'
155
+ },
156
+ {
157
+ :name => 'test-option-2',
158
+ :value => 'qwe'
159
+ }
160
+ ])
161
+ expect(PARSER.parse(config, %w(-t abcd -p qwe value1 value2))).to eq(parsed_input)
162
+ expect(PARSER.parse(config, %w(value1 -t abcd value2 -p qwe))).to eq(parsed_input)
163
+ expect(PARSER.parse(config, %w(-t abcd value1 -p qwe value2))).to eq(parsed_input)
164
+ end
94
165
  end
@@ -93,6 +93,7 @@ false
93
93
  cli = Cliqr.interface do
94
94
  basename 'my-command'
95
95
  handler TestCommand
96
+ arguments :disable
96
97
 
97
98
  option 'opt' do
98
99
  type :boolean
@@ -2,10 +2,10 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Cliqr::Validation do
5
+ describe Cliqr::ConfigValidation do
6
6
  it 'does not know how to validate unknown types' do
7
7
  expect do
8
- Cliqr::Validation::ValidatorFactory.get(:bla, {}).validate(nil, nil, nil)
8
+ Cliqr::ConfigValidation::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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cliqr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.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-04 00:00:00.000000000 Z
11
+ date: 2015-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: log4r
@@ -65,19 +65,21 @@ files:
65
65
  - lib/cliqr/cli/executor.rb
66
66
  - lib/cliqr/cli/interface.rb
67
67
  - lib/cliqr/cli/router.rb
68
+ - lib/cliqr/config_validation/validation_set.rb
69
+ - lib/cliqr/config_validation/validator_factory.rb
70
+ - lib/cliqr/config_validation/verifiable.rb
68
71
  - lib/cliqr/dsl.rb
69
72
  - lib/cliqr/error.rb
70
73
  - lib/cliqr/parser/argument_parser.rb
74
+ - lib/cliqr/parser/argument_token.rb
71
75
  - lib/cliqr/parser/argument_tree_walker.rb
72
76
  - lib/cliqr/parser/boolean_option_token.rb
77
+ - lib/cliqr/parser/option_token.rb
73
78
  - lib/cliqr/parser/parsed_input.rb
74
79
  - lib/cliqr/parser/parsed_input_builder.rb
75
80
  - lib/cliqr/parser/single_valued_option_token.rb
76
81
  - lib/cliqr/parser/token.rb
77
82
  - lib/cliqr/parser/token_factory.rb
78
- - lib/cliqr/validation/validation_set.rb
79
- - lib/cliqr/validation/validator_factory.rb
80
- - lib/cliqr/validation/verifiable.rb
81
83
  - lib/cliqr/validation_errors.rb
82
84
  - lib/cliqr/version.rb
83
85
  - spec/config/config_finalize_spec.rb
@@ -87,6 +89,7 @@ files:
87
89
  - spec/executor/command_runner_spec.rb
88
90
  - spec/executor/executor_spec.rb
89
91
  - spec/fixtures/always_error_command.rb
92
+ - spec/fixtures/argument_reader_command.rb
90
93
  - spec/fixtures/option_reader_command.rb
91
94
  - spec/fixtures/test_command.rb
92
95
  - spec/fixtures/test_option_checker_command.rb
@@ -128,6 +131,7 @@ test_files:
128
131
  - spec/executor/command_runner_spec.rb
129
132
  - spec/executor/executor_spec.rb
130
133
  - spec/fixtures/always_error_command.rb
134
+ - spec/fixtures/argument_reader_command.rb
131
135
  - spec/fixtures/option_reader_command.rb
132
136
  - spec/fixtures/test_command.rb
133
137
  - spec/fixtures/test_option_checker_command.rb