termux-api: Start to cleanup api calling scripts
authorFredrik Fornwall <fredrik@fornwall.net>
Tue, 19 Apr 2016 14:34:01 +0000 (16:34 +0200)
committerFredrik Fornwall <fredrik@fornwall.net>
Tue, 19 Apr 2016 14:35:23 +0000 (16:35 +0200)
Start using shell built-in getops and remove long arguments.

Make both clipboard-set and sms-send either take argument or read
from stdin.  Fixes https://github.com/termux/termux-api/issues/14

packages/termux-api/build.sh
packages/termux-api/termux-clipboard-get
packages/termux-api/termux-clipboard-set
packages/termux-api/termux-dialog
packages/termux-api/termux-notification
packages/termux-api/termux-sms-send

index 362f45b..8746517 100644 (file)
@@ -1,6 +1,6 @@
 TERMUX_PKG_HOMEPAGE=http://termux.com/add-ons/api/
 TERMUX_PKG_DESCRIPTION="Termux API commands"
-TERMUX_PKG_VERSION=0.15
+TERMUX_PKG_VERSION=0.16
 
 termux_step_make_install () {
         mkdir -p $TERMUX_PREFIX/bin
index 3a39aac..99ccc90 100755 (executable)
@@ -1,3 +1,22 @@
 #!/bin/sh
+set -e -u
+
+SCRIPTNAME=termux-clipboard-get
+show_usage () {
+       echo "Usage: $SCRIPTNAME"
+       echo "Get the system clipboard text."
+       exit 0
+}
+
+while getopts :h option
+do
+    case "$option" in
+       h) show_usage;;
+       ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
+    esac
+done
+shift $(($OPTIND-1))
+
+if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
 
 @TERMUX_API@ Clipboard
index fb797ac..8486179 100755 (executable)
@@ -1,9 +1,29 @@
 #!/bin/sh
+set -e -u
 
+SCRIPTNAME=termux-clipboard-set
+show_usage () {
+       echo "Usage: $SCRIPTNAME <text>"
+       echo "Set the system clipboard text."
+       echo ""
+       echo "If no arguments are given the text to set is read from stdin,"
+       echo "otherwise all arguments given are used as the text to set."
+       exit 0
+}
+
+while getopts :h option
+do
+    case "$option" in
+       h) show_usage;;
+       ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
+    esac
+done
+shift $(($OPTIND-1))
+
+CMD="@TERMUX_API@ Clipboard -e api_version 2 --ez set true"
 if [ $# = 0 ]; then
-       TEXT=`cat -`
+       $CMD
 else
-       TEXT="$@"
+       echo $@ | $CMD
 fi
 
-@TERMUX_API@ Clipboard --es text "$TEXT"
index f827b2e..d968453 100755 (executable)
@@ -1,34 +1,40 @@
 #!/bin/sh
-
 set -e -u
 
-SCRIPTNAME=$0
+SCRIPTNAME=termux-dialog
 show_usage () {
-       echo "usage: $SCRIPTNAME [OPTIONS]"
-       echo "       Show a text entry dialog."
-       echo "       -i, --input-hint <hint>   The input hint to show when the input is empty"
-       echo "       -m, --multiple-lines      Use a textarea with multiple lines instead of a single"
-       echo "       -p, --password            Enter the input as a password"
-       echo "       -t, --title <title>       The title to show for the input prompt"
+       echo "Usage: $SCRIPTNAME [OPTIONS]"
+       echo "Show a text entry dialog."
+       echo ""
+       echo "  -i <hint>   The input hint to show when the input is empty"
+       echo "  -m          Use a textarea with multiple lines instead of a single"
+       echo "  -p          Enter the input as a password"
+       echo "  -t <title>  The title to show for the input prompt"
+       exit 0
 }
 
 PARAMS=""
-O=`busybox getopt -q -l help -l input-hint: -l multiple-lines -l password -l title: -- hi:mpt: "$@"`
-if [ $? != 0 ] ; then show_usage; exit 1 ; fi
-eval set -- "$O"
-while true; do
-case "$1" in
-       -h|--help) show_usage; exit 0;;
-       -i|--input-hint) PARAMS="$PARAMS --es input_hint '$2'"; shift 2;;
-       -m|--multiple-lines) PARAMS="$PARAMS --ez multiple_lines true"; shift 1;;
-       -p|--password) PARAMS="$PARAMS --es input_type password"; shift 1;;
-       -t|--title) PARAMS="$PARAMS --es input_title '$2'"; shift 2;;
-       --)     shift; break;;
-       *)      echo Error; exit 1;;
-esac
+
+ARG_I=""
+OPT_I=""
+ARG_M=""
+ARG_P=""
+ARG_T=""
+OPT_T=""
+
+while getopts :hi:mpt: option
+do
+       case "$option" in
+               h) show_usage;;
+               i) ARG_I="--es input_hint";  OPT_I="$OPTARG";;
+               m) ARG_M="--ez multiple_lines true";;
+               p) ARG_P="--es input_type password";;
+               t) ARG_T="--es input_title"; OPT_T="$OPTARG";;
+               ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
+       esac
 done
