Permalink
Browse files

teach custom commands to respect `-h` & `--help` flags

Fixes #151
  • Loading branch information...
1 parent a4b7a20 commit 9afdd8d029a63dde5f68aab6c71af9a228335472 @mislav mislav committed Mar 20, 2012
Showing with 49 additions and 3 deletions.
  1. +33 −3 lib/hub/commands.rb
  2. +16 −0 test/hub_test.rb
View
@@ -38,20 +38,26 @@ module Commands
OWNER_RE = /[a-zA-Z0-9-]+/
NAME_WITH_OWNER_RE = /^(?:#{NAME_RE}|#{OWNER_RE}\/#{NAME_RE})$/
+ CUSTOM_COMMANDS = %w[alias create browse compare fork pull-request]
+
def run(args)
slurp_global_flags(args)
# Hack to emulate git-style
args.unshift 'help' if args.empty?
cmd = args[0]
- expanded_args = expand_alias(cmd)
- cmd = expanded_args[0] if expanded_args
+ if expanded_args = expand_alias(cmd)
+ cmd = expanded_args[0]
+ expanded_args.concat args[1..-1]
+ end
+
+ respect_help_flags(expanded_args || args) if custom_command? cmd
# git commands can have dashes
cmd = cmd.sub(/(\w)-/, '\1_')
if method_defined?(cmd) and cmd != 'run'
- args[0, 1] = expanded_args if expanded_args
+ args.replace expanded_args if expanded_args
send(cmd, args)
end
rescue Errno::ENOENT
@@ -689,6 +695,30 @@ def help(args)
# from the command line.
#
+ def custom_command? cmd
+ CUSTOM_COMMANDS.include? cmd
+ end
+
+ # Show short usage help for `-h` flag, and open man page for `--help`
+ def respect_help_flags args
+ return if args.size > 2
+ case args[1]
+ when '-h'
+ pattern = /(git|hub) #{Regexp.escape args[0].gsub('-', '\-')}/
+ hub_raw_manpage.each_line { |line|
+ if line =~ pattern
+ $stderr.print "Usage: "
+ $stderr.puts line.gsub(/\\f./, '').gsub('\-', '-')
+ abort
+ end
+ }
+ abort "Error: couldn't find usage help for #{args[0]}"
+ when '--help'
+ puts hub_manpage
+ exit
+ end
+ end
+
# The text print when `hub help` is run, kept in its own method
# for the convenience of the author.
def improved_help_text
View
@@ -1053,6 +1053,22 @@ def test_help_hub
config
end
+ def test_help_flag_on_command
+ help_manpage = hub("browse --help")
+ assert_includes "git + hub = github", help_manpage
+ assert_includes "git browse", help_manpage
+ end
+
+ def test_help_short_flag_on_command
+ usage_help = hub("create -h")
+ expected = "Usage: git create [NAME] [-p] [-d DESCRIPTION] [-h HOMEPAGE]\n"
+ assert_equal expected, usage_help
+
+ usage_help = hub("pull-request -h")
+ expected = "Usage: git pull-request [-f] [TITLE|-i ISSUE] [-b BASE] [-h HEAD]\n"
+ assert_equal expected, usage_help
+ end
+
def test_help_hub_no_groff
stub_available_commands()
assert_equal "** Can't find groff(1)\n", hub("help hub")

0 comments on commit 9afdd8d

Please sign in to comment.