subcommand-optparse 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/sample_command.rb +9 -0
- data/lib/subcommand_optparse.rb +53 -6
- data/lib/subcommand_optparse/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5254886d098c538ae00e28d7795c05c0b1e6f2c
|
4
|
+
data.tar.gz: 9cd2000a1319ee66fef314571ece3230405e8456
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ff59e3a2b8fef696a11efd555b8b1faa88be6a54f0891621b3658d84edb1aba627719f8dbba649e1fb4325d6a6b5d4ea5ba6273093ca9f5e8feae5a5f57ac37
|
7
|
+
data.tar.gz: 61c811561c75a88e256fbca0c26bd5ea40668038dae9fa8975244bc1a828c8a144e8bee476c72fe50f5c1cd8da20b8494619b8a4346ce9600d052e2bdb8e9164
|
data/examples/sample_command.rb
CHANGED
@@ -7,6 +7,7 @@ parser = SubCmdOptParser.new(:help_command => true) do |sc|
|
|
7
7
|
sc.program_name = "sample_command.rb"
|
8
8
|
sc.version = "0.0.1"
|
9
9
|
sc.release = "hello world"
|
10
|
+
sc.summary = "Sample program"
|
10
11
|
|
11
12
|
sc.global_option do |opt|
|
12
13
|
opt.on("--global-option", "Banner of global option") do |v|
|
@@ -19,6 +20,14 @@ parser = SubCmdOptParser.new(:help_command => true) do |sc|
|
|
19
20
|
end
|
20
21
|
end
|
21
22
|
sc.subcommand("bar") do |opt|
|
23
|
+
opt.usage = <<USAGE
|
24
|
+
sample_command.rb bar ARGS
|
25
|
+
[--bar-option] args...
|
26
|
+
USAGE
|
27
|
+
opt.example = <<EXAMPLE
|
28
|
+
ruby sample_command.rb bar 100 200
|
29
|
+
ruby sample_command.rb bar --bar-option
|
30
|
+
EXAMPLE
|
22
31
|
opt.on("--bar-option", "Description of bar") do |v|
|
23
32
|
options[:bar] = true
|
24
33
|
end
|
data/lib/subcommand_optparse.rb
CHANGED
@@ -6,25 +6,66 @@ class SubCmdOptParser
|
|
6
6
|
class OptionParserForSubCmd < OptionParser
|
7
7
|
attr_accessor :subcommand_name
|
8
8
|
attr_accessor :description
|
9
|
+
attr_accessor :summary
|
10
|
+
attr_writer :usage
|
11
|
+
attr_writer :example
|
9
12
|
|
10
13
|
def initialize(subcmd, description, width, indent)
|
11
14
|
@subcommand_name = subcmd
|
12
15
|
@description = description
|
16
|
+
@summary = nil
|
13
17
|
super(nil, width, indent)
|
14
18
|
end
|
15
19
|
|
20
|
+
def usage
|
21
|
+
usage_str = "Usage: "
|
22
|
+
if @usage
|
23
|
+
@usage.each_line.with_index do |line, ind|
|
24
|
+
usage_str << " " if ind > 0
|
25
|
+
usage_str << line.sub(/\n?\z/, "\n")
|
26
|
+
end
|
27
|
+
else
|
28
|
+
usage_str << "#{program_name} #{@subcommand_name || '<command>'} [options]\n"
|
29
|
+
end
|
30
|
+
usage_str
|
31
|
+
end
|
32
|
+
|
16
33
|
def banner
|
17
34
|
unless @banner
|
18
|
-
@banner =
|
19
|
-
if @
|
20
|
-
@banner << "\n
|
35
|
+
@banner = usage
|
36
|
+
if @summary
|
37
|
+
@banner << "\n#{@summary.sub(/\n?\z/, "\n")}"
|
38
|
+
elsif @description
|
39
|
+
@banner << "\n#{@description.sub(/\n?\z/, "\n")}"
|
40
|
+
end
|
41
|
+
if @example
|
42
|
+
if @example.each_line.count == 1
|
43
|
+
@banner << "\nExample: #{@example.strip}"
|
44
|
+
else
|
45
|
+
@banner << "\nExamples:\n"
|
46
|
+
@example.each_line do |line|
|
47
|
+
@banner << " #{line.sub(/\n?\z/, "\n")}"
|
48
|
+
end
|
49
|
+
end
|
21
50
|
end
|
22
|
-
visit(:add_banner, @banner)
|
23
51
|
end
|
24
52
|
@banner
|
25
53
|
end
|
54
|
+
|
55
|
+
def help
|
56
|
+
str_banner = "#{banner}".sub(/\n?\z/, "\n")
|
57
|
+
str_summary = summarize("")
|
58
|
+
if str_summary.size > 0
|
59
|
+
str_banner << "\nOptions:\n"
|
60
|
+
end
|
61
|
+
str_banner + str_summary
|
62
|
+
end
|
63
|
+
|
64
|
+
alias_method :to_s, :help
|
26
65
|
end
|
27
66
|
|
67
|
+
# summary is shows in help message
|
68
|
+
attr_accessor :summary
|
28
69
|
attr_accessor :program_name
|
29
70
|
attr_accessor :summary_width
|
30
71
|
attr_accessor :summary_indent
|
@@ -55,6 +96,7 @@ class SubCmdOptParser
|
|
55
96
|
@global_option_setting = nil
|
56
97
|
@subcommand = []
|
57
98
|
@help_subcommand_use_p = (!opts.has_key?(:help_command) || opts[:help_command])
|
99
|
+
@summary = nil
|
58
100
|
@version_subcommand_use_p = (!opts.has_key?(:version_command) || opts[:version_command])
|
59
101
|
@accept_undefined_command = opts[:accept_undefined_command]
|
60
102
|
@parse_only = opts[:parse_only]
|
@@ -125,7 +167,7 @@ class SubCmdOptParser
|
|
125
167
|
private :get_option_parser
|
126
168
|
|
127
169
|
def message_list_subcommands
|
128
|
-
mes = "Commands
|
170
|
+
mes = "Commands:\n"
|
129
171
|
max_size_subcmd = (@subcommand.map { |name, val| name.size }).max
|
130
172
|
str_size = (max_size_subcmd.even? ? max_size_subcmd : max_size_subcmd + 1) + 4
|
131
173
|
@subcommand.each do |name, val|
|
@@ -137,7 +179,12 @@ class SubCmdOptParser
|
|
137
179
|
private :message_list_subcommands
|
138
180
|
|
139
181
|
def get_banner_help(opt)
|
140
|
-
"Usage: #{opt.program_name} <command> [options]\n\n"
|
182
|
+
mes = "Usage: #{opt.program_name} <command> [options]\n\n"
|
183
|
+
if @summary
|
184
|
+
mes << @summary << "\n\n"
|
185
|
+
end
|
186
|
+
mes << message_list_subcommands
|
187
|
+
mes
|
141
188
|
end
|
142
189
|
private :get_banner_help
|
143
190
|
|