Browse Source

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
android-5
Fredrik Fornwall 9 years ago
parent
commit
0ec2b70462
  1. 2
      packages/termux-api/build.sh
  2. 19
      packages/termux-api/termux-clipboard-get
  3. 26
      packages/termux-api/termux-clipboard-set
  4. 54
      packages/termux-api/termux-dialog
  5. 64
      packages/termux-api/termux-notification
  6. 35
      packages/termux-api/termux-sms-send

2
packages/termux-api/build.sh

@ -1,6 +1,6 @@
TERMUX_PKG_HOMEPAGE=http://termux.com/add-ons/api/ TERMUX_PKG_HOMEPAGE=http://termux.com/add-ons/api/
TERMUX_PKG_DESCRIPTION="Termux API commands" TERMUX_PKG_DESCRIPTION="Termux API commands"
TERMUX_PKG_VERSION=0.15 TERMUX_PKG_VERSION=0.16
termux_step_make_install () { termux_step_make_install () {
mkdir -p $TERMUX_PREFIX/bin mkdir -p $TERMUX_PREFIX/bin

19
packages/termux-api/termux-clipboard-get

@ -1,3 +1,22 @@
#!/bin/sh #!/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 @TERMUX_API@ Clipboard

26
packages/termux-api/termux-clipboard-set

@ -1,9 +1,29 @@
#!/bin/sh #!/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 if [ $# = 0 ]; then
TEXT=`cat -` $CMD
else else
TEXT="$@" echo $@ | $CMD
fi fi
@TERMUX_API@ Clipboard --es text "$TEXT"

54
packages/termux-api/termux-dialog

@ -1,34 +1,40 @@
#!/bin/sh #!/bin/sh
set -e -u set -e -u
SCRIPTNAME=$0 SCRIPTNAME=termux-dialog
show_usage () { show_usage () {
echo "usage: $SCRIPTNAME [OPTIONS]" echo "Usage: $SCRIPTNAME [OPTIONS]"
echo " Show a text entry dialog." echo "Show a text entry dialog."
echo " -i, --input-hint <hint> The input hint to show when the input is empty" echo ""
echo " -m, --multiple-lines Use a textarea with multiple lines instead of a single" echo " -i <hint> The input hint to show when the input is empty"
echo " -p, --password Enter the input as a password" echo " -m Use a textarea with multiple lines instead of a single"
echo " -t, --title <title> The title to show for the input prompt" echo " -p Enter the input as a password"
echo " -t <title> The title to show for the input prompt"
exit 0
} }
PARAMS="" 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 ARG_I=""
eval set -- "$O" OPT_I=""
while true; do ARG_M=""
case "$1" in ARG_P=""
-h|--help) show_usage; exit 0;; ARG_T=""
-i|--input-hint) PARAMS="$PARAMS --es input_hint '$2'"; shift 2;; OPT_T=""
-m|--multiple-lines) PARAMS="$PARAMS --ez multiple_lines true"; shift 1;;
-p|--password) PARAMS="$PARAMS --es input_type password"; shift 1;; while getopts :hi:mpt: option
-t|--title) PARAMS="$PARAMS --es input_title '$2'"; shift 2;; do
--) shift; break;; case "$option" in
*) echo Error; exit 1;; h) show_usage;;
esac 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 done
shift $(($OPTIND-1))
# Too many arguments: if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
if [ $# != 0 ]; then show_usage; exit 1; fi
eval @TERMUX_API@ Dialog $PARAMS @TERMUX_API@ Dialog $ARG_I "$OPT_I" $ARG_M $ARG_P $ARG_T "$OPT_T"

64
packages/termux-api/termux-notification

@ -1,38 +1,46 @@
#!/bin/sh #!/bin/sh
set -e -u
set -u SCRIPTNAME=termux-notification
PARAMS=""
CONTENT_OR_TITLE_SET=no
SCRIPTNAME=$0
show_usage () { show_usage () {
echo "usage: termux-notification [OPTIONS]" echo "usage: termux-notification [OPTIONS]"
echo "Display a notification. Options:" echo "Display a system notification."
echo " -c, --content <content> notification content to show" echo ""
echo " -i, --id <id> notification id (will overwrite the previous notification with the same id)" echo " -c <content> notification content to show"
echo " -t, --title <title> notification title to show" echo " -i <id> notification id (will overwrite the previous notification with the same id)"
echo " -u, --url <url> notification url when clicking on it" 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: "$@"` CONTENT_OR_TITLE_SET=no
if [ $? != 0 ] ; then show_usage; exit 1 ; fi ARG_C=""
eval set -- "$O" OPT_C=""
while true; do ARG_I=""
case "$1" in OPT_I=""
-c|--content) PARAMS="$PARAMS --es content '$2'"; CONTENT_OR_TITLE_SET=yes; shift 2;; ARG_T=""
-h|--help) show_usage; exit 0;; OPT_T=""
-i|--id) PARAMS="$PARAMS --es id $2"; shift 2;; ARG_U=""
-t|--title) PARAMS="$PARAMS --es title '$2'"; CONTENT_OR_TITLE_SET=yes; shift 2;; OPT_U=""
-u|--url) PARAMS="$PARAMS --es url '$2'"; shift 2;;
--) shift; break;; while getopts :hc:i:t:u: option
*) echo Error; exit 1;; do
esac 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 done
shift $(($OPTIND-1))
if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
if [ $CONTENT_OR_TITLE_SET = "no" ]; then if [ $CONTENT_OR_TITLE_SET = "no" ]; then
show_usage echo "$SCRIPTNAME: no title or content set"
exit 1; exit 1
fi; 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"

35
packages/termux-api/termux-sms-send

@ -1,10 +1,35 @@
#!/bin/sh #!/bin/sh
set -e -u set -e -u
if [ $# != 1 -o $1 = "-h" ]; then SCRIPTNAME=termux-sms-send
echo "usage: termux-sms-send <recipient-number>" show_usage () {
echo "Send a SMS message given on stdin." echo "Usage: $SCRIPTNAME [-t <text>] <recipient-number>"
exit 1 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 fi
@TERMUX_API@ SmsSend --es recipient $1

Loading…
Cancel
Save