cliqr 0.0.4 → 0.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.
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
+ require 'cliqr/cli/command_runner_factory'
6
+
5
7
  describe Cliqr::CLI::CommandRunnerFactory do
6
8
  it 'returns standard runner for default output' do
7
9
  runner = Cliqr::CLI::CommandRunnerFactory.get(output: :default)
@@ -18,8 +20,4 @@ describe Cliqr::CLI::CommandRunnerFactory do
18
20
  raise_error(be_kind_of(Cliqr::Error::UnknownCommandRunnerException))
19
21
  )
20
22
  end
21
-
22
- it 'returns code 0 for default command runner' do
23
- expect(Cliqr.command.new.execute).to eq(0)
24
- end
25
23
  end
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ require 'cliqr/error'
6
+
7
+ require 'fixtures/test_command'
8
+ require 'fixtures/always_error_command'
9
+ require 'fixtures/option_reader_command'
10
+ require 'fixtures/test_option_reader_command'
11
+
12
+ describe Cliqr::CLI::Executor do
13
+ it 'returns code 0 for default command runner' do
14
+ expect(Cliqr.command.new.execute).to eq(0)
15
+ end
16
+
17
+ it 'routes base command with no arguments' do
18
+ cli = Cliqr.interface do
19
+ basename 'my-command'
20
+ handler TestCommand
21
+ end
22
+ result = cli.execute [], output: :buffer
23
+ expect(result[:stdout]).to eq "test command executed\n"
24
+ end
25
+
26
+ it 'handles error appropriately' do
27
+ cli = Cliqr.interface do
28
+ basename 'my-command'
29
+ handler AlwaysErrorCommand
30
+ end
31
+ expect { cli.execute [] }.to raise_error(Cliqr::Error::CommandRuntimeException)
32
+ end
33
+
34
+ it 'routes a command with option values' do
35
+ cli = Cliqr.interface do
36
+ basename 'my-command'
37
+ handler TestCommand
38
+
39
+ option 'test-option'
40
+ end
41
+ result = cli.execute %w(--test-option some-value), output: :buffer
42
+ expect(result[:stdout]).to eq "test command executed\n"
43
+ end
44
+
45
+ it 'lets a command get all option values' do
46
+ cli = Cliqr.interface do
47
+ basename 'my-command'
48
+ handler OptionReaderCommand
49
+
50
+ option 'test-option'
51
+ end
52
+ result = cli.execute %w(--test-option some-value), output: :buffer
53
+ expect(result[:stdout]).to eq <<-EOS
54
+ my-command
55
+
56
+ [option] test-option => some-value
57
+ EOS
58
+ end
59
+
60
+ it 'lets a command get single option value' do
61
+ cli = Cliqr.interface do
62
+ basename 'my-command'
63
+ handler TestOptionReaderCommand
64
+
65
+ option 'test-option'
66
+ end
67
+ result = cli.execute %w(--test-option some-value), output: :buffer
68
+ expect(result[:stdout]).to eq <<-EOS
69
+ some-value
70
+ EOS
71
+ end
72
+
73
+ it 'handles executor error cause properly' do
74
+ cli = Cliqr.interface do
75
+ basename 'my-command'
76
+ handler AlwaysErrorCommand
77
+ end
78
+ begin
79
+ cli.execute
80
+ rescue Cliqr::Error::CliqrError => e
81
+ expect(e.backtrace[0]).to end_with "cliqr/spec/fixtures/always_error_command.rb:6:in `execute'"
82
+ expect(e.message).to eq "command 'my-command' failed\n\nCause: StandardError - I always throw an error"
83
+ end
84
+ end
85
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  # A command that always throws an error
4
4
  class AlwaysErrorCommand < Cliqr.command
5
- def execute
5
+ def execute(_context)
6
6
  fail StandardError, 'I always throw an error'
7
7
  end
8
8
  end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ # A command that echoes the options back
4
+ class OptionReaderCommand < Cliqr.command
5
+ def execute(context)
6
+ puts "#{context.command}\n\n"
7
+ context.options.each do |option|
8
+ puts "[option] #{option.name} => #{option.value}"
9
+ end
10
+ end
11
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  # Test command for the router_spec
4
4
  class TestCommand < Cliqr.command
5
- def execute
5
+ def execute(_context)
6
6
  puts 'test command executed'
7
7
  end
