Skip to content

Commit

Permalink
check empty command and print help
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaocai committed Sep 14, 2012
1 parent 101c095 commit 20c0ed4
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions lib/subcommand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# as well as summarizes subcommands in global help.
#
# Thanks to Robert Klemme for his idea on lazy loading the subcommand option parsers.
#
#
# @author Rahul Kumar, Jun 2010
# @date 2010-06-20 22:33
# @date 2010-06-20 22:33
#
# @examples
# if a program has subcommands foo and baz
Expand All @@ -19,7 +19,7 @@
# ruby subcommand.rb baz --quiet "some text"
# ruby subcommand.rb --verbose foo --force file.zzz
#
# == STEPS
# == STEPS
# 1. define global_options (optional)
#
# global_options do |opts|
Expand Down Expand Up @@ -66,7 +66,7 @@ def command *names
@commands ||= {}
@aliases ||= {}
if names.length > 0
names.each do |n|
names.each do |n|
#puts "aliases #{n} => #{name} "
@aliases[n.to_s] = name.to_s
end
Expand All @@ -91,6 +91,7 @@ def global_options
end
end


# Added so applications can print out a bare listing of top level commands
# for dynamic custom completion.
def list_actions
Expand All @@ -100,15 +101,15 @@ def list_actions

def print_actions
cmdtext = "Commands are:"
@commands.each_pair do |c, opt|
@commands.each_pair do |c, opt|
#puts "inside opt.call loop"
desc = opt.call.description
cmdtext << "\n #{c} : #{desc}"
end

# print aliases
unless @aliases.empty?
cmdtext << "\n\nAliases: \n"
cmdtext << "\n\nAliases: \n"
@aliases.each_pair { |name, val| cmdtext << " #{name} - #{val}\n" }
end

Expand Down Expand Up @@ -140,6 +141,12 @@ def add_help_option
end
end
end

def print_help
add_subcommand_help
puts @global
end

# first parse global optinos
# then parse subcommand options if valid subcommand
# special case of "help command" so we print help of command - git style (3)
Expand All @@ -154,8 +161,7 @@ def opt_parse
opts.separator ""
opts.separator "Global options are:"
opts.on("-h", "--help", "Print this help") do |v|
add_subcommand_help
puts @global
print_help
exit
end
opts.separator ""
Expand All @@ -167,7 +173,7 @@ def opt_parse
cmd = ARGV.shift
if cmd
#$stderr.puts "Command: #{cmd}, args:#{ARGV}, #{@commands.keys} "
sc = @commands[cmd]
sc = @commands[cmd]
#puts "sc: #{sc}: #{@commands}"
unless sc
# see if an alias exists
Expand All @@ -190,20 +196,22 @@ def opt_parse
if sc
#puts " 111 help #{cmd}"
puts sc.call
else
else
# no help for this command XXX check for alias
puts "Invalid command: #{cmd}."
add_subcommand_help
puts @global
print_help
end
else
# invalid command
# invalid command
puts "Invalid command: #{cmd}" unless cmd == "help"
add_subcommand_help
puts @global
print_help
end
exit 0
end
else
puts "Empty command!"
print_help
exit 1
end
return @command_name
end
Expand Down

0 comments on commit 20c0ed4

Please sign in to comment.