+shift $(($OPTIND-1))
 
-# Too many arguments:
-if [ $# != 0 ]; then show_usage; exit 1; fi
+if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
 
-eval @TERMUX_API@ Dialog $PARAMS
+@TERMUX_API@ Dialog $ARG_I "$OPT_I" $ARG_M $ARG_P $ARG_T "$OPT_T"
index 420b6a5..96a0e2a 100755 (executable)
@@ -1,38 +1,46 @@
 #!/bin/sh
+set -e -u
 
-set -u
-
-PARAMS=""
-CONTENT_OR_TITLE_SET=no
-
-SCRIPTNAME=$0
+SCRIPTNAME=termux-notification
 show_usage () {
        echo "usage: termux-notification [OPTIONS]"
-       echo "Display a notification. Options:"
-       echo "  -c, --content <content>      notification content to show"
-       echo "  -i, --id <id>                notification id (will overwrite the previous notification with the same id)"
-       echo "  -t, --title <title>          notification title to show"
-       echo "  -u, --url <url>              notification url when clicking on it"
+       echo "Display a system notification."
+       echo ""
+       echo "  -c <content> notification content to show"
+       echo "  -i <id>      notification id (will overwrite the previous notification with the same id)"
+       echo "  -t <title>   notification title to show"
+       echo "  -u <url>     notification url when clicking on it"
+       exit 1
 }
 
-O=`busybox getopt -q -l content: -l help -l title: -l id: -l url: -- c:hi:t:u: "$@"`
-if [ $? != 0 ] ; then show_usage; exit 1 ; fi
-eval set -- "$O"
-while true; do
-case "$1" in
-       -c|--content) PARAMS="$PARAMS --es content '$2'"; CONTENT_OR_TITLE_SET=yes; shift 2;;
-       -h|--help) show_usage; exit 0;;
-       -i|--id) PARAMS="$PARAMS --es id $2"; shift 2;;
-       -t|--title) PARAMS="$PARAMS --es title '$2'"; CONTENT_OR_TITLE_SET=yes; shift 2;;
-       -u|--url) PARAMS="$PARAMS --es url '$2'"; shift 2;;
-       --)     shift; break;;
-       *)      echo Error; exit 1;;
-esac
+CONTENT_OR_TITLE_SET=no
+ARG_C=""
+OPT_C=""
+ARG_I=""
+OPT_I=""
+ARG_T=""
+OPT_T=""
+ARG_U=""
+OPT_U=""
+
+while getopts :hc:i:t:u: option
+do
+       case "$option" in
+               h) show_usage;;
+               c) ARG_C="--es content"; OPT_C="$OPTARG"; CONTENT_OR_TITLE_SET=yes;;
+               i) ARG_I="--es id"; OPT_I="$OPTARG";;
+               t) ARG_T="--es title"; OPT_T="$OPTARG"; CONTENT_OR_TITLE_SET=yes;;
+               u) ARG_U="--es url"; OPT_U="$OPTARG";;
+               ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
+       esac
 done
+shift $(($OPTIND-1))
+
+if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
 
 if [ $CONTENT_OR_TITLE_SET = "no" ]; then
-       show_usage
-       exit 1;
-fi;
+       echo "$SCRIPTNAME: no title or content set"
+       exit 1
+fi
 
-eval @TERMUX_API@ Notification $PARAMS
+@TERMUX_API@ Notification $ARG_C "$OPT_C" $ARG_I "$OPT_I" $ARG_T "$OPT_T" $ARG_U "$OPT_U"
index 67b0f31..43664a5 100755 (executable)
@@ -1,10 +1,35 @@
 #!/bin/sh
 set -e -u
 
-if [ $# != 1 -o $1 = "-h" ]; then
-       echo "usage: termux-sms-send <recipient-number>"
-       echo "Send a SMS message given on stdin."
-       exit 1
+SCRIPTNAME=termux-sms-send
+show_usage () {
+       echo "Usage: $SCRIPTNAME [-t <text>] <recipient-number>"
+       echo "Send a SMS."
+       echo ""
+       echo "  -t <text>  The text to send (optional - else from stdin)"
+       echo ""
+       echo "If no text is specified with the -t option the text to send is read from stdin."
+       exit 0
+}
+
+TEXT_TO_SEND=""
+while getopts :ht: option
+do
+       case "$option" in
+               h) show_usage;;
+               t) TEXT_TO_SEND="$OPTARG";;
+               ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
+       esac
+done
+shift $(($OPTIND-1))
+
+if [ $# = 0 ]; then echo "$SCRIPTNAME: too few arguments"; exit 1; fi
+if [ $# -gt 1 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
+
+CMD="@TERMUX_API@ SmsSend --es recipient $1"
+if [ -z "$TEXT_TO_SEND" ]; then
+       $CMD
+else
+       echo $TEXT_TO_SEND | $CMD
 fi
 
-@TERMUX_API@ SmsSend --es recipient $1