8
8
  end
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ # A command that echoes the value for option named 'test-option'
4
+ class TestOptionReaderCommand < Cliqr.command
5
+ def execute(context)
6
+ puts context.option('test-option').value
7
+ end
8
+ end
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ require 'cliqr/cli/parser/argument_parser'
6
+
7
+ require 'fixtures/test_command'
8
+ require 'fixtures/option_reader_command'
9
+
10
+ describe Cliqr::CLI::Parser do
11
+ TEST_CLI = Cliqr.interface do
12
+ basename 'my-command'
13
+ handler TestCommand
14
+
15
+ option 'test-option' do
16
+ short 't'
17
+ end
18
+ end
19
+ CONFIG = TEST_CLI.config
20
+ PARSER = Cliqr::CLI::Parser
21
+
22
+ it 'can parse no argument command' do
23
+ expect(PARSER.parse(CONFIG, [])).to eq(:command => 'my-command',
24
+ :options => [])
25
+ end
26
+
27
+ it 'can parse command with option using long name' do
28
+ expected_arguments = {
29
+ :command => 'my-command',
30
+ :options => [
31
+ {
32
+ :name => 'test-option',
33
+ :value => 'abcd'
34
+ }
35
+ ]
36
+ }
37
+ expect(PARSER.parse(CONFIG, %w(--test-option abcd))).to eq(expected_arguments)
38
+ end
39
+
40
+ it 'can parse multiple options' do
41
+ expected_arguments = {
42
+ :command => 'my-command',
43
+ :options => [
44
+ {
45
+ :name => 'test-option-1',
46
+ :value => 'abcd'
47
+ },
48
+ {
49
+ :name => 'test-option-2',
50
+ :value => 'xyz'
51
+ }
52
+ ]
53
+ }
54
+ cli = Cliqr.interface do
55
+ basename 'my-command'
56
+ handler TestCommand
57
+
58
+ option 'test-option-1'
59
+ option 'test-option-2'
60
+ end
61
+ expect(Cliqr::CLI::Parser.parse(cli.config, %w(--test-option-1 abcd --test-option-2 xyz))).to eq(expected_arguments)
62
+ end
63
+
64
+ it 'can parse command with option using short name' do
65
+ expected_arguments = {
66
+ :command => 'my-command',
67
+ :options => [
68
+ {
69
+ :name => 'test-option',
70
+ :value => 'abcd'
71
+ }
72
+ ]
73
+ }
74
+ expect(PARSER.parse(CONFIG, %w(-t abcd))).to eq(expected_arguments)
75
+ end
76
+
77
+ it 'cannot parse unknown options' do
78
+ expect { PARSER.parse(CONFIG, %w(--unknown-option abcd)) }.to(
79
+ raise_error(Cliqr::Error::UnknownCommandOption, 'unknown option "--unknown-option"'))
80
+ expect { PARSER.parse(CONFIG, %w(-u abcd)) }.to(
81
+ raise_error(Cliqr::Error::UnknownCommandOption, 'unknown option "-u"'))
82
+ end
83
+
84
+ it 'cannot parse invalid options' do
85
+ expect { PARSER.parse(CONFIG, %w(--1)) }.to(
86
+ raise_error(Cliqr::Error::InvalidArgumentError, 'invalid command argument "--1"'))
87
+ expect { PARSER.parse(CONFIG, %w(-$)) }.to(
88
+ raise_error(Cliqr::Error::InvalidArgumentError, 'invalid command argument "-$"'))
89
+ end
90
+
91
+ it 'cannot parse option without value if required' do
92
+ expect { PARSER.parse(CONFIG, %w(--test-option)) }.to(
93
+ raise_error(Cliqr::Error::OptionValueMissing, 'a value must be defined for option "--test-option"'))
94
+ end
95
+
96
+ it 'cannot parse option if it has multiple values' do
97
+ expect { PARSER.parse(CONFIG, %w(--test-option val1 --test-option val2)) }.to(
98
+ raise_error(Cliqr::Error::MultipleOptionValues, 'multiple values for option "--test-option"'))
99
+ end
100
+ 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: 0.0.4
4
+ version: 0.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-05-11 00:00:00.000000000 Z
11
+ date: 2015-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: log4r
@@ -55,13 +55,21 @@ files:
55
55
  - README.md
56
56
  - Rakefile
57
57
  - lib/cliqr.rb
58
- - lib/cliqr/cli/builder.rb
58
+ - lib/cliqr/cli/argument_validator.rb
59
59
  - lib/cliqr/cli/command.rb
60
+ - lib/cliqr/cli/command_context.rb
60
61
  - lib/cliqr/cli/command_runner_factory.rb
61
62
  - lib/cliqr/cli/config.rb
63
+ - lib/cliqr/cli/config_validator.rb
64
+ - lib/cliqr/cli/executor.rb
62
65
  - lib/cliqr/cli/interface.rb
66
+ - lib/cliqr/cli/parser/argument_parser.rb
67
+ - lib/cliqr/cli/parser/argument_tree_walker.rb
68
+ - lib/cliqr/cli/parser/option_token.rb
69
+ - lib/cliqr/cli/parser/parsed_argument_builder.rb
70
+ - lib/cliqr/cli/parser/token.rb
71
+ - lib/cliqr/cli/parser/token_factory.rb
63
72
  - lib/cliqr/cli/router.rb
