console_runner 0.1.14 → 0.1.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/command_line_parser.rb +20 -15
- data/lib/console_runner/version.rb +1 -1
- data/lib/method_parser.rb +4 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b185a9456da7ae30b9968451c2c4e83f2ede4072
|
4
|
+
data.tar.gz: e3cf2221df5e63c8908ff1ca84870a3ffa0a23d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77c2ade30c51ab89a515ca54151ae4d84b7e20e3f7ce85a5a92664c68ceb0e5f03ea8ad3e3cde51051f0925642725b49dd548984edd698b4164db4cb3195f77f
|
7
|
+
data.tar.gz: 133b07c51e0a3bc846725a45ed4b8ba09d40b62656f6c96b9474dcae9161dbbd3d06e7cce1a4412794c74a1af77f2f08ba0cfc72881a412c305e0bd3e4b06e3a
|
data/lib/command_line_parser.rb
CHANGED
@@ -17,12 +17,11 @@ class CommandLineParser
|
|
17
17
|
]
|
18
18
|
end.to_h
|
19
19
|
@parser = Trollop::Parser.new
|
20
|
-
@banner = generate_banner
|
21
20
|
@parser.stop_on @sub_commands
|
22
21
|
@init_method = nil
|
23
22
|
end
|
24
23
|
|
25
|
-
def
|
24
|
+
def tool_banner
|
26
25
|
result = FileParser.select_runnable_tags(@file_parser.clazz).map(&:text).join("\n")
|
27
26
|
result += "\n\n\tAvailable actions:\n"
|
28
27
|
result += @sub_commands_text.map do |c, text|
|
@@ -30,31 +29,37 @@ class CommandLineParser
|
|
30
29
|
t += "\n\t\t\t#{text}" if text != ''
|
31
30
|
t
|
32
31
|
end.join("\n")
|
33
|
-
@parser.banner(result)
|
34
32
|
result
|
35
33
|
end
|
36
34
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
35
|
+
def maybe_help(banner, action_name = nil)
|
36
|
+
action = action_name
|
37
|
+
scope = ARGV
|
38
|
+
if action_name
|
39
|
+
action_index = ARGV.index(action)
|
40
|
+
scope = ARGV[0..action_index] if action_index
|
40
41
|
end
|
41
|
-
return
|
42
|
-
|
42
|
+
return unless scope.any?{|a| %w(-h --help).include? a }
|
43
|
+
@parser.banner(banner)
|
44
|
+
Trollop::with_standard_exception_handling(@parser) { raise Trollop::HelpNeeded }
|
45
|
+
end
|
46
|
+
|
47
|
+
def raise_on_action_absence(sub_commands)
|
48
|
+
return if ARGV.any? {|a| sub_commands.include? a }
|
49
|
+
raise ConsoleRunnerError, "You must provide one of available actions: #{sub_commands.join ', '}"
|
43
50
|
end
|
44
51
|
|
45
52
|
def run(action)
|
46
|
-
|
53
|
+
maybe_help(tool_banner, action ? action.name.to_s : nil )
|
54
|
+
raise ConsoleRunnerError, 'Cannot find any @runnable action' unless action
|
55
|
+
raise_on_action_absence @sub_commands
|
47
56
|
@init_method ||= MethodParser.new(@file_parser.initialize_method) if @file_parser.initialize_method
|
48
57
|
@method = MethodParser.new action
|
49
58
|
[@init_method, @method].each do |method|
|
50
59
|
next unless method
|
51
60
|
method.trollop_opts.each { |a| @parser.opt(*a) }
|
52
|
-
|
53
|
-
|
54
|
-
raise Trollop::HelpNeeded if ARGV.empty?
|
55
|
-
end
|
56
|
-
@parser.parse ARGV
|
57
|
-
end
|
61
|
+
maybe_help(method.text, action.name.to_s)
|
62
|
+
cmd_opts = @parser.parse ARGV
|
58
63
|
given_attrs = cmd_opts.keys.select { |k| k.to_s.include? '_given' }.map { |k| k.to_s.gsub('_given', '').to_sym }
|
59
64
|
method.cmd_opts = cmd_opts.select { |k, _| given_attrs.include? k }
|
60
65
|
method.default_values.each do |k, v|
|
data/lib/method_parser.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
class MethodParser
|
3
3
|
attr_reader :method,
|
4
4
|
:name,
|
5
|
+
:text,
|
5
6
|
:parameters,
|
6
7
|
:param_tags, # All method parameters tags
|
7
8
|
:option_tags, # Only options tags
|
@@ -28,6 +29,7 @@ class MethodParser
|
|
28
29
|
def initialize(method)
|
29
30
|
@method = method
|
30
31
|
@name = @method.name
|
32
|
+
@text = FileParser.select_runnable_tags(@method).map(&:text).join("\n")
|
31
33
|
@parameters = @method.parameters
|
32
34
|
@default_values = default_params
|
33
35
|
@param_tags = FileParser.select_param_tags @method
|
@@ -83,8 +85,8 @@ Use different names to `console_runner` be able to run #{@name} method."
|
|
83
85
|
end
|
84
86
|
|
85
87
|
def params_array
|
86
|
-
options_groups
|
87
|
-
get_params
|
88
|
+
options_groups = {}
|
89
|
+
get_params = {}
|
88
90
|
@parameters.map(&:first).map.with_index { |p, i| [i, p] }.to_h.each do |index, name|
|
89
91
|
if options_group?(name)
|
90
92
|
options_groups[index] = name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: console_runner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Karpovich
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|