Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix option parsing, add missing help #45

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ is not met.
### Available Command Line Options

```text
Usage: ./zfs-replicate.sh [options] [config]
Usage: ./zfs-replicate.sh [config] [options]

POSIX shell script to automate ZFS Replication

Expand Down
23 changes: 16 additions & 7 deletions zfs-replicate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ showStatus() {

## show usage and exit
showHelp() {
printf "Usage: %s [options] [config]\n\n" "${SCRIPT}"
printf "Usage: %s [config] [options]\n\n" "${SCRIPT}"
printf "POSIX shell script to automate ZFS Replication\n\n"
printf "Options:\n"
printf " -c, --config <configFile> configuration file\n"
Expand All @@ -459,9 +459,15 @@ showHelp() {
loadConfig() {
configFile=""
status=0
help=0
## sub macros for logging
TAG="$(subTags "$TAG")"
LOG_FILE="$(subTags "$LOG_FILE")"
## check for config file as first argument for backwards compatibility
if [ $# -gt 0 ] && [ -f "$1" ]; then
configFile="$1"
shift
fi
## process command-line options
while [ $# -gt 0 ]; do
if [ "$1" = "-c" ] || [ "$1" = "--config" ]; then
Expand All @@ -470,20 +476,23 @@ loadConfig() {
shift
continue
fi
if [ "$1" = "-s" ] || [ "$1" = "--status" ]; then
status=1
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
help=1
shift
continue
fi
## unknown option - check for config file for backwards compatibility
if [ -z "$configFile" ] && [ -f "$1" ]; then
configFile="$1"
if [ "$1" = "-s" ] || [ "$1" = "--status" ]; then
status=1
shift
continue
fi
## nothing left, error out
## unknown option
writeLog "ERROR: illegal option ${1}" && exit 1
done
## someone ask for help?
if [ "$help" -eq 1 ]; then
showHelp
fi
## attempt to load configuration
if [ -f "$configFile" ]; then
# shellcheck disable=SC1090
Expand Down