cliqr 0.1.0 → 1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +55 -0
- data/README.md +46 -17
- data/lib/cliqr/argument_validation/argument_type_validator.rb +31 -0
- data/lib/cliqr/argument_validation/option_validator.rb +27 -0
- data/lib/cliqr/argument_validation/validator.rb +67 -0
- data/lib/cliqr/cli/command_context.rb +21 -12
- data/lib/cliqr/cli/config.rb +57 -9
- data/lib/cliqr/cli/executor.rb +17 -11
- data/lib/cliqr/cli/interface.rb +7 -3
- data/lib/cliqr/error.rb +17 -19
- data/lib/cliqr/parser/argument_parser.rb +21 -0
- data/lib/cliqr/parser/argument_tree_walker.rb +51 -0
- data/lib/cliqr/parser/boolean_option_token.rb +26 -0
- data/lib/cliqr/parser/parsed_input.rb +53 -0
- data/lib/cliqr/parser/parsed_input_builder.rb +61 -0
- data/lib/cliqr/parser/single_valued_option_token.rb +53 -0
- data/lib/cliqr/parser/token.rb +45 -0
- data/lib/cliqr/parser/token_factory.rb +69 -0
- data/lib/cliqr/validation/validation_set.rb +48 -0
- data/lib/cliqr/validation/validator_factory.rb +265 -0
- data/lib/cliqr/validation/verifiable.rb +89 -0
- data/lib/cliqr/validation_errors.rb +61 -0
- data/lib/cliqr/version.rb +1 -1
- data/spec/config/config_validator_spec.rb +51 -30
- data/spec/config/option_config_validator_spec.rb +143 -0
- data/spec/dsl/interface_spec.rb +48 -114
- data/spec/executor/executor_spec.rb +19 -1
- data/spec/fixtures/test_option_checker_command.rb +8 -0
- data/spec/parser/argument_parser_spec.rb +33 -39
- data/spec/validation/argument_validation_spec.rb +141 -0
- data/spec/validation/error_spec.rb +22 -0
- data/spec/validation/validation_spec.rb +11 -0
- metadata +27 -10
- data/lib/cliqr/cli/argument_validator.rb +0 -19
- data/lib/cliqr/cli/config_validator.rb +0 -104
- data/lib/cliqr/cli/parser/argument_parser.rb +0 -23
- data/lib/cliqr/cli/parser/argument_tree_walker.rb +0 -56
- data/lib/cliqr/cli/parser/option_token.rb +0 -72
- data/lib/cliqr/cli/parser/parsed_argument_builder.rb +0 -66
- data/lib/cliqr/cli/parser/token.rb +0 -38
- data/lib/cliqr/cli/parser/token_factory.rb +0 -58
@@ -1,23 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'cliqr/cli/parser/argument_tree_walker'
|
4
|
-
|
5
|
-
module Cliqr
|
6
|
-
module CLI
|
7
|
-
# A set of utility methods and classes used to parse the command line arguments
|
8
|
-
#
|
9
|
-
# @api private
|
10
|
-
module Parser
|
11
|
-
# Parse command line arguments based on [Cliqr::CLI::Config]
|
12
|
-
#
|
13
|
-
# @param [Cliqr::CLI::Config] config Command line configuration
|
14
|
-
# @param [Array<String>] args An array of arguments from command line
|
15
|
-
#
|
16
|
-
# @return [Hash] Parsed hash of command linet arguments
|
17
|
-
def self.parse(config, args)
|
18
|
-
tree_walker = ArgumentTreeWalker.new(config)
|
19
|
-
tree_walker.walk(args)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'cliqr/cli/parser/parsed_argument_builder'
|
4
|
-
require 'cliqr/cli/parser/token_factory'
|
5
|
-
|
6
|
-
module Cliqr
|
7
|
-
module CLI
|
8
|
-
module Parser
|
9
|
-
# Walks the list of arguments and parses them one token at a time
|
10
|
-
#
|
11
|
-
# @api private
|
12
|
-
class ArgumentTreeWalker
|
13
|
-
# Create a new instance
|
14
|
-
#
|
15
|
-
# @param [Cliqr::CLI::Config] config Configuration settings for the command line interface
|
16
|
-
#
|
17
|
-
# @return [Cliqr::CLI::Parser::ArgumentTreeWalker]
|
18
|
-
def initialize(config)
|
19
|
-
@config = config
|
20
|
-
end
|
21
|
-
|
22
|
-
# Parse the arguments and generate tokens by iterating over command line arguments
|
23
|
-
#
|
24
|
-
# @param [Array<String>] args List of arguments that needs to parsed
|
25
|
-
#
|
26
|
-
# @return [Hash] Parsed hash of command line arguments
|
27
|
-
def walk(args)
|
28
|
-
argument_builder = ParsedArgumentBuilder.new(@config)
|
29
|
-
token_factory = TokenFactory.new(@config)
|
30
|
-
token = token_factory.get_token
|
31
|
-
args.each do |arg|
|
32
|
-
token = handle_argument(arg, token, argument_builder, token_factory)
|
33
|
-
end
|
34
|
-
token.finalize
|
35
|
-
argument_builder.build
|
36
|
-
end
|
37
|
-
|
38
|
-
# Handle the next argument in the context of the current token
|
39
|
-
#
|
40
|
-
# @return [Cliqr::CLI::Parser::Token] The new active token in case <tt>current_token</tt>
|
41
|
-
# becomes inactive
|
42
|
-
def handle_argument(arg, current_token, argument_builder, token_factory)
|
43
|
-
if current_token.active?
|
44
|
-
current_token.append(arg)
|
45
|
-
arg = nil
|
46
|
-
end
|
47
|
-
unless current_token.active?
|
48
|
-
argument_builder.add_token(current_token)
|
49
|
-
current_token = token_factory.get_token(arg)
|
50
|
-
end
|
51
|
-
current_token
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Cliqr
|
4
|
-
module CLI
|
5
|
-
module Parser
|
6
|
-
# Token handler for parsing a option and its value
|
7
|
-
#
|
8
|
-
# @api private
|
9
|
-
class OptionToken < Token
|
10
|
-
# Name of the option token
|
11
|
-
#
|
12
|
-
# @return [String]
|
13
|
-
attr_accessor :name
|
14
|
-
|
15
|
-
# Argument that was used to parse the option name from
|
16
|
-
#
|
17
|
-
# @return [String]
|
18
|
-
attr_accessor :arg
|
19
|
-
|
20
|
-
# Create a new option token. Initial state will be <tt>active</tt>
|
21
|
-
#
|
22
|
-
# @param [String] name Long name of the option
|
23
|
-
# @param [String] arg Value of the option
|
24
|
-
#
|
25
|
-
# @return [Cliqr::CLI::Parser::OptionToken] A new Token instance
|
26
|
-
def initialize(name, arg)
|
27
|
-
@name = name
|
28
|
-
@arg = arg
|
29
|
-
|
30
|
-
@value = nil
|
31
|
-
@active = true
|
32
|
-
@type = :option
|
33
|
-
end
|
34
|
-
|
35
|
-
# Check if the token handler is active and needs more arguments
|
36
|
-
#
|
37
|
-
# @return [Boolean] <tt>true</tt> if the token handler is active
|
38
|
-
def active?
|
39
|
-
@active
|
40
|
-
end
|
41
|
-
|
42
|
-
# Append the next argument in the series and set token to inactive
|
43
|
-
#
|
44
|
-
# @param [String] arg Argument value of the next command line parameter
|
45
|
-
#
|
46
|
-
# @return [Boolean] Active state of the token handler
|
47
|
-
def append(arg)
|
48
|
-
@value = arg
|
49
|
-
@active = false
|
50
|
-
end
|
51
|
-
|
52
|
-
# Get the token representation
|
53
|
-
#
|
54
|
-
# @return [Hash] A hash of the token parameters and their values
|
55
|
-
def build
|
56
|
-
{
|
57
|
-
:name => @name.to_s,
|
58
|
-
:value => @value
|
59
|
-
}
|
60
|
-
end
|
61
|
-
|
62
|
-
# Called if this token handler was still active once the argument list ends
|
63
|
-
#
|
64
|
-
# @return [Cliqr::CLI::Parser::OptionToken] Current instance object
|
65
|
-
def finalize
|
66
|
-
# should not be called
|
67
|
-
fail Cliqr::Error::OptionValueMissing, "a value must be defined for option \"#{@arg}\""
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
@@ -1,66 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Cliqr
|
4
|
-
module CLI
|
5
|
-
module Parser
|
6
|
-
# Builder for collecting parsed command line arguments that can be used to
|
7
|
-
# build a command context
|
8
|
-
#
|
9
|
-
# @api private
|
10
|
-
class ParsedArgumentBuilder
|
11
|
-
# Initialize a new instance
|
12
|
-
#
|
13
|
-
# @param [Cliqr::CLI::Config] config Configuration settings for the command line interface
|
14
|
-
#
|
15
|
-
# @return [Cliqr::CLI::Parser::ParsedArgumentBuilder]
|
16
|
-
def initialize(config)
|
17
|
-
@config = config
|
18
|
-
@options = []
|
19
|
-
@option_names = Set.new
|
20
|
-
end
|
21
|
-
|
22
|
-
# Add a new parsed token from the list of arguments
|
23
|
-
#
|
24
|
-
# @param [Cliqr::CLI::Parser::Token] token A parsed token from command line arguments
|
25
|
-
#
|
26
|
-
# @return [Boolean] <tt>true</tt> if the token was added
|
27
|
-
def add_token(token)
|
28
|
-
case token.type
|
29
|
-
when :option
|
30
|
-
add_option_name(token)
|
31
|
-
@options.push(token.build)
|
32
|
-
else
|
33
|
-
return false
|
34
|
-
end
|
35
|
-
true
|
36
|
-
end
|
37
|
-
|
38
|
-
# Build the hash of parsed command line arguments
|
39
|
-
#
|
40
|
-
# @return [Hash] Parsed arguments
|
41
|
-
def build
|
42
|
-
{
|
43
|
-
:command => @config.basename,
|
44
|
-
:options => @options
|
45
|
-
}
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
# Add option's name to a list of already added options and fail if duplicate
|
51
|
-
#
|
52
|
-
# @param [Cliqr::CLI::Parser::Token] token A parsed token from command line arguments
|
53
|
-
#
|
54
|
-
# @return [Set<String>] Current list of option names
|
55
|
-
def add_option_name(token)
|
56
|
-
option_config = @config.option(token.name)
|
57
|
-
old_config = @option_names.add?(option_config.name)
|
58
|
-
fail Cliqr::Error::MultipleOptionValues,
|
59
|
-
"multiple values for option \"#{token.arg}\"" if old_config.nil?
|
60
|
-
@option_names.add(option_config.short) if option_config.short?
|
61
|
-
@option_names
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Cliqr
|
4
|
-
module CLI
|
5
|
-
module Parser
|
6
|
-
# A NO-OP argument token
|
7
|
-
#
|
8
|
-
# @api private
|
9
|
-
class Token
|
10
|
-
# Type of the token handler (:option in this case)
|
11
|
-
#
|
12
|
-
# @return [Symbol]
|
13
|
-
attr_accessor :type
|
14
|
-
|
15
|
-
# Create a new NO OP token
|
16
|
-
#
|
17
|
-
# @return [Cliqr::CLI::Parser::Token]
|
18
|
-
def initialize
|
19
|
-
@type = :NO_OP
|
20
|
-
end
|
21
|
-
|
22
|
-
# This token is never active
|
23
|
-
#
|
24
|
-
# @return [Boolean] This will always return <tt>false</tt> in this case
|
25
|
-
def active?
|
26
|
-
false
|
27
|
-
end
|
28
|
-
|
29
|
-
# Called if this token was still active once the argument list ends
|
30
|
-
#
|
31
|
-
# @return [Cliqr::CLI::Parser::TokenHandler] Current instance object
|
32
|
-
def finalize
|
33
|
-
self
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'cliqr/cli/parser/token'
|
4
|
-
require 'cliqr/cli/parser/option_token'
|
5
|
-
|
6
|
-
module Cliqr
|
7
|
-
module CLI
|
8
|
-
module Parser
|
9
|
-
# A factory class to get a instance of {Cliqr::CLI::Parser::Token}
|
10
|
-
# based on the argument
|
11
|
-
#
|
12
|
-
# @api private
|
13
|
-
class TokenFactory
|
14
|
-
# Create a new token factory instance
|
15
|
-
#
|
16
|
-
# @param [Cliqr::CLI::Config] config Command line interface configuration
|
17
|
-
#
|
18
|
-
# @return [Cliqr::CLI::Parser::TokenFactory]
|
19
|
-
def initialize(config)
|
20
|
-
@config = config
|
21
|
-
end
|
22
|
-
|
23
|
-
# Get a new instance of {Cliqr::CLI::Parser::Token} based on the argument
|
24
|
-
#
|
25
|
-
# @param [String] arg The argument used to get a token instance (default nil)
|
26
|
-
#
|
27
|
-
# @return [Cliqr::CLI::Parser::Token]
|
28
|
-
def get_token(arg = nil)
|
29
|
-
if arg.nil?
|
30
|
-
Token.new
|
31
|
-
else
|
32
|
-
case arg
|
33
|
-
when /^--([a-zA-Z][a-zA-Z0-9\-_]*)$/, /^-([a-zA-Z])$/
|
34
|
-
option_config = get_option_config(Regexp.last_match(1), arg)
|
35
|
-
OptionToken.new(option_config.name, arg)
|
36
|
-
else
|
37
|
-
fail Cliqr::Error::InvalidArgumentError, "invalid command argument \"#{arg}\""
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
# Check if a option is defined with the requested name then return it
|
45
|
-
#
|
46
|
-
# @param [String] name Long name of the option
|
47
|
-
# @param [String] arg THe argument that was parsed to get the option name
|
48
|
-
#
|
49
|
-
# @return [Cliqr::CLI::OptionConfig] Requested option configuration
|
50
|
-
def get_option_config(name, arg)
|
51
|
-
fail Cliqr::Error::UnknownCommandOption,
|
52
|
-
"unknown option \"#{arg}\"" unless @config.option?(name)
|
53
|
-
@config.option(name)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|