From 0ec2b70462bf2052dd2080460112c3a8fc87f62e Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Tue, 19 Apr 2016 16:34:01 +0200 Subject: [PATCH] termux-api: Start to cleanup api calling scripts 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 | 2 +- packages/termux-api/termux-clipboard-get | 19 ++++++++++ packages/termux-api/termux-clipboard-set | 26 +++++++++++-- packages/termux-api/termux-dialog | 54 +++++++++++++++------------ packages/termux-api/termux-notification | 64 ++++++++++++++++++-------------- packages/termux-api/termux-sms-send | 35 ++++++++++++++--- 6 files changed, 139 insertions(+), 61 deletions(-) diff --git a/packages/termux-api/build.sh b/packages/termux-api/build.sh index 362f45bc..87465179 100644 --- a/packages/termux-api/build.sh +++ b/packages/termux-api/build.sh @@ -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 diff --git a/packages/termux-api/termux-clipboard-get b/packages/termux-api/termux-clipboard-get index 3a39aace..99ccc90e 100755 --- a/packages/termux-api/termux-clipboard-get +++ b/packages/termux-api/termux-clipboard-get @@ -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 diff --git a/packages/termux-api/termux-clipboard-set b/packages/termux-api/termux-clipboard-set index fb797ac3..8486179b 100755 --- a/packages/termux-api/termux-clipboard-set +++ b/packages/termux-api/termux-clipboard-set @@ -1,9 +1,29 @@ #!/bin/sh +set -e -u +SCRIPTNAME=termux-clipboard-set +show_usage () { + echo "Usage: $SCRIPTNAME " + 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" diff --git a/packages/termux-api/termux-dialog b/packages/termux-api/termux-dialog index f827b2e0..d9684533 100755 --- a/packages/termux-api/termux-dialog +++ b/packages/termux-api/termux-dialog @@ -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 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 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" diff --git a/packages/termux-api/termux-notification b/packages/termux-api/termux-notification index 420b6a55..96a0e2a5 100755 --- a/packages/termux-api/termux-notification +++ b/packages/termux-api/termux-notification @@ -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" diff --git a/packages/termux-api/termux-sms-send b/packages/termux-api/termux-sms-send index 67b0f310..43664a5c 100755 --- a/packages/termux-api/termux-sms-send +++ b/packages/termux-api/termux-sms-send @@ -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 -- 2.11.0