Skip to content

Commit

Permalink
some cleaning in how CLI options are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
phyver committed May 8, 2024
1 parent 2e5c190 commit 504e6d8
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 107 deletions.
6 changes: 3 additions & 3 deletions i18n/start-help/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ options:
On non GNU systems, set the variable LC_MESSAGES
to a valid locale.

--simple-savefiles
-S simple
the savefile is obtained by adding a "-save" suffix,
each savefile overwrites the previous one
--index-savefiles
-S index
savefiles are numbered so that all savefiles are kept
--overwrite-savefiles
-S overwrite
the savefile overwrite the initial GameShell file
(you can use option -R to start a new game from the start)

Expand Down
6 changes: 3 additions & 3 deletions i18n/start-help/fr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ options :
Pour les systèmes non GNU, initialisez la variable LC_MESSAGES
avec une locale valide.

--simple-savefiles
-S simple
le fichier de sauvegarde est obtenu en ajoutant un suffix "-save",
chaque fichier de sauvegarde remplace le précédent
--index-savefiles
-S index
les fichiers de sauvegarde sont numérotés pour que toutes les
sauvegardes soient conservées
--overwrite-savefiles
-S overwrite
le fichier de sauvegarde remplace l'instance initial de GameShell
(on peut utiliser l'option -R pour lancer une partie à partir du début)

Expand Down
6 changes: 3 additions & 3 deletions i18n/start-help/it.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ options:
Su sistemi non GNU, imposta la variabile LC_MESSAGES
per un "locale" valido.

--simple-savefiles
-S simple
il file di salvataggio è generato con un suffiso "-save",
ogni salvataggio sovrascrive il precedente
--index-savefiles
-S index
i salvataggi sono numerati così sono tutti conservati
--overwrite-savefiles
-S overwrite
il salvataggio sovrascrive il file iniziale di GameShell
(puoi utilizzare l'opzione -R per avviare un nuovo gioco dall'inizio)

Expand Down
110 changes: 57 additions & 53 deletions lib/header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,61 +34,65 @@ GSH_EXEC_DIR=$(cd "$GSH_EXEC_DIR"; pwd -P)
# just in case
GSH_EXEC_DIR=${GSH_EXEC_DIR:-.}

for arg in "$@"
while getopts "VUXKFh" opt
do
if [ "$arg" = "-V" ]
then
echo "Gameshell $GSH_VERSION"
if [ -n "$GSH_LAST_CHECKED_MISSION" ]
then
echo "saved game: [mission $GSH_LAST_CHECKED_MISSION] OK"
fi
exit 0
elif [ "$arg" = "-U" ]
then
TARGET="$GSH_EXEC_DIR/gameshell.sh"
TMPFILE="$GSH_EXEC_DIR/gameshell.sh$$"
if command -v wget >/dev/null
then
if wget -O "$TMPFILE" https://github.com/phyver/GameShell/releases/download/latest/gameshell.sh
then
mv "$TMPFILE" "$TARGET"
chmod +x "$TARGET"
echo "Latest version of GameShell downloaded to $GSH_EXEC_DIR/gameshell.sh"
exit 0
else
rm -f "$TMPFILE"
echo "Error: couldn't download or save the latest version of GameShell." >&2
exit 1
fi
elif command -v curl >/dev/null
then
if curl -fo "$TMPFILE" https://github.com/phyver/GameShell/releases/download/latest/gameshell.sh
then
mv "$TMPFILE" "$TARGET"
chmod +x "$TARGET"
echo "Latest version of GameShell downloaded to $GSH_EXEC_DIR/gameshell.sh"
case "$opt" in
V)
echo "Gameshell $GSH_VERSION"
if [ -n "$GSH_LAST_CHECKED_MISSION" ]
then
echo "saved game: [mission $GSH_LAST_CHECKED_MISSION] OK"
fi
exit 0
else
rm -f "$TMPFILE"
echo "Error: couldn't download or save the latest version of GameShell." >&2
exit 1
fi
fi
elif [ "$arg" = "-X" ]
then
GSH_EXTRACT="true"
elif [ "$arg" = "-K" ]
then
KEEP_DIR="true"
elif [ "$arg" = "-F" ]
then
GSH_FORCE="true"
elif [ "$arg" = "-h" ]
then
# used to avoid checking for more recent files
GSH_HELP="true"
fi
;;
U)
TARGET="$GSH_EXEC_DIR/gameshell.sh"
TMPFILE="$GSH_EXEC_DIR/gameshell.sh$$"
if command -v wget >/dev/null
then
if wget -O "$TMPFILE" https://github.com/phyver/GameShell/releases/download/latest/gameshell.sh
then
mv "$TMPFILE" "$TARGET"
chmod +x "$TARGET"
echo "Latest version of GameShell downloaded to $GSH_EXEC_DIR/gameshell.sh"
exit 0
else
rm -f "$TMPFILE"
echo "Error: couldn't download or save the latest version of GameShell." >&2
exit 1
fi
elif command -v curl >/dev/null
then
if curl -fo "$TMPFILE" https://github.com/phyver/GameShell/releases/download/latest/gameshell.sh
then
mv "$TMPFILE" "$TARGET"
chmod +x "$TARGET"
echo "Latest version of GameShell downloaded to $GSH_EXEC_DIR/gameshell.sh"
exit 0
else
rm -f "$TMPFILE"
echo "Error: couldn't download or save the latest version of GameShell." >&2
exit 1
fi
fi
;;
X)
GSH_EXTRACT="true"
;;
K)
KEEP_DIR="true"
;;
F)
GSH_FORCE="true"
;;
h)
# used to avoid checking for more recent files
GSH_HELP="true"
;;
*)
# ignore other options, they will be passed to start.sh
;;
esac
done


