commander 4.2.1 → 4.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/.rubocop.yml +10 -0
- data/.rubocop_todo.yml +212 -0
- data/.travis.yml +0 -2
- data/Gemfile +1 -1
- data/History.rdoc +6 -0
- data/README.md +9 -4
- data/Rakefile +7 -4
- data/bin/commander +12 -12
- data/commander.gemspec +17 -18
- data/lib/commander/blank.rb +2 -2
- data/lib/commander/command.rb +63 -65
- data/lib/commander/configure.rb +1 -2
- data/lib/commander/core_ext.rb +1 -1
- data/lib/commander/core_ext/array.rb +2 -3
- data/lib/commander/core_ext/object.rb +1 -3
- data/lib/commander/delegates.rb +15 -4
- data/lib/commander/help_formatters.rb +2 -1
- data/lib/commander/help_formatters/base.rb +14 -7
- data/lib/commander/help_formatters/terminal.rb +5 -5
- data/lib/commander/help_formatters/terminal_compact.rb +2 -2
- data/lib/commander/methods.rb +0 -1
- data/lib/commander/runner.rb +1 -1
- data/lib/commander/user_interaction.rb +87 -91
- data/lib/commander/version.rb +1 -1
- data/spec/command_spec.rb +57 -58
- data/spec/configure_spec.rb +4 -4
- data/spec/core_ext/array_spec.rb +5 -7
- data/spec/core_ext/object_spec.rb +6 -8
- data/spec/help_formatters/terminal_spec.rb +19 -20
- data/spec/runner_spec.rb +179 -181
- data/spec/spec_helper.rb +12 -12
- data/spec/ui_spec.rb +10 -11
- metadata +23 -6
data/lib/commander/version.rb
CHANGED
data/spec/command_spec.rb
CHANGED
@@ -2,157 +2,156 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Commander::Command do
|
4
4
|
include Commander::Methods
|
5
|
-
|
5
|
+
|
6
6
|
before :each do
|
7
7
|
mock_terminal
|
8
8
|
create_test_command
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
describe 'Options' do
|
12
12
|
before :each do
|
13
|
-
@options = Commander::Command::Options.new
|
13
|
+
@options = Commander::Command::Options.new
|
14
14
|
end
|
15
|
-
|
16
|
-
it
|
15
|
+
|
16
|
+
it 'should act like an open struct' do
|
17
17
|
@options.send = 'mail'
|
18
18
|
@options.call = true
|
19
19
|
expect(@options.send).to eq('mail')
|
20
20
|
expect(@options.call).to eq(true)
|
21
21
|
end
|
22
|
-
|
23
|
-
it
|
22
|
+
|
23
|
+
it 'should allow __send__ to function as always' do
|
24
24
|
@options.send = 'foo'
|
25
25
|
expect(@options.__send__(:send)).to eq('foo')
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
29
|
-
describe
|
30
|
-
it
|
28
|
+
|
29
|
+
describe '#option' do
|
30
|
+
it 'should add options' do
|
31
31
|
expect { @command.option '--recursive' }.to change(@command.options, :length).from(1).to(2)
|
32
32
|
end
|
33
|
-
|
34
|
-
it
|
35
|
-
@command.option('--recursive') { |recursive| expect(recursive).to
|
33
|
+
|
34
|
+
it 'should allow procs as option handlers' do
|
35
|
+
@command.option('--recursive') { |recursive| expect(recursive).to be true }
|
36
36
|
@command.run '--recursive'
|
37
37
|
end
|
38
|
-
|
39
|
-
it
|
38
|
+
|
39
|
+
it 'should allow usage of common method names' do
|
40
40
|
@command.option '--open file'
|
41
|
-
@command.when_called { |_, options| expect(options.open).to eq('foo') }
|
41
|
+
@command.when_called { |_, options| expect(options.open).to eq('foo') }
|
42
42
|
@command.run '--open', 'foo'
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
46
|
-
describe
|
47
|
-
describe
|
48
|
-
it
|
49
|
-
@command.when_called { |args,
|
45
|
+
|
46
|
+
describe '#run' do
|
47
|
+
describe 'should invoke #when_called' do
|
48
|
+
it 'with arguments seperated from options' do
|
49
|
+
@command.when_called { |args, _options| expect(args.join(' ')).to eq('just some args') }
|
50
50
|
@command.run '--verbose', 'just', 'some', 'args'
|
51
51
|
end
|
52
|
-
|
53
|
-
it
|
52
|
+
|
53
|
+
it 'calling the #call method by default when an object is called' do
|
54
54
|
object = double 'Object'
|
55
55
|
expect(object).to receive(:call).once
|
56
56
|
@command.when_called object
|
57
|
-
@command.run 'foo'
|
57
|
+
@command.run 'foo'
|
58
58
|
end
|
59
|
-
|
60
|
-
it
|
59
|
+
|
60
|
+
it 'should allow #action as an alias to #when_called' do
|
61
61
|
object = double 'Object'
|
62
62
|
expect(object).to receive(:call).once
|
63
63
|
@command.action object
|
64
64
|
@command.run 'foo'
|
65
65
|
end
|
66
|
-
|
67
|
-
it
|
66
|
+
|
67
|
+
it 'calling an arbitrary method when an object is called' do
|
68
68
|
object = double 'Object'
|
69
69
|
expect(object).to receive(:foo).once
|
70
70
|
@command.when_called object, :foo
|
71
|
-
@command.run 'foo'
|
71
|
+
@command.run 'foo'
|
72
72
|
end
|
73
|
-
|
74
|
-
it
|
73
|
+
|
74
|
+
it 'should raise an error when no handler is present' do
|
75
75
|
expect { @command.when_called }.to raise_error(ArgumentError)
|
76
76
|
end
|
77
77
|
end
|
78
|
-
|
79
|
-
describe
|
80
|
-
it
|
78
|
+
|
79
|
+
describe 'should populate options with' do
|
80
|
+
it 'boolean values' do
|
81
81
|
@command.option '--[no-]toggle'
|
82
|
-
@command.when_called { |_, options| expect(options.toggle).to
|
82
|
+
@command.when_called { |_, options| expect(options.toggle).to be true }
|
83
83
|
@command.run '--toggle'
|
84
|
-
@command.when_called { |_, options| expect(options.toggle).to
|
84
|
+
@command.when_called { |_, options| expect(options.toggle).to be false }
|
85
85
|
@command.run '--no-toggle'
|
86
86
|
end
|
87
87
|
|
88
|
-
it
|
88
|
+
it 'mandatory arguments' do
|
89
89
|
@command.option '--file FILE'
|
90
|
-
@command.when_called { |_, options| expect(options.file).to eq('foo') }
|
90
|
+
@command.when_called { |_, options| expect(options.file).to eq('foo') }
|
91
91
|
@command.run '--file', 'foo'
|
92
92
|
expect { @command.run '--file' }.to raise_error(OptionParser::MissingArgument)
|
93
93
|
end
|
94
94
|
|
95
|
-
describe
|
95
|
+
describe 'optional arguments' do
|
96
96
|
before do
|
97
97
|
@command.option '--use-config [file] '
|
98
98
|
end
|
99
99
|
|
100
|
-
it
|
100
|
+
it 'should return the argument when provided' do
|
101
101
|
@command.when_called { |_, options| expect(options.use_config).to eq('foo') }
|
102
102
|
@command.run '--use-config', 'foo'
|
103
103
|
end
|
104
104
|
|
105
|
-
it
|
106
|
-
@command.when_called { |_, options| expect(options.use_config).to
|
105
|
+
it 'should return true when present without an argument' do
|
106
|
+
@command.when_called { |_, options| expect(options.use_config).to be true }
|
107
107
|
@command.run '--use-config'
|
108
108
|
end
|
109
109
|
|
110
|
-
it
|
110
|
+
it 'should return nil when not present' do
|
111
111
|
@command.when_called { |_, options| expect(options.use_config).to be_nil }
|
112
112
|
@command.run
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
|
-
describe
|
116
|
+
describe 'typed arguments' do
|
117
117
|
before do
|
118
118
|
@command.option '--interval N', Integer
|
119
119
|
end
|
120
120
|
|
121
|
-
it
|
121
|
+
it 'should parse valid values' do
|
122
122
|
@command.when_called { |_, options| expect(options.interval).to eq(5) }
|
123
123
|
@command.run '--interval', '5'
|
124
124
|
end
|
125
125
|
|
126
|
-
it
|
126
|
+
it 'should reject invalid values' do
|
127
127
|
expect { @command.run '--interval', 'invalid' }.to raise_error(OptionParser::InvalidArgument)
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
131
|
-
it
|
131
|
+
it 'lists' do
|
132
132
|
@command.option '--fav COLORS', Array
|
133
|
-
@command.when_called { |_, options| expect(options.fav).to eq(
|
133
|
+
@command.when_called { |_, options| expect(options.fav).to eq(%w(red green blue)) }
|
134
134
|
@command.run '--fav', 'red,green,blue'
|
135
135
|
end
|
136
|
-
|
137
|
-
it
|
136
|
+
|
137
|
+
it 'lists with multi-word items' do
|
138
138
|
@command.option '--fav MOVIES', Array
|
139
|
-
@command.when_called { |_, options| expect(options.fav).to eq(['super\ bad', 'nightmare']) }
|
140
|
-
@command.run '--fav', 'super\ bad,nightmare'
|
139
|
+
@command.when_called { |_, options| expect(options.fav).to eq(['super\ bad', 'nightmare']) }
|
140
|
+
@command.run '--fav', 'super\ bad,nightmare'
|
141
141
|
end
|
142
|
-
|
143
|
-
it
|
142
|
+
|
143
|
+
it 'defaults' do
|
144
144
|
@command.option '--files LIST', Array
|
145
145
|
@command.option '--interval N', Integer
|
146
146
|
@command.when_called do |_, options|
|
147
147
|
options.default \
|
148
|
-
:
|
149
|
-
:
|
150
|
-
expect(options.files).to eq(
|
148
|
+
files: %w(foo bar),
|
149
|
+
interval: 5
|
150
|
+
expect(options.files).to eq(%w(foo bar))
|
151
151
|
expect(options.interval).to eq(15)
|
152
152
|
end
|
153
153
|
@command.run '--interval', '15'
|
154
154
|
end
|
155
155
|
end
|
156
156
|
end
|
157
|
-
|
158
157
|
end
|
data/spec/configure_spec.rb
CHANGED
@@ -13,17 +13,17 @@ describe Commander do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'provides Commander configuration methods' do
|
16
|
-
Commander.configure
|
16
|
+
Commander.configure do
|
17
17
|
program :name, 'test'
|
18
|
-
|
18
|
+
end
|
19
19
|
|
20
20
|
expect(Commander::Runner.instance.program(:name)).to eq('test')
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'passes all arguments to the block' do
|
24
|
-
Commander.configure('foo')
|
24
|
+
Commander.configure('foo') do |first_arg|
|
25
25
|
program :name, first_arg
|
26
|
-
|
26
|
+
end
|
27
27
|
|
28
28
|
expect(Commander::Runner.instance.program(:name)).to eq('foo')
|
29
29
|
end
|
data/spec/core_ext/array_spec.rb
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Array do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
expect(Array.parse('just a test')).to eq(['just', 'a', 'test'])
|
4
|
+
describe '#parse' do
|
5
|
+
it 'should seperate a list of words into an array' do
|
6
|
+
expect(Array.parse('just a test')).to eq(%w(just a test))
|
8
7
|
end
|
9
8
|
|
10
|
-
it
|
9
|
+
it 'should preserve escaped whitespace' do
|
11
10
|
expect(Array.parse('just a\ test')).to eq(['just', 'a test'])
|
12
11
|
end
|
13
12
|
|
14
|
-
it
|
13
|
+
it 'should match %w behavior with multiple backslashes' do
|
15
14
|
str = 'just a\\ test'
|
16
15
|
expect(Array.parse(str)).to eq(eval("%w(#{str})"))
|
17
16
|
end
|
18
17
|
end
|
19
|
-
|
20
18
|
end
|
@@ -1,21 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Object do
|
4
|
-
|
5
|
-
|
6
|
-
it "should return the objects binding" do
|
4
|
+
describe '#get_binding' do
|
5
|
+
it 'should return the objects binding' do
|
7
6
|
expect(lambda {}.get_binding).to be_instance_of(Binding)
|
8
7
|
end
|
9
8
|
end
|
10
9
|
|
11
|
-
describe
|
12
|
-
it
|
13
|
-
expect { i_am_a_missing_method
|
10
|
+
describe '#method_missing' do
|
11
|
+
it 'should preserve its original behavior for missing methods' do
|
12
|
+
expect { send(:i_am_a_missing_method) }.to raise_error(NoMethodError)
|
14
13
|
end
|
15
14
|
|
16
|
-
it
|
15
|
+
it 'should preserve its original behavior for missing variables' do
|
17
16
|
expect { i_am_a_missing_variable }.to raise_error(NameError)
|
18
17
|
end
|
19
18
|
end
|
20
|
-
|
21
19
|
end
|
@@ -2,12 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Commander::HelpFormatter::Terminal do
|
4
4
|
include Commander::Methods
|
5
|
-
|
5
|
+
|
6
6
|
before :each do
|
7
7
|
mock_terminal
|
8
8
|
end
|
9
|
-
|
10
|
-
describe
|
9
|
+
|
10
|
+
describe 'global help' do
|
11
11
|
before :each do
|
12
12
|
new_command_runner 'help' do
|
13
13
|
command :'install gem' do |c|
|
@@ -17,19 +17,19 @@ describe Commander::HelpFormatter::Terminal do
|
|
17
17
|
end.run!
|
18
18
|
@global_help = @output.string
|
19
19
|
end
|
20
|
-
|
21
|
-
describe
|
22
|
-
it
|
20
|
+
|
21
|
+
describe 'should display' do
|
22
|
+
it 'the command name' do
|
23
23
|
expect(@global_help).to include('install gem')
|
24
24
|
end
|
25
|
-
|
26
|
-
it
|
25
|
+
|
26
|
+
it 'the summary' do
|
27
27
|
expect(@global_help).to include('Install some gem')
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
32
|
-
describe
|
31
|
+
|
32
|
+
describe 'command help' do
|
33
33
|
before :each do
|
34
34
|
new_command_runner 'help', 'install', 'gem' do
|
35
35
|
command :'install gem' do |c|
|
@@ -42,27 +42,26 @@ describe Commander::HelpFormatter::Terminal do
|
|
42
42
|
end.run!
|
43
43
|
@command_help = @output.string
|
44
44
|
end
|
45
|
-
|
46
|
-
describe
|
47
|
-
it
|
45
|
+
|
46
|
+
describe 'should display' do
|
47
|
+
it 'the command name' do
|
48
48
|
expect(@command_help).to include('install gem')
|
49
49
|
end
|
50
|
-
|
51
|
-
it
|
50
|
+
|
51
|
+
it 'the description' do
|
52
52
|
expect(@command_help).to include('Install some gem, blah blah blah')
|
53
53
|
end
|
54
|
-
|
55
|
-
it
|
54
|
+
|
55
|
+
it 'all examples' do
|
56
56
|
expect(@command_help).to include('# one')
|
57
57
|
expect(@command_help).to include('two')
|
58
58
|
expect(@command_help).to include('# three')
|
59
59
|
expect(@command_help).to include('four')
|
60
60
|
end
|
61
|
-
|
62
|
-
it
|
61
|
+
|
62
|
+
it 'the syntax' do
|
63
63
|
expect(@command_help).to include('foo install gem [options]')
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
67
|
-
|
68
67
|
end
|
data/spec/runner_spec.rb
CHANGED
@@ -8,111 +8,111 @@ describe Commander do
|
|
8
8
|
mock_terminal
|
9
9
|
create_test_command
|
10
10
|
end
|
11
|
-
|
12
|
-
describe
|
13
|
-
it
|
11
|
+
|
12
|
+
describe '#program' do
|
13
|
+
it 'should set / get program information' do
|
14
14
|
program :name, 'test'
|
15
15
|
expect(program(:name)).to eq('test')
|
16
16
|
end
|
17
|
-
|
18
|
-
it
|
17
|
+
|
18
|
+
it 'should allow arbitrary blocks of global help documentation' do
|
19
19
|
program :help, 'Copyright', 'TJ Holowaychuk'
|
20
20
|
expect(program(:help)['Copyright']).to eq('TJ Holowaychuk')
|
21
21
|
end
|
22
|
-
|
23
|
-
it
|
22
|
+
|
23
|
+
it 'should raise an error when required info has not been set' do
|
24
24
|
new_command_runner '--help'
|
25
25
|
program :version, ''
|
26
26
|
expect { run! }.to raise_error(Commander::Runner::CommandError)
|
27
27
|
end
|
28
|
-
|
29
|
-
it
|
28
|
+
|
29
|
+
it 'should allow aliases of help formatters' do
|
30
30
|
program :help_formatter, :compact
|
31
31
|
expect(program(:help_formatter)).to eq(Commander::HelpFormatter::TerminalCompact)
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
35
|
-
describe
|
36
|
-
it
|
34
|
+
|
35
|
+
describe '#command' do
|
36
|
+
it 'should return a command instance when only the name is passed' do
|
37
37
|
expect(command(:test)).to be_instance_of(Commander::Command)
|
38
38
|
end
|
39
|
-
|
40
|
-
it
|
39
|
+
|
40
|
+
it 'should return nil when the command does not exist' do
|
41
41
|
expect(command(:im_not_real)).to be_nil
|
42
42
|
end
|
43
43
|
end
|
44
|
-
|
45
|
-
describe
|
46
|
-
it
|
44
|
+
|
45
|
+
describe '#separate_switches_from_description' do
|
46
|
+
it 'should seperate switches and description returning both' do
|
47
47
|
switches, description = *Commander::Runner.separate_switches_from_description('-h', '--help', 'display help')
|
48
48
|
expect(switches).to eq(['-h', '--help'])
|
49
49
|
expect(description).to eq('display help')
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
53
|
-
describe
|
54
|
-
it
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
52
|
+
|
53
|
+
describe '#switch_to_sym' do
|
54
|
+
it 'should return a symbol based on the switch name' do
|
55
|
+
expect(Commander::Runner.switch_to_sym('--trace')).to eq(:trace)
|
56
|
+
expect(Commander::Runner.switch_to_sym('--foo-bar')).to eq(:foo_bar)
|
57
|
+
expect(Commander::Runner.switch_to_sym('--[no-]feature"')).to eq(:feature)
|
58
|
+
expect(Commander::Runner.switch_to_sym('--[no-]feature ARG')).to eq(:feature)
|
59
|
+
expect(Commander::Runner.switch_to_sym('--file [ARG]')).to eq(:file)
|
60
|
+
expect(Commander::Runner.switch_to_sym('--colors colors')).to eq(:colors)
|
61
61
|
end
|
62
62
|
end
|
63
|
-
|
64
|
-
describe
|
65
|
-
it
|
63
|
+
|
64
|
+
describe '#alias_command' do
|
65
|
+
it 'should alias a command' do
|
66
66
|
alias_command :foo, :test
|
67
67
|
expect(command(:foo)).to eq(command(:test))
|
68
68
|
end
|
69
|
-
|
70
|
-
it
|
69
|
+
|
70
|
+
it 'should pass arguments passed to the alias when called' do
|
71
71
|
gem_name = ''
|
72
72
|
new_command_runner 'install', 'gem', 'commander' do
|
73
73
|
command :install do |c|
|
74
74
|
c.option '--gem-name NAME', 'Install a gem'
|
75
75
|
c.when_called { |_, options| gem_name = options.gem_name }
|
76
|
-
end
|
76
|
+
end
|
77
77
|
alias_command :'install gem', :install, '--gem-name'
|
78
78
|
end.run!
|
79
79
|
expect(gem_name).to eq('commander')
|
80
80
|
end
|
81
81
|
end
|
82
|
-
|
83
|
-
describe
|
84
|
-
it
|
82
|
+
|
83
|
+
describe '#global_option' do
|
84
|
+
it 'should be invoked when used in the args list' do
|
85
85
|
file = ''
|
86
86
|
new_command_runner 'test', '--config', 'foo' do
|
87
87
|
global_option('--config FILE') { |f| file = f }
|
88
88
|
end.run!
|
89
89
|
expect(file).to eq('foo')
|
90
90
|
end
|
91
|
-
|
92
|
-
it
|
91
|
+
|
92
|
+
it 'should be inherited by commands' do
|
93
93
|
quiet = nil
|
94
94
|
new_command_runner 'foo', '--quiet' do
|
95
95
|
global_option('--quiet', 'Suppress output')
|
96
96
|
command :foo do |c|
|
97
|
-
c.when_called { |_, options| quiet = options.quiet }
|
97
|
+
c.when_called { |_, options| quiet = options.quiet }
|
98
98
|
end
|
99
99
|
end.run!
|
100
|
-
expect(quiet).to
|
100
|
+
expect(quiet).to be true
|
101
101
|
end
|
102
|
-
|
103
|
-
it
|
102
|
+
|
103
|
+
it 'should be inherited by commands even when a block is present' do
|
104
104
|
quiet = nil
|
105
105
|
new_command_runner 'foo', '--quiet' do
|
106
106
|
global_option('--quiet', 'Suppress output') {}
|
107
107
|
command :foo do |c|
|
108
|
-
c.when_called { |_, options| quiet = options.quiet }
|
108
|
+
c.when_called { |_, options| quiet = options.quiet }
|
109
109
|
end
|
110
110
|
end.run!
|
111
|
-
expect(quiet).to
|
111
|
+
expect(quiet).to be true
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
-
describe
|
115
|
+
describe '#parse_global_options' do
|
116
116
|
it 'should parse global options before command' do
|
117
117
|
global_option = nil
|
118
118
|
new_command_runner('--testing-global', 'foo') do
|
@@ -127,7 +127,7 @@ describe Commander do
|
|
127
127
|
|
128
128
|
it 'should parse global options after command' do
|
129
129
|
global_option = nil
|
130
|
-
new_command_runner('foo','--testing-global') do
|
130
|
+
new_command_runner('foo', '--testing-global') do
|
131
131
|
global_option('--testing-global') { global_option = 'MAGIC' }
|
132
132
|
|
133
133
|
command :foo do |c|
|
@@ -218,14 +218,14 @@ describe Commander do
|
|
218
218
|
global_option('--testing-global VALUE') { |v| global_option = v }
|
219
219
|
|
220
220
|
command :foo do |c|
|
221
|
-
c.option('--testing-command') {
|
221
|
+
c.option('--testing-command') {}
|
222
222
|
c.when_called {}
|
223
223
|
end
|
224
224
|
end.run!
|
225
225
|
|
226
226
|
expect(global_option).to eq('bar')
|
227
227
|
end
|
228
|
-
|
228
|
+
|
229
229
|
it 'should allow global arguments with values before command arguments (github issue #8)' do
|
230
230
|
global_option = nil
|
231
231
|
command_option = nil
|
@@ -243,13 +243,12 @@ describe Commander do
|
|
243
243
|
end
|
244
244
|
end
|
245
245
|
|
246
|
-
|
247
|
-
|
248
|
-
it "should remove only specified switches" do
|
246
|
+
describe '#remove_global_options' do
|
247
|
+
it 'should remove only specified switches' do
|
249
248
|
options, args = [], []
|
250
|
-
options << { :
|
251
|
-
options << { :
|
252
|
-
options << { :
|
249
|
+
options << { switches: ['-t', '--trace'] }
|
250
|
+
options << { switches: ['--help'] }
|
251
|
+
options << { switches: ['--paths PATHS'] }
|
253
252
|
args << '-t'
|
254
253
|
args << '--help'
|
255
254
|
args << '--command'
|
@@ -259,65 +258,65 @@ describe Commander do
|
|
259
258
|
expect(args).to eq(['--command', '--command-with-arg', 'rawr'])
|
260
259
|
end
|
261
260
|
|
262
|
-
it
|
261
|
+
it 'should not swallow an argument unless it expects an argument' do
|
263
262
|
options, args = [], []
|
264
|
-
options << { :
|
265
|
-
options << { :
|
266
|
-
options << { :
|
263
|
+
options << { switches: ['-n', '--no-arg'] }
|
264
|
+
options << { switches: ['-y', '--yes ARG'] }
|
265
|
+
options << { switches: ['-a', '--alternative=ARG'] }
|
267
266
|
args << '-n' << 'alpha'
|
268
267
|
args << '--yes' << 'deleted'
|
269
268
|
args << '-a' << 'deleted'
|
270
269
|
args << 'beta'
|
271
270
|
command_runner.remove_global_options options, args
|
272
|
-
expect(args).to eq(
|
271
|
+
expect(args).to eq(%w(alpha beta))
|
273
272
|
end
|
274
273
|
end
|
275
|
-
|
276
|
-
describe
|
277
|
-
it
|
278
|
-
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform
|
279
|
-
expect
|
274
|
+
|
275
|
+
describe '--trace' do
|
276
|
+
it 'should display pretty errors by default' do
|
277
|
+
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform.jruby?
|
278
|
+
expect do
|
280
279
|
new_command_runner 'foo' do
|
281
|
-
command(:foo) { |c| c.when_called {
|
280
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
282
281
|
end.run!
|
283
|
-
|
282
|
+
end.to raise_error(SystemExit, /error: cookies!. Use --trace/)
|
284
283
|
end
|
285
284
|
|
286
|
-
it
|
287
|
-
expect
|
285
|
+
it 'should display callstack when using this switch' do
|
286
|
+
expect do
|
288
287
|
new_command_runner 'foo', '--trace' do
|
289
|
-
command(:foo) { |c| c.when_called {
|
290
|
-
end.run!
|
291
|
-
|
288
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
289
|
+
end.run!
|
290
|
+
end.to raise_error(RuntimeError)
|
292
291
|
end
|
293
292
|
end
|
294
293
|
|
295
|
-
describe
|
296
|
-
it
|
297
|
-
expect
|
294
|
+
describe '#always_trace!' do
|
295
|
+
it 'should enable tracing globally, regardless of whether --trace was passed or not' do
|
296
|
+
expect do
|
298
297
|
new_command_runner 'foo' do
|
299
298
|
always_trace!
|
300
|
-
command(:foo) { |c| c.when_called {
|
299
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
301
300
|
end.run!
|
302
|
-
|
301
|
+
end.to raise_error(RuntimeError)
|
303
302
|
end
|
304
303
|
end
|
305
304
|
|
306
|
-
describe
|
307
|
-
it
|
308
|
-
expect
|
305
|
+
describe '#never_trace!' do
|
306
|
+
it 'should disable tracing globally, regardless of whether --trace was passed or not' do
|
307
|
+
expect do
|
309
308
|
new_command_runner 'help', '--trace' do
|
310
309
|
never_trace!
|
311
310
|
end.run!
|
312
|
-
|
311
|
+
end.to raise_error(SystemExit, /invalid option: --trace/)
|
313
312
|
end
|
314
313
|
|
315
|
-
it
|
314
|
+
it 'should not prompt to use --trace switch on errors' do
|
316
315
|
msg = nil
|
317
316
|
begin
|
318
317
|
new_command_runner 'foo' do
|
319
318
|
never_trace!
|
320
|
-
command(:foo) { |c| c.when_called {
|
319
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
321
320
|
end.run!
|
322
321
|
rescue SystemExit => e
|
323
322
|
msg = e.message
|
@@ -327,171 +326,171 @@ describe Commander do
|
|
327
326
|
end
|
328
327
|
end
|
329
328
|
|
330
|
-
context
|
331
|
-
it
|
332
|
-
expect
|
329
|
+
context 'conflict between #always_trace! and #never_trace!' do
|
330
|
+
it 'respects the last used command' do
|
331
|
+
expect do
|
333
332
|
new_command_runner 'foo' do
|
334
333
|
never_trace!
|
335
334
|
always_trace!
|
336
|
-
command(:foo) { |c| c.when_called {
|
335
|
+
command(:foo) { |c| c.when_called { fail 'cookies!' } }
|
337
336
|
end.run!
|
338
|
-
|
337
|
+
end.to raise_error(RuntimeError)
|
339
338
|
end
|
340
339
|
end
|
341
340
|
|
342
|
-
describe
|
343
|
-
it
|
341
|
+
describe '--version' do
|
342
|
+
it 'should output program version' do
|
344
343
|
expect(run('--version')).to eq("test 1.2.3\n")
|
345
344
|
end
|
346
345
|
end
|
347
|
-
|
348
|
-
describe
|
349
|
-
it
|
346
|
+
|
347
|
+
describe '--help' do
|
348
|
+
it 'should not output an invalid command message' do
|
350
349
|
expect(run('--help')).not_to eq("invalid command. Use --help for more information\n")
|
351
350
|
end
|
352
|
-
|
353
|
-
it
|
351
|
+
|
352
|
+
it 'can be used before or after the command and options' do
|
354
353
|
expect(run('test', '--help')).to eq("Implement help for test here\n")
|
355
354
|
end
|
356
355
|
end
|
357
|
-
|
358
|
-
describe
|
359
|
-
it
|
360
|
-
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform
|
361
|
-
expect
|
362
|
-
run('test', '--invalid-option')
|
363
|
-
|
356
|
+
|
357
|
+
describe 'with invalid options' do
|
358
|
+
it 'should output an invalid option message' do
|
359
|
+
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform.jruby?
|
360
|
+
expect do
|
361
|
+
run('test', '--invalid-option')
|
362
|
+
end.to raise_error(SystemExit, /invalid option: --invalid-option/)
|
364
363
|
end
|
365
364
|
end
|
366
|
-
|
367
|
-
describe
|
368
|
-
it
|
369
|
-
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform
|
370
|
-
expect
|
371
|
-
run('foo')
|
372
|
-
|
365
|
+
|
366
|
+
describe 'with invalid command passed' do
|
367
|
+
it 'should output an invalid command message' do
|
368
|
+
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform.jruby?
|
369
|
+
expect do
|
370
|
+
run('foo')
|
371
|
+
end.to raise_error(SystemExit, /invalid command. Use --help for more information/)
|
373
372
|
end
|
374
373
|
end
|
375
|
-
|
376
|
-
describe
|
377
|
-
it
|
378
|
-
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform
|
379
|
-
expect
|
374
|
+
|
375
|
+
describe 'with invalid command passed to help' do
|
376
|
+
it 'should output an invalid command message' do
|
377
|
+
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform.jruby?
|
378
|
+
expect do
|
380
379
|
run('help', 'does_not_exist')
|
381
|
-
|
380
|
+
end.to raise_error(SystemExit, /invalid command. Use --help for more information/)
|
382
381
|
end
|
383
382
|
end
|
384
|
-
|
385
|
-
describe
|
386
|
-
it
|
387
|
-
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform
|
388
|
-
expect
|
383
|
+
|
384
|
+
describe 'with invalid command passed to --help' do
|
385
|
+
it 'should output an invalid command message' do
|
386
|
+
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform.jruby?
|
387
|
+
expect do
|
389
388
|
run('--help', 'does_not_exist')
|
390
|
-
|
389
|
+
end.to raise_error(SystemExit, /invalid command. Use --help for more information/)
|
391
390
|
end
|
392
391
|
end
|
393
392
|
|
394
|
-
describe
|
395
|
-
it
|
396
|
-
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform
|
397
|
-
expect
|
393
|
+
describe 'with invalid option passed to --help' do
|
394
|
+
it 'should output an invalid option message' do
|
395
|
+
pending("JRuby's Kernel.abort implementation is not testable") if Commander::Platform.jruby?
|
396
|
+
expect do
|
398
397
|
run('--help', 'test', '--invalid-option')
|
399
|
-
|
398
|
+
end.to raise_error(SystemExit, /invalid option: --invalid-option/)
|
400
399
|
end
|
401
400
|
end
|
402
|
-
|
403
|
-
describe
|
404
|
-
it
|
401
|
+
|
402
|
+
describe '#valid_command_names_from' do
|
403
|
+
it 'should return array of valid command names' do
|
405
404
|
new_command_runner do
|
406
405
|
command('foo bar') {}
|
407
406
|
command('foo bar foo') {}
|
408
407
|
expect(command_runner.valid_command_names_from('foo', 'bar', 'foo').sort).to eq(['foo bar', 'foo bar foo'])
|
409
408
|
end
|
410
409
|
end
|
411
|
-
|
412
|
-
it
|
410
|
+
|
411
|
+
it 'should return empty array when no possible command names exist' do
|
413
412
|
new_command_runner do
|
414
413
|
expect(command_runner.valid_command_names_from('fake', 'command', 'name')).to eq([])
|
415
414
|
end
|
416
415
|
end
|
417
416
|
|
418
|
-
it
|
417
|
+
it 'should match exact commands only' do
|
419
418
|
new_command_runner do
|
420
419
|
command('foo') {}
|
421
420
|
expect(command_runner.valid_command_names_from('foobar')).to eq([])
|
422
421
|
end
|
423
422
|
end
|
424
423
|
end
|
425
|
-
|
426
|
-
describe
|
427
|
-
it
|
424
|
+
|
425
|
+
describe '#command_name_from_args' do
|
426
|
+
it 'should locate command within arbitrary arguments passed' do
|
428
427
|
new_command_runner '--help', '--arbitrary', 'test'
|
429
428
|
expect(command_runner.command_name_from_args).to eq('test')
|
430
429
|
end
|
431
|
-
|
432
|
-
it
|
430
|
+
|
431
|
+
it 'should support multi-word commands' do
|
433
432
|
new_command_runner '--help', '--arbitrary', 'some', 'long', 'command', 'foo'
|
434
433
|
command('some long command') {}
|
435
434
|
expect(command_runner.command_name_from_args).to eq('some long command')
|
436
435
|
end
|
437
|
-
|
438
|
-
it
|
436
|
+
|
437
|
+
it 'should match the longest possible command' do
|
439
438
|
new_command_runner '--help', '--arbitrary', 'foo', 'bar', 'foo'
|
440
439
|
command('foo bar') {}
|
441
440
|
command('foo bar foo') {}
|
442
|
-
expect(command_runner.command_name_from_args).to eq('foo bar foo'
|
441
|
+
expect(command_runner.command_name_from_args).to eq('foo bar foo')
|
443
442
|
end
|
444
|
-
|
445
|
-
it
|
443
|
+
|
444
|
+
it 'should use the left-most command name when multiple are present' do
|
446
445
|
new_command_runner 'help', 'test'
|
447
|
-
expect(command_runner.command_name_from_args).to eq('help'
|
446
|
+
expect(command_runner.command_name_from_args).to eq('help')
|
448
447
|
end
|
449
448
|
end
|
450
|
-
|
451
|
-
describe
|
452
|
-
it
|
449
|
+
|
450
|
+
describe '#active_command' do
|
451
|
+
it 'should resolve the active command' do
|
453
452
|
new_command_runner '--help', 'test'
|
454
453
|
expect(command_runner.active_command).to be_instance_of(Commander::Command)
|
455
454
|
end
|
456
|
-
|
457
|
-
it
|
455
|
+
|
456
|
+
it 'should resolve active command when invalid options are passed' do
|
458
457
|
new_command_runner '--help', 'test', '--arbitrary'
|
459
458
|
expect(command_runner.active_command).to be_instance_of(Commander::Command)
|
460
459
|
end
|
461
|
-
|
462
|
-
it
|
460
|
+
|
461
|
+
it 'should return nil when the command is not found' do
|
463
462
|
new_command_runner 'foo'
|
464
463
|
expect(command_runner.active_command).to be_nil
|
465
464
|
end
|
466
465
|
end
|
467
|
-
|
468
|
-
describe
|
469
|
-
it
|
466
|
+
|
467
|
+
describe '#default_command' do
|
468
|
+
it 'should allow you to default any command when one is not explicitly passed' do
|
470
469
|
new_command_runner '--trace' do
|
471
470
|
default_command :test
|
472
471
|
expect(command(:test)).to receive(:run).once
|
473
472
|
expect(command_runner.active_command).to eq(command(:test))
|
474
473
|
end.run!
|
475
474
|
end
|
476
|
-
|
477
|
-
it
|
475
|
+
|
476
|
+
it 'should not prevent other commands from being called' do
|
478
477
|
new_command_runner 'foo', 'bar', '--trace' do
|
479
478
|
default_command :test
|
480
|
-
command(:'foo bar'){}
|
479
|
+
command(:'foo bar') {}
|
481
480
|
expect(command(:'foo bar')).to receive(:run).once
|
482
481
|
expect(command_runner.active_command).to eq(command(:'foo bar'))
|
483
482
|
end.run!
|
484
483
|
end
|
485
|
-
|
486
|
-
it
|
484
|
+
|
485
|
+
it 'should not prevent longer commands to use the same words as the default' do
|
487
486
|
new_command_runner 'foo', 'bar', 'something'
|
488
487
|
default_command :'foo bar'
|
489
|
-
command(:'foo bar'){}
|
490
|
-
command(:'foo bar something'){}
|
488
|
+
command(:'foo bar') {}
|
489
|
+
command(:'foo bar something') {}
|
491
490
|
expect(command_runner.active_command).to eq(command(:'foo bar something'))
|
492
491
|
end
|
493
|
-
|
494
|
-
it
|
492
|
+
|
493
|
+
it 'should allow defaulting of command aliases' do
|
495
494
|
new_command_runner '--trace' do
|
496
495
|
default_command :foobar
|
497
496
|
alias_command :foobar, :test
|
@@ -499,36 +498,36 @@ describe Commander do
|
|
499
498
|
end.run!
|
500
499
|
end
|
501
500
|
end
|
502
|
-
|
503
|
-
describe
|
504
|
-
it
|
501
|
+
|
502
|
+
describe 'should function correctly' do
|
503
|
+
it 'when options are passed before the command name' do
|
505
504
|
new_command_runner '--verbose', 'test', 'foo', 'bar' do
|
506
505
|
@command.when_called do |args, options|
|
507
|
-
expect(args).to eq(
|
508
|
-
expect(options.verbose).to
|
506
|
+
expect(args).to eq(%w(foo bar))
|
507
|
+
expect(options.verbose).to be true
|
509
508
|
end
|
510
509
|
end.run!
|
511
510
|
end
|
512
511
|
|
513
|
-
it
|
512
|
+
it 'when options are passed after the command name' do
|
514
513
|
new_command_runner 'test', '--verbose', 'foo', 'bar' do
|
515
514
|
@command.when_called do |args, options|
|
516
|
-
expect(args).to eq(
|
517
|
-
expect(options.verbose).to
|
515
|
+
expect(args).to eq(%w(foo bar))
|
516
|
+
expect(options.verbose).to be true
|
518
517
|
end
|
519
518
|
end.run!
|
520
519
|
end
|
521
520
|
|
522
|
-
it
|
521
|
+
it 'when an argument passed is the same name as the command' do
|
523
522
|
new_command_runner 'test', '--verbose', 'foo', 'test', 'bar' do
|
524
523
|
@command.when_called do |args, options|
|
525
|
-
expect(args).to eq(
|
526
|
-
expect(options.verbose).to
|
524
|
+
expect(args).to eq(%w(foo test bar))
|
525
|
+
expect(options.verbose).to be true
|
527
526
|
end
|
528
527
|
end.run!
|
529
528
|
end
|
530
|
-
|
531
|
-
it
|
529
|
+
|
530
|
+
it 'when using multi-word commands' do
|
532
531
|
new_command_runner '--verbose', 'my', 'command', 'something', 'foo', 'bar' do
|
533
532
|
command('my command') { |c| c.option('--verbose') }
|
534
533
|
expect(command_runner.command_name_from_args).to eq('my command')
|
@@ -536,15 +535,15 @@ describe Commander do
|
|
536
535
|
end.run!
|
537
536
|
end
|
538
537
|
|
539
|
-
it
|
538
|
+
it 'when using multi-word commands with parts of the command name as arguments' do
|
540
539
|
new_command_runner '--verbose', 'my', 'command', 'something', 'my', 'command' do
|
541
540
|
command('my command') { |c| c.option('--verbose') }
|
542
541
|
expect(command_runner.command_name_from_args).to eq('my command')
|
543
542
|
expect(command_runner.args_without_command_name).to eq(['--verbose', 'something', 'my', 'command'])
|
544
543
|
end.run!
|
545
544
|
end
|
546
|
-
|
547
|
-
it
|
545
|
+
|
546
|
+
it 'when using multi-word commands with other commands using the same words' do
|
548
547
|
new_command_runner '--verbose', 'my', 'command', 'something', 'my', 'command' do
|
549
548
|
command('my command') {}
|
550
549
|
command('my command something') { |c| c.option('--verbose') }
|
@@ -554,8 +553,8 @@ describe Commander do
|
|
554
553
|
end
|
555
554
|
end
|
556
555
|
|
557
|
-
describe
|
558
|
-
it
|
556
|
+
describe 'options with optional arguments' do
|
557
|
+
it 'should return the argument when it is specified' do
|
559
558
|
new_command_runner 'foo', '--optional', 'arg1' do
|
560
559
|
command('foo') do |c|
|
561
560
|
c.option('--optional [argument]')
|
@@ -566,16 +565,15 @@ describe Commander do
|
|
566
565
|
end.run!
|
567
566
|
end
|
568
567
|
|
569
|
-
it
|
568
|
+
it 'should return true when no argument is specified for the option' do
|
570
569
|
new_command_runner 'foo', '--optional' do
|
571
570
|
command('foo') do |c|
|
572
571
|
c.option('--optional [argument]')
|
573
572
|
c.when_called do |_, options|
|
574
|
-
expect(options.optional).to
|
573
|
+
expect(options.optional).to be true
|
575
574
|
end
|
576
575
|
end
|
577
576
|
end.run!
|
578
577
|
end
|
579
578
|
end
|
580
|
-
|
581
579
|
end
|