Skip to content

Instantly share code, notes, and snippets.

@LouisdeBruijn
Last active July 13, 2021 12:00
Show Gist options
  • Save LouisdeBruijn/59769d388fd6c5f574a6f62031e25bf4 to your computer and use it in GitHub Desktop.
Save LouisdeBruijn/59769d388fd6c5f574a6f62031e25bf4 to your computer and use it in GitHub Desktop.
#!/bin/bash
script="run.sh"
function example {
# specifies a script usage example
echo -e "example: sh $script -n Louis -b 20210709 -a 27 -l -d"
}
function usage {
# specifies the script's usage
echo -e "usage: $ sh $script -n <name> [-b <birth date>] [-a <age>] [-l <alive>] [-d] [-h|--help]\n"
}
function help {
usage
echo -e "MANDATORY:"
echo -e "\t -n specifies the name\n"
echo -e "OPTIONAL:"
echo -e "\t -b [date|YYYYMMDD] specifies the birth date, either use 'date' for current date or YYYYMMDD format"
echo -e "\t -a specifies the age of this person"
echo -e "\t -l specifies whether this person is currently alive"
echo -e "\t -d debug-mode: logging enabled"
echo -e "\t -h, --help prints this help\n"
}
function return_help {
# find command-line argument 'help' or 'h' and return help, exit script
for argument in "$@"
do
if [[ $argument == "--help" ]] || [[ $argument == "-h" ]]; then
help
exit
fi
done
}
# initialize all mandatory and optional arguments from command-line
while getopts :n:b:a:ld flag
do
case "${flag}" in
n) name=${OPTARG};;
b) birth_date=${OPTARG};;
a) age=${OPTARG};;
l) alive=${OPTARG};;
d) debug=d;;
\? ) usage;;
esac
done
if [[ $# -eq 0 ]] || [[ -z ${name} ]]; then
# no command-line arguments OR $name missing, show usage and exit
usage
exit
fi
# look for --help or -h CLA and return help statement
return_help "$@"
# instantiate our keyword arguments for run.py
run_py_args="--name $name "
if [[ ${birth_date} ]]; then
run_py_args+="--date $birth_date "
fi
if [[ ${age} ]]; then
run_py_args+="--age $age "
fi
if [[ ${alive} ]]; then
run_py_args+="--alive "
fi
if [[ -z ${debug} ]]; then
# no debug supplied with arg [-d]
run_py_args+="--debug False"
else
run_py_args+="--debug True"
fi
echo "$ python3 run.py $run_py_args"
python3 run.py $run_py_args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment