subcommand 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,9 @@
1
+ = subcommand 1.0.4 2010-06-24 15:51
2
+ * text for subcommands was being processed always before execution, so the
3
+ point of lazy loading was lost. I've moved it away so it's only processed if
4
+ actually printing help.
5
+
6
+ Howeve, this means that if you are relying on OptionParser's implicit --help,
7
+ that won't print subcommands. So i've added a method add_help_option which
8
+ adds a correct help option, to print subcommands. This way, command help and
9
+ command --help print identical output.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.3
1
+ 1.0.5
data/lib/subcommand.rb CHANGED
@@ -90,6 +90,48 @@ module Subcommands
90
90
  yield @global
91
91
  end
92
92
  end
93
+ def print_actions
94
+ cmdtext = "Commands are:"
95
+ @commands.each_pair do |c, opt|
96
+ #puts "inside opt.call loop"
97
+ desc = opt.call.description
98
+ cmdtext << "\n #{c} : #{desc}"
99
+ end
100
+
101
+ # print aliases
102
+ unless @aliases.empty?
103
+ cmdtext << "\n\nAliases: \n"
104
+ @aliases.each_pair { |name, val| cmdtext << " #{name} - #{val}\n" }
105
+ end
106
+
107
+ cmdtext << "\n\nSee '#{$0} help COMMAND' for more information on a specific command."
108
+ end
109
+ ## add text of subcommands in help and --help option
110
+ def add_subcommand_help
111
+ # user has defined some, but lets add subcommand information
112
+
113
+ cmdtext = print_actions
114
+
115
+ global_options do |opts|
116
+ # lets add the description user gave into banner
117
+ opts.banner << "\n#{opts.description}\n" if opts.description
118
+ opts.separator ""
119
+ opts.separator cmdtext
120
+ end
121
+ end
122
+ # this is so that on pressing --help he gets same subcommand help as when doing help.
123
+ # This is to be added in your main program, after defining global options
124
+ # if you want detailed help on --help. This is since optionparser's default
125
+ # --help will not print your actions/commands
126
+ def add_help_option
127
+ global_options do |opts|
128
+ opts.on("-h", "--help", "Print this help") do |v|
129
+ add_subcommand_help
130
+ puts @global
131
+ exit
132
+ end
133
+ end
134
+ end
93
135
  # first parse global optinos
94
136
  # then parse subcommand options if valid subcommand
95
137
  # special case of "help command" so we print help of command - git style (3)
@@ -104,34 +146,14 @@ module Subcommands
104
146
  opts.separator ""
105
147
  opts.separator "Global options are:"
106
148
  opts.on("-h", "--help", "Print this help") do |v|
149
+ add_subcommand_help
107
150
  puts @global
108
151
  exit
109
152
  end
110
153
  opts.separator ""
111
- opts.separator subtext
154
+ #opts.separator subtext # FIXME: no such variable supposed to have subcommand help
112
155
  end
113
156
  else
114
- # user has defined some, but lets add subcommand information
115
- cmdtext = "Commands are:"
116
- @commands.each_pair do |c, opt|
117
- desc = opt.call.description
118
- cmdtext << "\n #{c} : #{desc}"
119
- end
120
-
121
- # print aliases
122
- unless @aliases.empty?
123
- cmdtext << "\n\nAliases: \n"
124
- @aliases.each_pair { |name, val| cmdtext << " #{name} - #{val}\n" }
125
- end
126
-
127
- cmdtext << "\n\nSee '#{$0} help COMMAND' for more information on a specific command."
128
-
129
- global_options do |opts|
130
- # lets add the description user gave into banner
131
- opts.banner << "\n#{opts.description}\n" if opts.description
132
- opts.separator ""
133
- opts.separator cmdtext
134
- end
135
157
  end
136
158
  @global.order!
137
159
  cmd = ARGV.shift
@@ -163,11 +185,13 @@ module Subcommands
163
185
  else
164
186
  # no help for this command XXX check for alias
165
187
  puts "Invalid command: #{cmd}."
188
+ add_subcommand_help
166
189
  puts @global
167
190
  end
168
191
  else
169
192
  # invalid command
170
193
  puts "Invalid command: #{cmd}" unless cmd == "help"
194
+ add_subcommand_help
171
195
  puts @global
172
196
  end
173
197
  exit 0
@@ -211,6 +235,7 @@ if __FILE__ == $PROGRAM_NAME
211
235
  options[:verbose] = v
212
236
  end
213
237
  end
238
+ add_help_option
214
239
  # define a command
215
240
  command :foo, :goo do |opts|
216
241
  opts.banner = "Usage: foo [options]"
data/subcommand.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{subcommand}
8
- s.version = "1.0.4"
8
+ s.version = "1.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rahul Kumar"]
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.files = [
21
21
  ".document",
22
22
  ".gitignore",
23
+ "CHANGELOG.rdoc",
23
24
  "LICENSE",
24
25
  "Makefile",
25
26
  "README.rdoc",
@@ -40,7 +41,9 @@ Gem::Specification.new do |s|
40
41
  "tests/t0006-goo_opt.sh",
41
42
  "tests/t0007-bar_baz.sh",
42
43
  "tests/t0008-boo_zoo.sh",
43
- "tests/test-lib.sh"
44
+ "tests/test-lib.sh",
45
+ "tests/test.rb",
46
+ "tests/test1.rb"
44
47
  ]
45
48
  s.homepage = %q{http://github.com/rkumar/subcommand}
46
49
  s.rdoc_options = ["--charset=UTF-8"]
data/tests/recreate.sh CHANGED
@@ -24,10 +24,12 @@ echo "Using suffix:$str"
24
24
  read -p "press enter "
25
25
  grep '^>>> ' "$oldfile" | sed 's/^>>> //'
26
26
  read -p "press enter "
27
+ cp serial_numbers serial_numbers.xxx
27
28
  grep '^>>> ' "$oldfile" | sed 's/^>>> //' | ./rtest2.sh $LOADSTR "$str"
28
29
 
29
30
  echo
30
31
  echo renaming old file with O prefix
31
32
  mv "$oldfile" O$oldfile
33
+ cp serial_numbers.xxx serial_numbers
32
34
  echo "If you don't find a test case, then rename transcript.txt to $oldfile"
33
35
  echo "cp transcript.txt $oldfile"
data/tests/t0001-main.sh CHANGED
@@ -12,6 +12,7 @@ Stupid program that does something
12
12
 
13
13
  Global options are:
14
14
  -v, --[no-]verbose Run verbosely
15
+ -h, --help Print this help
15
16
 
16
17
  Commands are:
17
18
  foo : desc for foo
@@ -26,12 +27,15 @@ Aliases:
26
27
 
27
28
  See 'bin/subcommand.rb help COMMAND' for more information on a specific command.
28
29
  >>> end
30
+ >>> end
31
+ >>> end
29
32
  >>> ruby bin/subcommand.rb --help
30
33
  Usage: subcommand.rb [options] [subcommand [options]]
31
34
  Stupid program that does something
32
35
 
33
36
  Global options are:
34
37
  -v, --[no-]verbose Run verbosely
38
+ -h, --help Print this help
35
39
 
36
40
  Commands are:
37
41
  foo : desc for foo
@@ -46,6 +50,8 @@ Aliases:
46
50
 
47
51
  See 'bin/subcommand.rb help COMMAND' for more information on a specific command.
48
52
  >>> end
53
+ >>> end
54
+ >>> end
49
55
 
50
56
  EOF
51
57
  test_done
@@ -13,6 +13,7 @@ Stupid program that does something
13
13
 
14
14
  Global options are:
15
15
  -v, --[no-]verbose Run verbosely
16
+ -h, --help Print this help
16
17
 
17
18
  Commands are:
18
19
  foo : desc for foo
@@ -27,6 +28,8 @@ Aliases:
27
28
 
28
29
  See 'bin/subcommand.rb help COMMAND' for more information on a specific command.
29
30
  >>> end
31
+ >>> end
32
+ >>> end
30
33
  >>> ruby bin/subcommand.rb fxxx --help
31
34
  Invalid command: fxxx
32
35
  Usage: subcommand.rb [options] [subcommand [options]]
@@ -34,6 +37,7 @@ Stupid program that does something
34
37
 
35
38
  Global options are:
36
39
  -v, --[no-]verbose Run verbosely
40
+ -h, --help Print this help
37
41
 
38
42
  Commands are:
39
43
  foo : desc for foo
@@ -48,6 +52,8 @@ Aliases:
48
52
 
49
53
  See 'bin/subcommand.rb help COMMAND' for more information on a specific command.
50
54
  >>> end
55
+ >>> end
56
+ >>> end
51
57
 
52
58
  EOF
53
59
  test_done
data/tests/test.rb ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby -w
2
+ require 'subcommand'
3
+
4
+ if __FILE__ == $PROGRAM_NAME
5
+ include Subcommands
6
+ options = {}
7
+ appname = File.basename($0)
8
+ # global is optional
9
+ #global_options do |opts|
10
+ #opts.banner = "Usage: #{appname} [options] [subcommand [options]]"
11
+ #opts.description = "Stupid program that does something"
12
+ #opts.separator ""
13
+ #opts.separator "Global options are:"
14
+ #opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
15
+ #options[:verbose] = v
16
+ #end
17
+ #end
18
+ # define a command
19
+ command :foo, :goo do |opts|
20
+ opts.banner = "Usage: foo [options]"
21
+ opts.description = "desc for foo"
22
+ opts.on("-f", "--[no-]force", "force verbosely") do |v|
23
+ options[:force] = v
24
+ end
25
+ end
26
+ command :baz do |opts|
27
+ opts.banner = "Usage: baz [options]"
28
+ opts.description = "desc for baz"
29
+ opts.on("-q", "--[no-]quiet", "quietly run ") do |v|
30
+ options[:quiet] = v
31
+ end
32
+ end
33
+ alias_command :bar, 'baz'
34
+ alias_command :boo, 'foo', '--force'
35
+ alias_command :zoo, 'foo', 'ruby'
36
+
37
+ # do the parsing.
38
+ cmd = opt_parse()
39
+
40
+ puts "cmd: #{cmd}"
41
+ puts "options ......"
42
+ p options
43
+ puts "ARGV:"
44
+ p ARGV
45
+ end
data/tests/test1.rb ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby -w
2
+ ######################################
3
+ require 'subcommand'
4
+
5
+ if __FILE__ == $PROGRAM_NAME
6
+ include Subcommands
7
+ options = {}
8
+ appname = File.basename($0)
9
+ # global is optional
10
+ global_options do |opts|
11
+ opts.banner = "Usage: #{appname} [options] [subcommand [options]]"
12
+ opts.description = "Stupid program that does something"
13
+ opts.separator ""
14
+ opts.separator "Global options are:"
15
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
16
+ options[:verbose] = v
17
+ end
18
+ end
19
+ add_help_option
20
+ # define a command
21
+ command :foo, :goo do |opts|
22
+ opts.banner = "Usage: foo [options]"
23
+ opts.description = "desc for foo"
24
+ opts.on("-f", "--[no-]force", "force verbosely") do |v|
25
+ options[:force] = v
26
+ end
27
+ end
28
+ command :baz do |opts|
29
+ opts.banner = "Usage: baz [options]"
30
+ opts.description = "desc for baz"
31
+ opts.on("-q", "--[no-]quiet", "quietly run ") do |v|
32
+ options[:quiet] = v
33
+ end
34
+ end
35
+ alias_command :bar, 'baz'
36
+ alias_command :boo, 'foo', '--force'
37
+ alias_command :zoo, 'foo', 'ruby'
38
+
39
+ # do the parsing.
40
+ cmd = opt_parse()
41
+
42
+ puts "cmd: #{cmd}"
43
+ puts "options ......"
44
+ p options
45
+ puts "ARGV:"
46
+ p ARGV
47
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 4
9
- version: 1.0.4
8
+ - 5
9
+ version: 1.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rahul Kumar
@@ -42,6 +42,7 @@ extra_rdoc_files:
42
42
  files:
43
43
  - .document
44
44
  - .gitignore
45
+ - CHANGELOG.rdoc
45
46
  - LICENSE
46
47
  - Makefile
47
48
  - README.rdoc
@@ -63,6 +64,8 @@ files:
63
64
  - tests/t0007-bar_baz.sh
64
65
  - tests/t0008-boo_zoo.sh
65
66
  - tests/test-lib.sh
67
+ - tests/test.rb
68
+ - tests/test1.rb
66
69
  - TODO
67
70
  has_rdoc: true
68
71
  homepage: http://github.com/rkumar/subcommand