64
- - lib/cliqr/cli/validator.rb
65
73
  - lib/cliqr/dsl.rb
66
74
  - lib/cliqr/error.rb
67
75
  - lib/cliqr/version.rb
@@ -69,9 +77,12 @@ files:
69
77
  - spec/config/config_validator_spec.rb
70
78
  - spec/dsl/interface_spec.rb
71
79
  - spec/executor/command_runner_spec.rb
72
- - spec/executor/fixtures/always_error_command.rb
73
- - spec/executor/fixtures/test_command.rb
74
- - spec/executor/router_spec.rb
80
+ - spec/executor/executor_spec.rb
81
+ - spec/fixtures/always_error_command.rb
82
+ - spec/fixtures/option_reader_command.rb
83
+ - spec/fixtures/test_command.rb
84
+ - spec/fixtures/test_option_reader_command.rb
85
+ - spec/parser/argument_parser_spec.rb
75
86
  - spec/spec_helper.rb
76
87
  homepage: https://github.com/anshulverma/cliqr
77
88
  licenses:
@@ -102,8 +113,11 @@ test_files:
102
113
  - spec/config/config_validator_spec.rb
103
114
  - spec/dsl/interface_spec.rb
104
115
  - spec/executor/command_runner_spec.rb
105
- - spec/executor/fixtures/always_error_command.rb
106
- - spec/executor/fixtures/test_command.rb
107
- - spec/executor/router_spec.rb
116
+ - spec/executor/executor_spec.rb
117
+ - spec/fixtures/always_error_command.rb
118
+ - spec/fixtures/option_reader_command.rb
119
+ - spec/fixtures/test_command.rb
120
+ - spec/fixtures/test_option_reader_command.rb
121
+ - spec/parser/argument_parser_spec.rb
108
122
  - spec/spec_helper.rb
109
123
  has_rdoc:
@@ -1,31 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'cliqr/cli/validator'
4
- require 'cliqr/cli/interface'
5
-
6
- module Cliqr
7
- module CLI
8
- # Builds usage information from [CLI::Config]
9
- #
10
- # @api private
11
- class Builder
12
- # Start building a command line interface
13
- #
14
- # @param [Cliqr::CLI::Config] config the configuration options for the
15
- # interface (validated using CLI::Validator)
16
- #
17
- # @return [Cliqr::CLI::Builder]
18
- def initialize(config)
19
- @config = config
20
- end
21
-
22
- # Validate and build a cli interface based on the configuration options
23
- #
24
- # @return [Cliqr::CLI::Interface]
25
- def build
26
- CLI::Validator.validate @config
27
- Interface.new(@config)
28
- end
29
- end
30
- end
31
- end
@@ -1,31 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'cliqr/error'
4
-
5
- module Cliqr
6
- module CLI
7
- # Validator for the command line interface configuration
8
- #
9
- # @api private
10
- class Validator
11
- # Validates the config to make sure all the options are correctly set
12
- #
13
- # @param [Cliqr::CLI::Config] config Settings for building command line interface
14
- #
15
- # @return [Cliqr::CLI::Config] Validated config object
16
- def self.validate(config)
17
- fail Cliqr::Error::ConfigNotFound, 'config is nil' if config.nil?
18
- fail Cliqr::Error::BasenameNotDefined, 'basename is not defined' if config.basename.empty?
19
-
20
- fail Cliqr::Error::HandlerNotDefined, 'command handler not defined' if config.handler.nil?
21
- fail Cliqr::Error::InvalidCommandHandler,
22
- 'command handler must extend from Cliqr::CLI::Command' unless config.handler < Command
23
-
24
- fail Cliqr::Error::OptionsNotDefinedException,
25
- 'options cannot be nil' if config.options.nil?
26
-
27
- config
28
- end
29
- end
30
- end
31
- end
@@ -1,25 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- require 'executor/fixtures/test_command'
6
- require 'executor/fixtures/always_error_command'
7
-
8
- describe Cliqr::CLI::Router do
9
- it 'routes base command with no arguments' do
10
- cli = Cliqr.interface do
11
- basename 'my-command'
12
- handler TestCommand
13
- end
14
- result = cli.execute output: :buffer
15
- expect(result[:stdout]).to eq "test command executed\n"
16
- end
17
-
18
- it 'handles error appropriately' do
19
- cli = Cliqr.interface do
20
- basename 'my-command'
21
- handler AlwaysErrorCommand
22
- end
23
- expect { cli.execute }.to raise_error(Cliqr::Error::CommandRuntimeException)
24
- end
25
- end