updates getopt parsing and supports long opts. removes shortening

This commit is contained in:
James Tomasino 2022-11-03 16:57:07 +00:00
parent a6cea6f11a
commit c5d06b17f9
2 changed files with 26 additions and 75 deletions

87
pb
View File

@ -1,13 +1,13 @@
#!/bin/sh #!/bin/sh
# init variables # init variables
version="v2020.10.27" version="v2022.11.03"
ENDPOINT="https://ttm.sh" ENDPOINT="https://ttm.sh"
flag_options=":hvcufe:s::" flag_options=":hvcfe:s:"
long_flag_options="help,version,color,file,extension:,server:"
flag_version=0 flag_version=0
flag_help=0 flag_help=0
flag_file=0 flag_file=0
flag_url=0
flag_colors=0 flag_colors=0
flag_ext=0 flag_ext=0
data="" data=""
@ -23,13 +23,12 @@ or
Uploads a file or data to the tilde 0x0 paste bin Uploads a file or data to the tilde 0x0 paste bin
OPTIONAL FLAGS: OPTIONAL FLAGS:
-h Show this help -h | --help) Show this help
-v Show current version number -v | --version) Show current version number
-f Explicitly interpret stdin as filename -f | --file) Explicitly interpret stdin as filename
-c Pretty color output -c | --color) Pretty color output
-u Shorten URL -s | --server server_address) Use alternative pastebin server address
-s server_address Use alternative pastebin server address -e | --extension bin_extension) Specify file extension used in the upload
-e bin_extension Specify a binary file extension used in the upload
END END
} }
@ -59,49 +58,27 @@ die () {
} }
# attempt to parse options or die # attempt to parse options or die
if ! parsed=$(getopt ${flag_options} "$@"); then if ! PARSED_ARGUMENTS=$(getopt -a -n pb -o ${flag_options} --long ${long_flag_options} -- "$@"); then
printf "pb: unknown option\\n" printf "pb: unknown option\\n"
show_usage show_usage
exit 2 exit 2
fi fi
# handle options # For debugging: echo "PARSED_ARGUMENTS is $PARSED_ARGUMENTS"
eval set -- "${parsed}" eval set -- "$PARSED_ARGUMENTS"
while true; do while :
do
case "$1" in case "$1" in
-h|?) -h | --help) flag_help=1 ; shift ;;
flag_help=1 -v | --version) flag_version=1 ; shift ;;
;; -c | --color) flag_color=1 ; shift ;;
-v) -f | --file) flag_file=1 ; shift ;;
flag_version=1 -e | --extension) flag_ext=1; EXT="$2" ; shift 2 ;;
;; -s | --server) ENDPOINT="$2" ; shift 2 ;;
-c) --) shift; break ;;
flag_colors=1 *) echo "Unexpected option: $1 - this should not happen."
;; show_usage ; die 3 ;;
-f)
flag_file=1
;;
-e)
shift
flag_ext=1
EXT="$1"
;;
-s)
shift
ENDPOINT="$1"
;;
-u)
flag_url=1
;;
--)
shift
break
;;
*)
die "Internal error: $1" 3
;;
esac esac
shift
done done
# display current version # display current version
@ -145,24 +122,6 @@ else
RESET="" RESET=""
fi fi
# URL shortening reference
# If URL mode detected, process URL shortener and end processing without
# checking for a file to upload to the pastebin
if [ ${flag_url} -gt 0 ]; then
if [ -z "${data}" ]; then
# if no data
# print error message
printf "%sProvide URL to shorten%s\\n" "$ERROR" "$RESET"
else
# shorten URL and print results
result=$(curl -sF"shorten=${data}" "${ENDPOINT}")
printf "%s%s%s\\n" "$SUCCESS" "$result" "$RESET"
fi
die "" 0
fi
if [ ${flag_file} -gt 0 ]; then if [ ${flag_file} -gt 0 ]; then
# file mode # file mode
if [ -z "${data}" ]; then if [ -z "${data}" ]; then

14
pb.1
View File

@ -1,4 +1,4 @@
.TH PB 1 "27 October 2020" "v2020.10.27" .TH PB 1 "03 November 2022" "v2022.11.03"
.SH NAME .SH NAME
pb \- a helper utility for using 0x0 pastebin services pb \- a helper utility for using 0x0 pastebin services
@ -16,8 +16,8 @@ comes pre-configured with a specific pastebin, the
service endpoint can be overridden. service endpoint can be overridden.
Data input can be provided as an argument or via stdin. Data input can be provided as an argument or via stdin.
The data will be processed as an image, text or URL to The data will be processed as binary or text
be shortened based on the context. based on the context.
The options are as follows: The options are as follows:
@ -32,11 +32,6 @@ Use alternative pastebin server address.
Specifes the file extension used in the upload of binary content passed to Specifes the file extension used in the upload of binary content passed to
.B pb .B pb
via standard input. via standard input.
.I Note:
this attribute will not work with file uploads or URL shortening.
.TP
.B -u
Shorten a URL.
.TP .TP
.B -c .B -c
Pretty color output. Pretty color output.
@ -69,9 +64,6 @@ Upload a list of files to the pastebin individually
.B pb -s http://0x0.st scores.txt .B pb -s http://0x0.st scores.txt
Upload a file to a different pastebin endpoint Upload a file to a different pastebin endpoint
.TP .TP
.B pb -u 'https://tilde.team'
Shorten the URL to tilde.team
.TP
.B curl -s https://some/image/file.png | pb -e "png" .B curl -s https://some/image/file.png | pb -e "png"
Download a binary file and re-upload it to the pastebin with an explicit binary Download a binary file and re-upload it to the pastebin with an explicit binary
type and extension. type and extension.