Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

check empty subcommand and print help #4

Merged
merged 2 commits into from over 1 year ago

2 participants

Zhao Cai r__k_u_m_a_r
Zhao Cai

as title

r__k_u_m_a_r rkumar merged commit c96653b into from October 09, 2012
r__k_u_m_a_r rkumar closed this October 09, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Sep 14, 2012
Zhao Cai check empty command and print help 20c0ed4
Oct 02, 2012
Zhao Cai unify print_help bf4f809
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 24 additions and 17 deletions. Show diff stats Hide diff stats

  1. 41  lib/subcommand.rb
41  lib/subcommand.rb
@@ -5,9 +5,9 @@
5 5
 # as well as summarizes subcommands in global help.
6 6
 #
7 7
 # Thanks to Robert Klemme for his idea on lazy loading the subcommand option parsers.
8  
-# 
  8
+#
9 9
 # @author Rahul Kumar, Jun  2010
10  
-# @date 2010-06-20 22:33 
  10
+# @date 2010-06-20 22:33
11 11
 #
12 12
 # @examples
13 13
 # if a program has subcommands foo and baz
@@ -19,7 +19,7 @@
19 19
 # ruby subcommand.rb baz --quiet "some text"
20 20
 # ruby subcommand.rb --verbose foo --force file.zzz
21 21
 #
22  
-# == STEPS 
  22
+# == STEPS
23 23
 #    1. define global_options (optional)
24 24
 #
25 25
 #     global_options do |opts|
@@ -66,7 +66,7 @@ def command *names
66 66
     @commands ||= {}
67 67
     @aliases ||= {}
68 68
     if names.length > 0
69  
-      names.each do |n| 
  69
+      names.each do |n|
70 70
         #puts "aliases #{n} => #{name} "
71 71
         @aliases[n.to_s] = name.to_s
72 72
       end
@@ -91,6 +91,7 @@ def global_options
91 91
     end
92 92
   end
93 93
 
  94
+
94 95
   # Added so applications can print out a bare listing of top level commands
95 96
   #  for dynamic custom completion.
96 97
   def list_actions
@@ -100,7 +101,7 @@ def list_actions
100 101
 
101 102
   def print_actions
102 103
     cmdtext = "Commands are:"
103  
-    @commands.each_pair do |c, opt| 
  104
+    @commands.each_pair do |c, opt|
104 105
       #puts "inside opt.call loop"
105 106
       desc = opt.call.description
106 107
       cmdtext << "\n   #{c} : #{desc}"
@@ -108,7 +109,7 @@ def print_actions
108 109
 
109 110
     # print aliases
110 111
     unless @aliases.empty?
111  
-      cmdtext << "\n\nAliases: \n" 
  112
+      cmdtext << "\n\nAliases: \n"
112 113
       @aliases.each_pair { |name, val| cmdtext << "   #{name} - #{val}\n"  }
113 114
     end
114 115
 
@@ -134,12 +135,17 @@ def add_subcommand_help
134 135
   def add_help_option
135 136
     global_options do |opts|
136 137
       opts.on("-h", "--help", "Print this help") do |v|
137  
-        add_subcommand_help
138  
-        puts @global
  138
+        print_help
139 139
         exit
140 140
       end
141 141
     end
142 142
   end
  143
+
  144
+  def print_help
  145
+    add_subcommand_help
  146
+    puts @global
  147
+  end
  148
+
143 149
   # first parse global optinos
144 150
   # then parse subcommand options if valid subcommand
145 151
   # special case of "help command" so we print help of command - git style (3)
@@ -154,8 +160,7 @@ def opt_parse
154 160
         opts.separator ""
155 161
         opts.separator "Global options are:"
156 162
         opts.on("-h", "--help", "Print this help") do |v|
157  
-          add_subcommand_help
158  
-          puts @global
  163
+          print_help
159 164
           exit
160 165
         end
161 166
         opts.separator ""
@@ -167,7 +172,7 @@ def opt_parse
167 172
     cmd = ARGV.shift
168 173
     if cmd
169 174
       #$stderr.puts "Command: #{cmd}, args:#{ARGV}, #{@commands.keys} "
170  
-      sc = @commands[cmd] 
  175
+      sc = @commands[cmd]
171 176
       #puts "sc: #{sc}: #{@commands}"
172 177
       unless sc
173 178
         # see if an alias exists
@@ -190,20 +195,22 @@ def opt_parse
190 195
           if sc
191 196
             #puts " 111 help #{cmd}"
192 197
             puts sc.call
193  
-          else 
  198
+          else
194 199
             # no help for this command XXX check for alias
195 200
             puts "Invalid command: #{cmd}."
196  
-            add_subcommand_help
197  
-            puts @global
  201
+            print_help
198 202
           end
199 203
         else
200  
-          # invalid command 
  204
+          # invalid command
201 205
           puts "Invalid command: #{cmd}" unless cmd == "help"
202  
-          add_subcommand_help
203  
-          puts @global 
  206
+          print_help
204 207
         end
205 208
         exit 0
206 209
       end
  210
+    else
  211
+      puts "Empty command!"
  212
+      print_help
  213
+      exit 1
207 214
     end
208 215
     return @command_name
209 216
   end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.