Skip to content

Commit

Permalink
Arguments parsing in scripts - Fix and improve compatibility (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
leondavi authored Jun 14, 2023
1 parent 9a5fbdf commit ef124fd
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 46 deletions.
139 changes: 97 additions & 42 deletions NerlnetBuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ INPUT_DATA_DIR="inputDataDir"
SHORT_OPTIONS_LIST=p:,j:,c:,h
LONG_OPTIONS_LIST=pull:,jobs:,clean:,help

# arguments parsing
# Thanks to https://github.com/matejak/argbash
Branch="master"
JobsNum=4

Expand All @@ -14,7 +16,7 @@ help()
echo "Usage:"
echo "--p or --pull Warning! this uses checkout -f! and branch name checkout to branch $Branch and pull the latest"
echo "--j or --jobs number of jobs to cmake build"
echo "--c or --clean with any value remove build directory"
echo "--c or --clean remove build directory"
exit 2
}

Expand All @@ -24,50 +26,103 @@ gitOperations()
sleep 5
echo "$NERLNET_PREFIX Interrupt is possible in the next 10 seconds"
sleep 10
git checkout -f $1
git pull origin $1
git checkout -f $Branch
git pull origin $Branch
git submodule update --init --recursive
}

OPTS=$(getopt -a -n jupyterEnv --options $SHORT_OPTIONS_LIST --longoptions $LONG_OPTIONS_LIST -- "$@")
#echo $OPTS

eval set -- "$OPTS"

while :
do
case "$1" in
-p | --pull )
Branch="$2"
gitOperations $Branch
shift 2
;;
-j | --jobs )
JobsNum="$2"
shift 2
;;
-c | --clean )
echo "Are you sure that you want to remove build directory?"
sleep 1
echo "Intterupt this process is possible with ctrl+c"
echo "Remove build directory in 10 seconds"
sleep 10
rm -rf build
shift 2
;;
-h | --help)
help
;;
--)
shift;
break
;;
*)
echo "Unexpected option: $1"
help
;;
esac
done
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}


begins_with_short_option()
{
local first_option all_short_options='hjp'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}

# THE DEFAULTS INITIALIZATION - OPTIONALS
clean_build_directory()
{
echo "Are you sure that you want to remove build directory?"
sleep 1
echo "Intterupt this process is possible with ctrl+c"
echo "Remove build directory in 10 seconds"
sleep 10
rm -rf build
}

print_help()
{
printf 'Usage: %s [-h|--help] [-c|--clean] [-j|--jobs <arg>] [-p|--pull <arg>]\n' "$0"
printf '\t%s\n' "-j, --jobs: number of jobs (default: '4')"
printf '\t%s\n' "-p, --pull: pull from branch (default: '4')"
}


parse_commandline()
{
while test $# -gt 0
do
_key="$1"
case "$_key" in
-h|--help)
help
exit 0
;;
-h*)
help
exit 0
;;
-c|--clean)
clean_build_directory
exit 0
;;
-c*)
clean_build_directory
exit 0
;;
-j|--jobs)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
JobsNum="$2"
shift
;;
--jobs=*)
JobsNum="${_key##--jobs=}"
;;
-j*)
JobsNum="${_key##-j}"
;;
-p|--pull)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
Branch="$2"
gitOperations
shift
;;
--pull=*)
Branch="${_key##--pull=}"
gitOperations
;;
-p*)
Branch="${_key##-p}"
gitOperations
;;
*)
_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
;;
esac
shift
done
}

parse_commandline "$@"
# end of args parsing

NERLNET_BUILD_PREFIX="[Nerlnet Build] "

Expand Down
11 changes: 7 additions & 4 deletions NerlnetInstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ NERLNET_LOG_DIR="/usr/local/lib/nerlnet-lib/log"
REBAR3_FILE=src_erl/rebar3/rebar3
REBAR3_SYMLINK=/usr/local/bin/rebar3

# args defaults
InstallAll=false
NumJobs=4

if [[ "$RUNNING_IN_DOCKER" = true ]]; then
# Not running inside a docker container
LOGGED_IN_USER=$(logname)
Expand All @@ -19,6 +15,13 @@ else
LOGGED_IN_USER="$(whoami)" # Probably root
fi

# arguments parsing
# Implemented by https://github.com/matejak/argbash

# args defaults
InstallAll=false
NumJobs=4

help()
{
echo "-------------------------------------" && echo "Nerlnet Install" && echo "-------------------------------------"
Expand Down

0 comments on commit ef124fd

Please sign in to comment.