Expand Down
31 changes: 11 additions & 20 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,19 @@ GSH_MODE="ANONYMOUS"
# if GSH_NO_GETTEXT is non-empty, gettext won't be used anywhere, the only language will thus be English
# export GSH_NO_GETTEXT=1 # DO NOT CHANGE OR REMOVE THIS LINE, it is used by utils/archive.sh
RESET=""
# hack to parse long options --index-savefiles --overwrite-savefiles --simple-savefiles
# cf https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options
_long_option=0
while getopts ":hnPdDACRXUVqGL:KBZc:F-:" opt
while getopts "hnPdDACRXUVqGL:KBZc:FS:" opt
do
if [ "$opt" = "-" ]
then
opt="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#$opt}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
_long_option=1
fi

case $opt in
index-savefiles)
GSH_SAVEFILE_MODE=index
;;
simple-savefiles)
GSH_SAVEFILE_MODE=simple
;;
overwrite-savefiles)
GSH_SAVEFILE_MODE=overwrite
S)
case "$OPTARG" in
"index" | "simple" | "overwrite")
GSH_SAVEFILE_MODE=$OPTARG
;;
*)
echo "$(gettext "Error: save mode can only be 'index', 'simple' or 'overwrite'")" >&2
exit 1
;;
esac
;;
h)
display_help
Expand Down
41 changes: 16 additions & 25 deletions utils/archive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ create a GameShell standalone archive
options:
-h this message
--password=... choose password for admin commands
-p ... choose password for admin commands
-P use the "passport mode" by default when running GameShell
-A use the "anonymous mode" by default when running GameShell
-L LANGS only keep the given languages (ex: -L 'en*,fr')
Expand All @@ -20,9 +20,9 @@ options:
-N ... name of the archive / top directory (default: "gameshell")
--simple-savefiles
--index-savefiles
--overwrite-savefiles
-S simple
-S index
-S overwrite
choose default savefile mode
-a keep 'auto.sh' scripts for missions that have one
Expand Down Expand Up @@ -62,38 +62,29 @@ KEEP_PO=0 # this is set to 1 if we generate .mo files. Setting it to 1 here
LANGUAGES=""
VERBOSE=

# hack to parse long option --password
# cf https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options
_long_option=0
while getopts ":hp:N:atPzL:Ev-:" opt
while getopts "hp:N:atPzL:EvS:p:" opt
do
if [ "$opt" = "-" ]
then
opt="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#"$opt"}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
_long_option=1
fi

case $opt in
h)
display_help
exit 0;
;;
password)
p)
ADMIN_PASSWD=$OPTARG
;;
N)
NAME=$OPTARG
;;
index-savefiles)
GSH_SAVEFILE_MODE=index
;;
simple-savefiles)
GSH_SAVEFILE_MODE=simple
;;
overwrite-savefiles)
GSH_SAVEFILE_MODE=overwrite
S)
case "$OPTARG" in
"index" | "simple" | "overwrite")
GSH_SAVEFILE_MODE=$OPTARG
;;
*)
echo "Error: save mode can only be 'index', 'simple' or 'overwrite'" >&2
exit 1
;;
esac
;;
a)
KEEP_AUTO=1
Expand Down

0 comments on commit 504e6d8

Please sign in to comment.