From a7a62352c46b5d2b37baaa09f8145b1171ca79fb Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl <trygvis@inamo.no>
Date: Sun, 14 Oct 2012 01:14:32 +0200
Subject: o Consistent output when the method is missing.

---
 .app/lib/app-app  | 13 ++++++++++---
 app               | 16 ++++++++++++++--
 test/01-help.bats | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 5 deletions(-)
 create mode 100644 test/01-help.bats

diff --git a/.app/lib/app-app b/.app/lib/app-app
index 30d2007..130244e 100644
--- a/.app/lib/app-app
+++ b/.app/lib/app-app
@@ -6,7 +6,7 @@ then
 else
   repo="http://repo1.maven.org"
 fi
-
+ 
 # TODO: support file:// repositories
 # TODO: look in the local repository first
 # TODO: assert that we got a 200 OK
@@ -420,10 +420,10 @@ method_app_usage() {
 method_app() {
   local name="$1"; shift
   local instance="$1"; shift
+  local method="$1"
 
   if [ $# -gt 0 ]
   then
-    method=$1
     shift
   fi
 
@@ -432,7 +432,14 @@ method_app() {
     list)          method_list          "$name" "$instance" "$@" ;;
     list-versions) method_list_versions "$name" "$instance" "$@" ;;
     set-current)   method_set_current   "$name" "$instance" "$@" ;;
-    *)             method_app_usage "Unknown method $method" ;;
+    *)
+      if [ -z "$method" ]
+      then
+        method_app_usage
+      else
+        method_app_usage "Unknown method $method"
+      fi
+      ;;
   esac
   exit $?
 }
diff --git a/app b/app
index 45adaee..dc70ab0 100755
--- a/app
+++ b/app
@@ -13,6 +13,11 @@ mkdir -p $BASEDIR/.app/var/pid
 mkdir -p $BASEDIR/.app/var/download
 
 method_usage() {
+  if [ -n "$1" ]
+  then
+    echo "Error:" "$@" >&2
+  fi
+
   echo "usage: $0 [-n name] [-i instance] <method group>" >&2
   echo "" >&2
   echo "Available method groups:" >&2
@@ -78,9 +83,9 @@ main() {
     esac
   done
 
+  local method=$1
   if [ $# -gt 0 ]
   then
-    method=$1
     shift
   fi
 
@@ -88,7 +93,14 @@ main() {
     app)           method_app     "$name" "$instance" "$@" ;;
     conf)          method_conf    "$name" "$instance" "$@" ;;
     operate)       method_operate "$name" "$instance" "$@" ;;
-    *)             method_usage ;;
+    *)             
+      if [ -z "$method" ]
+      then
+        method_usage
+      else
+        method_usage "No such method group: $method"
+      fi
+      ;;
   esac
   exit $?
 }
diff --git a/test/01-help.bats b/test/01-help.bats
new file mode 100644
index 0000000..7d38592
--- /dev/null
+++ b/test/01-help.bats
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+workdir=test-run
+
+# TODO: assert that the exit code is 1 for 'usage' outputs.
+exit_usage=0
+
+setup() {
+  rm -rf $workdir
+}
+
+echo_lines() {
+  for line in "${lines[@]}"; do echo $line; done
+}
+
+@test "./app" {
+  run ./app; echo_lines
+  [ $status -eq $exit_usage ]
+  [ $(expr "${lines[0]}" : "usage: ./app .*") -ne 0 ]
+}
+
+@test "./app foo" {
+  run ./app foo; echo_lines
+  [ $status -eq $exit_usage ]
+  [ "${lines[0]}" = "Error: No such method group: foo" ]
+  [ $(expr "${lines[1]}" : "usage: ./app .*") -ne 0 ]
+}
+
+@test "./app app" {
+  run ./app app; echo_lines
+  [ $status -eq $exit_usage ]
+  [ $(expr "${lines[0]}" : "usage: ./app app .*") -ne 0 ]
+}
-- 
cgit v1.2.3