Update bin/star_trek_date so that arguments can be optional, and placed anywhere, and if -l to list languages is used, all other options are ignored

This commit is contained in:
MeaTLoTioN 2024-06-19 10:44:17 +01:00
parent debbec732f
commit 6c7d1eec4a

View File

@ -63,32 +63,39 @@ translate_date() {
# Check for command line arguments # Check for command line arguments
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
echo "Usage: $(basename $0) <language> [-d|--debug] [-l|--list]" echo "Usage: $(basename $0) [-d] [-l] <language>"
exit 1 exit 1
fi fi
# Parse command line arguments # Parse command line options using getopts
language=${1,,} while getopts "ld" opt; do
shift case ${opt} in
args=${1,,} l) # List available languages
while [[ "$args" ]]; do
case $args in
-d|--debug)
DEBUG=true
;;
-l|--list)
list_languages list_languages
exit 0 exit 0
;; ;;
*) d) # Enable debug mode
echo "Unknown option: $1" DEBUG=true
echo "Usage: $0 <language> [-d|--debug] [-l|--list]" ;;
\?) # Invalid option
echo "Invalid option: -$OPTARG"
exit 1 exit 1
;; ;;
esac esac
shift
done done
# Shift to the positional arguments after options
shift $((OPTIND -1))
# If no positional arguments left after processing options, show usage
if [ $# -eq 0 ]; then
echo "Usage: $(basename $0) [-d] [-l] <language>"
exit 1
fi
# The first remaining argument should be the language
language=$1
# Call the function with the provided language # Call the function with the provided language
translate_date "$language" translate_date "$language"