mirror of https://git.envs.net/envs/burrow.git
refactor arg parsing to be order agnostic
This commit is contained in:
parent
f5566efb44
commit
955fd09e81
70
burrow
70
burrow
|
@ -5,6 +5,9 @@ shopt -s extglob
|
||||||
configfiles="$HOME/.config/burrow/config $HOME/.config/burrow $HOME/.burrow"
|
configfiles="$HOME/.config/burrow/config $HOME/.config/burrow $HOME/.burrow"
|
||||||
editor=${EDITOR:-vi}
|
editor=${EDITOR:-vi}
|
||||||
|
|
||||||
|
arg_options=hvd
|
||||||
|
arg_longoptions=help,version,debug
|
||||||
|
|
||||||
# vars from config
|
# vars from config
|
||||||
config_location_gopher="$HOME/gopher"
|
config_location_gopher="$HOME/gopher"
|
||||||
config_location_recipebox="${config_location_gopher}/recipebox"
|
config_location_recipebox="${config_location_gopher}/recipebox"
|
||||||
|
@ -26,7 +29,7 @@ arg_update_git=0
|
||||||
|
|
||||||
function show_help() {
|
function show_help() {
|
||||||
cat > /dev/stdout << END
|
cat > /dev/stdout << END
|
||||||
${0} [-hv] <arguments>
|
${0} [-hvd] [--help] [--version] [--debug] <arguments>
|
||||||
|
|
||||||
ARGUMENTS (multiple allowed):
|
ARGUMENTS (multiple allowed):
|
||||||
phlog - create new phlog entry
|
phlog - create new phlog entry
|
||||||
|
@ -35,9 +38,9 @@ create-config - create a default configuration file
|
||||||
update-git - silently pulls gopher git repo if it exists
|
update-git - silently pulls gopher git repo if it exists
|
||||||
|
|
||||||
OPTIONAL FLAGS:
|
OPTIONAL FLAGS:
|
||||||
-h - show this help
|
--help|-h - show this help
|
||||||
-v - show current version info
|
--version|-v - show current version info
|
||||||
-d - debug mode
|
--debug|-d - debug mode
|
||||||
|
|
||||||
END
|
END
|
||||||
}
|
}
|
||||||
|
@ -50,33 +53,54 @@ popd () {
|
||||||
command popd 2>/dev/null 1>&2
|
command popd 2>/dev/null 1>&2
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseopts() {
|
function parse_input() {
|
||||||
while getopts ":hvd" opt
|
getopt --test > /dev/null
|
||||||
do
|
if [[ $? -ne 4 ]]
|
||||||
case "${opt}" in
|
then
|
||||||
h)
|
echo "I'm sorry, \`getopt --test\` failed in this environment."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
parsed=$(getopt --options=$arg_options --longoptions=$arg_longoptions --name "$0" -- "$@")
|
||||||
|
if [[ $? -ne 0 ]]; then
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
eval set -- "$parsed"
|
||||||
|
|
||||||
|
while true; do
|
||||||
|
case "$1" in
|
||||||
|
-h|--help)
|
||||||
show_help
|
show_help
|
||||||
exit 0
|
shift
|
||||||
|
;;
|
||||||
|
-v|--version)
|
||||||
|
echo "$version"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-d|--debug)
|
||||||
|
flag_debug=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--)
|
||||||
|
shift
|
||||||
|
break
|
||||||
;;
|
;;
|
||||||
d) flag_debug=1 ;;
|
|
||||||
v) echo "$version" ;;
|
|
||||||
*)
|
*)
|
||||||
echo "Invalid option. Try -h for help."
|
echo "Internal error: $1"
|
||||||
exit 1
|
exit 3
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
return $OPTIND
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseargs() {
|
if [[ $# -lt 1 ]]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
for arg in "$@"
|
for arg in "$@"
|
||||||
do
|
do
|
||||||
argc=${arg^^}
|
argc=${arg^^}
|
||||||
case $argc in
|
case $argc in
|
||||||
"HELP")
|
|
||||||
show_help
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
"SHORTLIST")
|
"SHORTLIST")
|
||||||
echo "phlog recipe create-config update-git"
|
echo "phlog recipe create-config update-git"
|
||||||
exit 0
|
exit 0
|
||||||
|
@ -87,6 +111,7 @@ function parseargs() {
|
||||||
"UPDATE-GIT") arg_update_git=1 ;;
|
"UPDATE-GIT") arg_update_git=1 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function day_suffix() {
|
function day_suffix() {
|
||||||
|
@ -279,8 +304,7 @@ function update_git() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
parseopts "$@"
|
parse_input "$@"
|
||||||
parseargs "${@:$?}"
|
|
||||||
|
|
||||||
if [[ ${flag_debug} -gt 0 ]]
|
if [[ ${flag_debug} -gt 0 ]]
|
||||||
then
|
then
|
||||||
|
|
6
burrow.1
6
burrow.1
|
@ -27,13 +27,13 @@ silent
|
||||||
.B git-pull
|
.B git-pull
|
||||||
which is useful for crons.
|
which is useful for crons.
|
||||||
.TP
|
.TP
|
||||||
.B -v
|
.B -v|--version
|
||||||
Display current version information.
|
Display current version information.
|
||||||
.TP
|
.TP
|
||||||
.B -d
|
.B -d|--debug
|
||||||
Debug mode.
|
Debug mode.
|
||||||
.TP
|
.TP
|
||||||
.B -h
|
.B -h|--help
|
||||||
Show the help.
|
Show the help.
|
||||||
.SH FILES
|
.SH FILES
|
||||||
.I ~/.config/burrow/config
|
.I ~/.config/burrow/config
|
||||||
|
|
Loading…
Reference in New Issue