commander-openflighthpc 2.0.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -138
  3. data/commander-openflighthpc.gemspec +2 -1
  4. data/lib/commander.rb +3 -4
  5. data/lib/commander/cli.rb +15 -76
  6. data/lib/commander/command.rb +81 -176
  7. data/lib/commander/error_handler.rb +62 -0
  8. data/lib/commander/help_formatters.rb +0 -4
  9. data/lib/commander/help_formatters/terminal.rb +0 -5
  10. data/lib/commander/help_formatters/terminal/command_help.erb +14 -6
  11. data/lib/commander/help_formatters/terminal/help.erb +12 -4
  12. data/lib/commander/runner.rb +92 -130
  13. data/lib/commander/version.rb +1 -1
  14. metadata +21 -46
  15. data/bin/commander +0 -104
  16. data/lib/commander/blank.rb +0 -7
  17. data/lib/commander/core_ext.rb +0 -2
  18. data/lib/commander/core_ext/array.rb +0 -24
  19. data/lib/commander/core_ext/object.rb +0 -8
  20. data/lib/commander/help_formatters/terminal/subcommand_help.erb +0 -23
  21. data/lib/commander/help_formatters/terminal_compact.rb +0 -11
  22. data/lib/commander/help_formatters/terminal_compact/command_help.erb +0 -26
  23. data/lib/commander/help_formatters/terminal_compact/help.erb +0 -29
  24. data/lib/commander/help_formatters/terminal_compact/subcommand_help.erb +0 -15
  25. data/lib/commander/import.rb +0 -5
  26. data/lib/commander/patches/decimal-integer.rb +0 -17
  27. data/lib/commander/patches/help_formatter_binding.rb +0 -15
  28. data/lib/commander/patches/implicit-short-tags.rb +0 -75
  29. data/lib/commander/patches/priority_sort.rb +0 -21
  30. data/lib/commander/patches/validate_inputs.rb +0 -79
  31. data/lib/commander/platform.rb +0 -7
  32. data/spec/command_spec.rb +0 -157
  33. data/spec/configure_spec.rb +0 -37
  34. data/spec/core_ext/array_spec.rb +0 -18
  35. data/spec/core_ext/object_spec.rb +0 -19
  36. data/spec/help_formatters/terminal_compact_spec.rb +0 -69
  37. data/spec/help_formatters/terminal_spec.rb +0 -67
  38. data/spec/methods_spec.rb +0 -61
  39. data/spec/patches/validate_inputs_spec.rb +0 -84
  40. data/spec/runner_spec.rb +0 -672
  41. data/spec/spec_helper.rb +0 -79
  42. data/spec/ui_spec.rb +0 -30
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Commander
4
- module Patches
5
- module PrioritySort
6
- attr_accessor :priority
7
-
8
- def <=>(other)
9
- # Different classes can not be compared and thus are considered
10
- # equal in priority
11
- return 0 unless self.class == other.class
12
-
13
- # Sort firstly based on the commands priority
14
- comp = (self.priority || 0) <=> (other.priority || 0)
15
-
16
- # Fall back on name comparison if priority is equal
17
- comp == 0 ? self.name <=> other.name : comp
18
- end
19
- end
20
- end
21
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Commander
4
- module Patches
5
- # An error in the usage of a command; will happen in practise and error
6
- # message should be shown along with command usage info.
7
- class CommandUsageError < StandardError; end
8
-
9
- module ValidateInputs
10
- # This is used to switch the Patch off during the original test of
11
- # Commander. It is VERY MUCH a hack but it works
12
- PatchEnabled = true
13
-
14
- def call(args = [])
15
- return super unless PatchEnabled
16
- return super if syntax_parts[0..1] == ['commander', 'help']
17
-
18
- # Use defined syntax to validate how many args this command can be
19
- # passed.
20
- assert_correct_number_of_args!(args)
21
-
22
- # Invoke original method.
23
- super(args)
24
- end
25
-
26
- private
27
-
28
- def assert_correct_number_of_args!(args)
29
- return if primary_command_word == 'help'
30
- too_many = too_many_args?(args)
31
- if too_many && sub_command_group?
32
- raise CommandUsageError, "unrecognised command. Please select from the following:"
33
- elsif too_many
34
- raise CommandUsageError, "excess arguments for command '#{primary_command_word}'"
35
- elsif too_few_args?(args)
36
- raise CommandUsageError, "insufficient arguments for command '#{primary_command_word}'"
37
- end
38
- end
39
-
40
- def syntax_parts
41
- @syntax_parts ||= syntax.split.tap do |parts|
42
- while part = parts.shift do
43
- break if part == primary_command_word || parts.length == 0
44
- end
45
- end
46
- end
47
-
48
- def primary_command_word
49
- name.split.last
50
- end
51
-
52
- def total_argument_count
53
- syntax_parts.length
54
- end
55
-
56
- def optional_argument_count
57
- syntax_parts.select do |part|
58
- part[0] == '[' && part[-1] == ']'
59
- end.length
60
- end
61
-
62
- def variable_arg?
63
- syntax_parts.any? {|part| part[-4..-1] == '...]' || part[-3..-1] == '...'}
64
- end
65
-
66
- def required_argument_count
67
- total_argument_count - optional_argument_count
68
- end
69
-
70
- def too_many_args?(args)
71
- !variable_arg? && args.length > total_argument_count
72
- end
73
-
74
- def too_few_args?(args)
75
- args.length < required_argument_count
76
- end
77
- end
78
- end
79
- end
@@ -1,7 +0,0 @@
1
- module Commander
2
- module Platform
3
- def self.jruby?
4
- defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'jruby')
5
- end
6
- end
7
- end
@@ -1,157 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Commander::Command do
4
- include Commander::Methods
5
-
6
- before :each do
7
- mock_terminal
8
- create_test_command
9
- end
10
-
11
- describe 'Options' do
12
- before :each do
13
- @options = Commander::Command::Options.new
14
- end
15
-
16
- it 'should act like an open struct' do
17
- @options.send = 'mail'
18
- @options.call = true
19
- expect(@options.send).to eq('mail')
20
- expect(@options.call).to eq(true)
21
- end
22
-
23
- it 'should allow __send__ to function as always' do
24
- @options.send = 'foo'
25
- expect(@options.__send__(:send)).to eq('foo')
26
- end
27
- end
28
-
29
- describe '#option' do
30
- it 'should add options' do
31
- expect { @command.option '--recursive' }.to change(@command.options, :length).from(1).to(2)
32
- end
33
-
34
- it 'should allow procs as option handlers' do
35
- @command.option('--recursive') { |recursive| expect(recursive).to be true }
36
- @command.run '--recursive'
37
- end
38
-
39
- it 'should allow usage of common method names' do
40
- @command.option '--open file'
41
- @command.when_called { |_, options| expect(options.open).to eq('foo') }
42
- @command.run '--open', 'foo'
43
- end
44
- end
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
- @command.run '--verbose', 'just', 'some', 'args'
51
- end
52
-
53
- it 'calling the #call method by default when an object is called' do
54
- object = double 'Object'
55
- expect(object).to receive(:call).once
56
- @command.when_called object
57
- @command.run 'foo'
58
- end
59
-
60
- it 'should allow #action as an alias to #when_called' do
61
- object = double 'Object'
62
- expect(object).to receive(:call).once
63
- @command.action object
64
- @command.run 'foo'
65
- end
66
-
67
- it 'calling an arbitrary method when an object is called' do
68
- object = double 'Object'
69
- expect(object).to receive(:foo).once
70
- @command.when_called object, :foo
71
- @command.run 'foo'
72
- end
73
-
74
- it 'should raise an error when no handler is present' do
75
- expect { @command.when_called }.to raise_error(ArgumentError)
76
- end
77
- end
78
-
79
- describe 'should populate options with' do
80
- it 'boolean values' do
81
- @command.option '--[no-]toggle'
82
- @command.when_called { |_, options| expect(options.toggle).to be true }
83
- @command.run '--toggle'
84
- @command.when_called { |_, options| expect(options.toggle).to be false }
85
- @command.run '--no-toggle'
86
- end
87
-
88
- it 'mandatory arguments' do
89
- @command.option '--file FILE'
90
- @command.when_called { |_, options| expect(options.file).to eq('foo') }
91
- @command.run '--file', 'foo'
92
- expect { @command.run '--file' }.to raise_error(OptionParser::MissingArgument)
93
- end
94
-
95
- describe 'optional arguments' do
96
- before do
97
- @command.option '--use-config [file] '
98
- end
99
-
100
- it 'should return the argument when provided' do
101
- @command.when_called { |_, options| expect(options.use_config).to eq('foo') }
102
- @command.run '--use-config', 'foo'
103
- end
104
-
105
- it 'should return true when present without an argument' do
106
- @command.when_called { |_, options| expect(options.use_config).to be true }
107
- @command.run '--use-config'
108
- end
109
-
110
- it 'should return nil when not present' do
111
- @command.when_called { |_, options| expect(options.use_config).to be_nil }
112
- @command.run
113
- end
114
- end
115
-
116
- describe 'typed arguments' do
117
- before do
118
- @command.option '--interval N', Integer
119
- end
120
-
121
- it 'should parse valid values' do
122
- @command.when_called { |_, options| expect(options.interval).to eq(5) }
123
- @command.run '--interval', '5'
124
- end
125
-
126
- it 'should reject invalid values' do
127
- expect { @command.run '--interval', 'invalid' }.to raise_error(OptionParser::InvalidArgument)
128
- end
129
- end
130
-
131
- it 'lists' do
132
- @command.option '--fav COLORS', Array
133
- @command.when_called { |_, options| expect(options.fav).to eq(%w(red green blue)) }
134
- @command.run '--fav', 'red,green,blue'
135
- end
136
-
137
- it 'lists with multi-word items' do
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'
141
- end
142
-
143
- it 'defaults' do
144
- @command.option '--files LIST', Array
145
- @command.option '--interval N', Integer
146
- @command.when_called do |_, options|
147
- options.default \
148
- files: %w(foo bar),
149
- interval: 5
150
- expect(options.files).to eq(%w(foo bar))
151
- expect(options.interval).to eq(15)
152
- end
153
- @command.run '--interval', '15'
154
- end
155
- end
156
- end
157
- end
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
- require 'commander/configure'
3
-
4
- describe Commander do
5
- describe '.configure' do
6
- it 'calls the given block' do
7
- expect { Commander.configure { throw :block_called } }.to throw_symbol(:block_called)
8
- end
9
-
10
- describe 'called block' do
11
- before(:each) do
12
- allow(Commander::Runner.instance).to receive(:run!)
13
- end
14
-
15
- it 'provides Commander configuration methods' do
16
- Commander.configure do
17
- program :name, 'test'
18
- end
19
-
20
- expect(Commander::Runner.instance.program(:name)).to eq('test')
21
- end
22
-
23
- it 'passes all arguments to the block' do
24
- Commander.configure('foo') do |first_arg|
25
- program :name, first_arg
26
- end
27
-
28
- expect(Commander::Runner.instance.program(:name)).to eq('foo')
29
- end
30
- end
31
-
32
- it 'calls Runner#run! after calling the configuration block' do
33
- expect(Commander::Runner.instance).to receive(:run!)
34
- Commander.configure {}
35
- end
36
- end
37
- end
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Array do
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))
7
- end
8
-
9
- it 'should preserve escaped whitespace' do
10
- expect(Array.parse('just a\ test')).to eq(['just', 'a test'])
11
- end
12
-
13
- it 'should match %w behavior with multiple backslashes' do
14
- str = 'just a\\ test'
15
- expect(Array.parse(str)).to eq(eval("%w(#{str})"))
16
- end
17
- end
18
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Object do
4
- describe '#get_binding' do
5
- it 'should return the objects binding' do
6
- expect(-> {}.get_binding).to be_instance_of(Binding)
7
- end
8
- end
9
-
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)
13
- end
14
-
15
- it 'should preserve its original behavior for missing variables' do
16
- expect { i_am_a_missing_variable }.to raise_error(NameError)
17
- end
18
- end
19
- end
@@ -1,69 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Commander::HelpFormatter::TerminalCompact do
4
- include Commander::Methods
5
-
6
- before :each do
7
- mock_terminal
8
- end
9
-
10
- describe 'global help' do
11
- before :each do
12
- new_command_runner 'help' do
13
- program :help_formatter, :compact
14
- command :'install gem' do |c|
15
- c.syntax = 'foo install gem [options]'
16
- c.summary = 'Install some gem'
17
- end
18
- end.run!
19
- @global_help = @output.string
20
- end
21
-
22
- describe 'should display' do
23
- it 'the command name' do
24
- expect(@global_help).to include('install gem')
25
- end
26
-
27
- it 'the summary' do
28
- expect(@global_help).to include('Install some gem')
29
- end
30
- end
31
- end
32
-
33
- describe 'command help' do
34
- before :each do
35
- new_command_runner 'help', 'install', 'gem' do
36
- program :help_formatter, :compact
37
- command :'install gem' do |c|
38
- c.syntax = 'foo install gem [options]'
39
- c.summary = 'Install some gem'
40
- c.description = 'Install some gem, blah blah blah'
41
- c.example 'one', 'two'
42
- c.example 'three', 'four'
43
- end
44
- end.run!
45
- @command_help = @output.string
46
- end
47
-
48
- describe 'should display' do
49
- it 'the command name' do
50
- expect(@command_help).to include('install gem')
51
- end
52
-
53
- it 'the description' do
54
- expect(@command_help).to include('Install some gem, blah blah blah')
55
- end
56
-
57
- it 'all examples' do
58
- expect(@command_help).to include('# one')
59
- expect(@command_help).to include('two')
60
- expect(@command_help).to include('# three')
61
- expect(@command_help).to include('four')
62
- end
63
-
64
- it 'the syntax' do
65
- expect(@command_help).to include('foo install gem [options]')
66
- end
67
- end
68
- end
69
- end
@@ -1,67 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Commander::HelpFormatter::Terminal do
4
- include Commander::Methods
5
-
6
- before :each do
7
- mock_terminal
8
- end
9
-
10
- describe 'global help' do
11
- before :each do
12
- new_command_runner 'help' do
13
- command :'install gem' do |c|
14
- c.syntax = 'foo install gem [options]'
15
- c.summary = 'Install some gem'
16
- end
17
- end.run!
18
- @global_help = @output.string
19
- end
20
-
21
- describe 'should display' do
22
- it 'the command name' do
23
- expect(@global_help).to include('install gem')
24
- end
25
-
26
- it 'the summary' do
27
- expect(@global_help).to include('Install some gem')
28
- end
29
- end
30
- end
31
-
32
- describe 'command help' do
33
- before :each do
34
- new_command_runner 'help', 'install', 'gem' do
35
- command :'install gem' do |c|
36
- c.syntax = 'foo install gem [options]'
37
- c.summary = 'Install some gem'
38
- c.description = 'Install some gem, blah blah blah'
39
- c.example 'one', 'two'
40
- c.example 'three', 'four'
41
- end
42
- end.run!
43
- @command_help = @output.string
44
- end
45
-
46
- describe 'should display' do
47
- it 'the command name' do
48
- expect(@command_help).to include('install gem')
49
- end
50
-
51
- it 'the description' do
52
- expect(@command_help).to include('Install some gem, blah blah blah')
53
- end
54
-
55
- it 'all examples' do
56
- expect(@command_help).to include('# one')
57
- expect(@command_help).to include('two')
58
- expect(@command_help).to include('# three')
59
- expect(@command_help).to include('four')
60
- end
61
-
62
- it 'the syntax' do
63
- expect(@command_help).to include('foo install gem [options]')
64
- end
65
- end
66
- end
67
- end