dracula 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dracula.rb +5 -2
- data/lib/dracula/command.rb +20 -31
- data/lib/dracula/command_help.rb +56 -0
- data/lib/dracula/namespace.rb +16 -17
- data/lib/dracula/namespace_help.rb +63 -0
- data/lib/dracula/ui.rb +20 -0
- data/lib/dracula/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c5993ea9d0d226f890560dade6d177de9d092b9
|
4
|
+
data.tar.gz: 556662ade80ed31ddbc494897844935c3bdde05f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32cffd934ccb445cc3e2c9580b6e920ad4471c6ee59fc23bbf87b1fdb1683427d64af4683f5f5ee451e4b3ab193755ba556b889c0f68917b77864c0ed97dceb0
|
7
|
+
data.tar.gz: 4c139824b3e88d2ec83885f045ac7b87051924c2942ffd00f6719040f4c6b9945bd70732c8b16d19ee08ced69e06284cbd6284ddfa33f42bf317426f9cfda483
|
data/lib/dracula.rb
CHANGED
@@ -2,10 +2,12 @@ require "dracula/version"
|
|
2
2
|
require "optparse"
|
3
3
|
|
4
4
|
class Dracula
|
5
|
-
require "dracula/
|
5
|
+
require "dracula/ui"
|
6
6
|
require "dracula/flag"
|
7
|
+
require "dracula/command"
|
8
|
+
require "dracula/command_help"
|
7
9
|
require "dracula/namespace"
|
8
|
-
require "dracula/
|
10
|
+
require "dracula/namespace_help"
|
9
11
|
|
10
12
|
class << self
|
11
13
|
def program_name(name = nil)
|
@@ -59,6 +61,7 @@ class Dracula
|
|
59
61
|
def register(name, description, klass)
|
60
62
|
klass.namespace.name = name
|
61
63
|
klass.namespace.description = description
|
64
|
+
klass.namespace.parent = namespace
|
62
65
|
|
63
66
|
namespace.add_subcommand(klass.namespace)
|
64
67
|
end
|
data/lib/dracula/command.rb
CHANGED
@@ -18,6 +18,14 @@ class Dracula
|
|
18
18
|
@options = options || []
|
19
19
|
end
|
20
20
|
|
21
|
+
def prefix
|
22
|
+
@klass.namespace.prefix
|
23
|
+
end
|
24
|
+
|
25
|
+
def full_name
|
26
|
+
"#{prefix}#{name}"
|
27
|
+
end
|
28
|
+
|
21
29
|
def name
|
22
30
|
desc.name
|
23
31
|
end
|
@@ -26,41 +34,22 @@ class Dracula
|
|
26
34
|
@klass.instance_method(@method_name).parameters.select { |p| p[0] == :req }.map { |p| p[1].to_s.upcase }
|
27
35
|
end
|
28
36
|
|
29
|
-
def banner
|
30
|
-
namespace = @klass.namespace.name ? "#{@klass.namespace.name}:" : ""
|
31
|
-
args = arguments.count > 0 ? " #{arguments.join(" ")}" : ""
|
32
|
-
|
33
|
-
"#{namespace}#{desc.name}#{args}"
|
34
|
-
end
|
35
|
-
|
36
37
|
def help
|
37
|
-
|
38
|
-
"Usage: #{Dracula.program_name} #{banner}",
|
39
|
-
"",
|
40
|
-
"#{desc.description}",
|
41
|
-
""
|
42
|
-
]
|
43
|
-
|
44
|
-
if options.size > 0
|
45
|
-
msg << "Flags:"
|
46
|
-
|
47
|
-
options.each { |option| msg << " #{option.banner}" }
|
48
|
-
|
49
|
-
msg << ""
|
50
|
-
end
|
51
|
-
|
52
|
-
unless long_desc.nil?
|
53
|
-
msg << long_desc
|
54
|
-
end
|
55
|
-
|
56
|
-
puts msg.join("\n")
|
38
|
+
CommandHelp.new(self).show
|
57
39
|
end
|
58
40
|
|
59
41
|
def run(params)
|
60
42
|
args = params.take_while { |p| p[0] != "-" }
|
61
43
|
|
62
|
-
if args.count
|
63
|
-
puts "Missing arguments"
|
44
|
+
if args.count < arguments.count
|
45
|
+
puts Dracula::UI.error("Missing arguments")
|
46
|
+
puts ""
|
47
|
+
help
|
48
|
+
exit(1)
|
49
|
+
end
|
50
|
+
|
51
|
+
if args.count > arguments.count
|
52
|
+
puts Dracula::UI.error("Too many arguments")
|
64
53
|
puts ""
|
65
54
|
help
|
66
55
|
exit(1)
|
@@ -71,7 +60,7 @@ class Dracula
|
|
71
60
|
missing_flags = missing_required_flags(parsed_flags)
|
72
61
|
|
73
62
|
unless missing_flags.empty?
|
74
|
-
puts "
|
63
|
+
puts Dracula::UI.error("Missing required Parameter: #{missing_flags.first.banner}")
|
75
64
|
puts ""
|
76
65
|
help
|
77
66
|
exit(1)
|
@@ -81,7 +70,7 @@ class Dracula
|
|
81
70
|
rescue OptionParser::MissingArgument => ex
|
82
71
|
flag = flags.find { |f| "--#{f.name}" == ex.args.first }
|
83
72
|
|
84
|
-
puts "
|
73
|
+
puts Dracula::UI.error("Missing flag parameter: #{flag.banner}")
|
85
74
|
puts ""
|
86
75
|
help
|
87
76
|
exit(1)
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class Dracula
|
2
|
+
class CommandHelp
|
3
|
+
|
4
|
+
def initialize(command)
|
5
|
+
@command = command
|
6
|
+
end
|
7
|
+
|
8
|
+
def show
|
9
|
+
show_usage
|
10
|
+
show_desc
|
11
|
+
show_flags if @command.flags.any?
|
12
|
+
show_long_desc if @command.long_desc
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def banner
|
18
|
+
args = if @command.arguments.count > 0
|
19
|
+
" #{@command.arguments.map { |a| Dracula::UI.bold("[#{a}]") }.join(" ")}"
|
20
|
+
else
|
21
|
+
""
|
22
|
+
end
|
23
|
+
|
24
|
+
flags_banner = if @command.flags.count > 0
|
25
|
+
" " + Dracula::UI.bold("[FLAGS]")
|
26
|
+
else
|
27
|
+
""
|
28
|
+
end
|
29
|
+
|
30
|
+
"#{@command.full_name}#{args}#{flags_banner}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def show_usage
|
34
|
+
puts "Usage: #{Dracula.program_name} #{banner}"
|
35
|
+
puts ""
|
36
|
+
end
|
37
|
+
|
38
|
+
def show_desc
|
39
|
+
puts @command.desc.description.capitalize
|
40
|
+
puts ""
|
41
|
+
end
|
42
|
+
|
43
|
+
def show_flags
|
44
|
+
puts Dracula::UI.bold("Flags:")
|
45
|
+
|
46
|
+
@command.flags.each { |flag| puts " #{flag.banner}" }
|
47
|
+
|
48
|
+
puts ""
|
49
|
+
end
|
50
|
+
|
51
|
+
def show_long_desc
|
52
|
+
puts @command.long_desc
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
data/lib/dracula/namespace.rb
CHANGED
@@ -5,11 +5,13 @@ class Dracula
|
|
5
5
|
attr_accessor :description
|
6
6
|
attr_accessor :commands
|
7
7
|
attr_accessor :subcommands
|
8
|
+
attr_accessor :parent
|
8
9
|
|
9
10
|
def initialize(klass)
|
10
11
|
@klass = klass
|
11
12
|
@commands = []
|
12
13
|
@subcommands = []
|
14
|
+
@parent = []
|
13
15
|
end
|
14
16
|
|
15
17
|
def dispatch(route, params, action = :run)
|
@@ -22,7 +24,10 @@ class Dracula
|
|
22
24
|
if handler
|
23
25
|
action == :run ? handler.run(params) : handler.help
|
24
26
|
else
|
27
|
+
puts Dracula::UI.error("Command not found")
|
28
|
+
puts ""
|
25
29
|
help
|
30
|
+
exit(1)
|
26
31
|
end
|
27
32
|
else
|
28
33
|
handler = subcommands.find { |c| c.name == route[0] }
|
@@ -30,7 +35,10 @@ class Dracula
|
|
30
35
|
if handler
|
31
36
|
handler.dispatch(route[1..-1], params, action)
|
32
37
|
else
|
38
|
+
puts Dracula::UI.error("Command not found #{prefix}#{Dracula::UI.danger(route.join(":"))}")
|
39
|
+
puts ""
|
33
40
|
help
|
41
|
+
exit(1)
|
34
42
|
end
|
35
43
|
end
|
36
44
|
end
|
@@ -39,25 +47,16 @@ class Dracula
|
|
39
47
|
help
|
40
48
|
end
|
41
49
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
puts "Usage: #{Dracula.program_name} #{prefix}<command>"
|
46
|
-
puts ""
|
47
|
-
puts (description || "Help topics, type #{Dracula.program_name} help TOPIC for more details:")
|
48
|
-
puts ""
|
49
|
-
|
50
|
-
command_list = []
|
51
|
-
|
52
|
-
commands.each do |cmd|
|
53
|
-
command_list << ["#{prefix}#{cmd.desc.name}", cmd.desc.description]
|
54
|
-
end
|
50
|
+
def prefix
|
51
|
+
name ? "#{parent.prefix}#{name}:" : ""
|
52
|
+
end
|
55
53
|
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
def top_level?
|
55
|
+
prefix == ""
|
56
|
+
end
|
59
57
|
|
60
|
-
|
58
|
+
def help
|
59
|
+
NamespaceHelp.new(self).show
|
61
60
|
end
|
62
61
|
|
63
62
|
def add_command(command)
|
@@ -0,0 +1,63 @@
|
|
1
|
+
class Dracula
|
2
|
+
class NamespaceHelp
|
3
|
+
|
4
|
+
attr_reader :namespace
|
5
|
+
|
6
|
+
def initialize(namespace)
|
7
|
+
@namespace = namespace
|
8
|
+
end
|
9
|
+
|
10
|
+
def show
|
11
|
+
show_usage
|
12
|
+
show_desc if namespace.description
|
13
|
+
show_commands
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def show_usage
|
19
|
+
puts "Usage: #{Dracula.program_name} #{Dracula::UI.bold "#{namespace.prefix}[command]"}\n"
|
20
|
+
puts ""
|
21
|
+
end
|
22
|
+
|
23
|
+
def show_desc
|
24
|
+
puts namespace.description.capitalize
|
25
|
+
puts ""
|
26
|
+
end
|
27
|
+
|
28
|
+
def show_commands
|
29
|
+
puts "Command list, type #{Dracula::UI.bold(Dracula.program_name.to_s + " help #{namespace.prefix}[command]")} for more details:"
|
30
|
+
puts ""
|
31
|
+
|
32
|
+
banners = []
|
33
|
+
|
34
|
+
banners += command_banners(namespace)
|
35
|
+
banners << ["", ""] # empty line
|
36
|
+
|
37
|
+
if namespace.top_level?
|
38
|
+
banners += short_subcommand_banners(namespace)
|
39
|
+
banners << ["", ""] # empty line
|
40
|
+
else
|
41
|
+
namespace.subcommands.each do |sub_cmd|
|
42
|
+
banners += command_banners(sub_cmd)
|
43
|
+
banners << ["", ""] # empty line
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
Dracula::UI.print_table(banners, :indent => 2)
|
48
|
+
end
|
49
|
+
|
50
|
+
def short_subcommand_banners(namespace)
|
51
|
+
namespace.subcommands.map do |sub_cmd|
|
52
|
+
[Dracula::UI.bold("#{namespace.prefix}#{sub_cmd.name}"), sub_cmd.description.capitalize]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def command_banners(namespace)
|
57
|
+
namespace.commands.map do |cmd|
|
58
|
+
[Dracula::UI.bold("#{namespace.prefix}#{cmd.desc.name}"), cmd.desc.description.capitalize]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
data/lib/dracula/ui.rb
CHANGED
@@ -1,6 +1,26 @@
|
|
1
1
|
class Dracula
|
2
2
|
class UI
|
3
3
|
|
4
|
+
def self.bold(str)
|
5
|
+
if $stdout.tty? # colorize only if output is terminal
|
6
|
+
"\e[34m#{str}\e[0m"
|
7
|
+
else
|
8
|
+
str
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.danger(str)
|
13
|
+
if $stdout.tty? # colorize only if output is terminal
|
14
|
+
"\e[31m#{str}\e[0m"
|
15
|
+
else
|
16
|
+
str
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.error(str)
|
21
|
+
danger("[ERROR] ") + str.to_s
|
22
|
+
end
|
23
|
+
|
4
24
|
# Prints a table. Shamelesly copied from thor.
|
5
25
|
#
|
6
26
|
# ==== Parameters
|
data/lib/dracula/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dracula
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Šarčević
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,8 +71,10 @@ files:
|
|
71
71
|
- dracula.gemspec
|
72
72
|
- lib/dracula.rb
|
73
73
|
- lib/dracula/command.rb
|
74
|
+
- lib/dracula/command_help.rb
|
74
75
|
- lib/dracula/flag.rb
|
75
76
|
- lib/dracula/namespace.rb
|
77
|
+
- lib/dracula/namespace_help.rb
|
76
78
|
- lib/dracula/ui.rb
|
77
79
|
- lib/dracula/version.rb
|
78
80
|
homepage: https://github.com/renderedtext/dracula
|