From: Stefan Huber Date: Mon, 7 Jan 2013 23:00:29 +0000 (+0100) Subject: mp-play: use getopt to parse args X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=f4c94df3df15d0d529b821db0cb15e4a66253bcd;p=shutils.git mp-play: use getopt to parse args --- diff --git a/mp-play b/mp-play index cd40b2d..96e08d9 100755 --- a/mp-play +++ b/mp-play @@ -23,22 +23,28 @@ optSorttime=0 function showHelp { - echo "Plays mp3 files in all subdirectories." - echo "Usage: ./mp-play.sh [-h|--help] [-R|--random] [-r|--repeat] " - echo " [-a|-recursive] [--reverse] [--time]" - echo " [--remove] [-q|--quiet] [-v|--verbose] [DESTINATION]" + echo "Usage: $0 [-h|--help]" + echo " $0 [OPTIONS] [--] [DESTINATIONS]" echo "" - echo "Options:" - echo " --time sort by age of file" - echo " -R, --random randomize files" - echo " -r, --repeat repeat playing infintely" + echo "OPTIONS:" echo " -a, --recursive recursively find files to play" - echo " --reverse reverse playing order" - echo " --remove ask to remove played files after playing" + echo " -h, --help show this message" echo " -q, --quiet do not produce output when playing" - echo " -v, --versbose produce more output" + echo " -r, --repeat repeat playing infintely" + echo " -R, --random randomize files" + echo " --remove ask to remove played files after playing" + echo " --reverse reverse playing order" + echo " --time sort by age of file" + echo " -v, --verbose produce more output" } +if ! options=$(getopt -u -o hRravq -l time,random,repeat,recursive,reverse,remove,quiet,verbose -- "$@") +then + showHelp >&2 + exit 1 +fi +set -- $options + #Parse the arguments while [ $# -gt 0 ] do @@ -52,6 +58,8 @@ do --remove) optRemove=1 ;; --reverse) optReverse=1 ;; --time) optSorttime=1 ;; + --) shift; break ;; + -*) echo "Unrecognized option $1" >&2; showHelp >&2; exit 1 ;; *) break ;; esac shift @@ -64,7 +72,7 @@ if [ "$optRecursive" == "1" ]; then else maxlevel=1 fi - + # Create playlist find "$@" -maxdepth $maxlevel -type f \