Skip to content
Snippets Groups Projects
Commit 7bb4c623 authored by ale's avatar ale
Browse files

improve command-line option handling

parent 928de6dc
No related branches found
No related tags found
No related merge requests found
......@@ -28,9 +28,39 @@ char *lock_dir = "/var/tmp";
int usage() {
fprintf(stderr,
"%s v%s - a cron job wrapper\n"
"Usage: %s [<OPTIONS>] <COMMAND> [<ARGS>...]\n"
"%s v%s - a cron job wrapper\n",
PACKAGE, PACKAGE, PACKAGE_VERSION
"\n"
"Known options:\n"
"\n"
" --help Show this help message\n"
" --splay SECONDS Wait a random amount of time between 0 and SECONDS before\n"
" actually starting the command.\n"
" --timeout SECONDS\n"
" If the command runs for more than the specified amount of\n"
" time, terminate it. By default commands can run forever.\n"
"\n"
"Options controlling the command output:\n"
"\n"
" --quiet Only show output from the command if it fails, suppress\n"
" all output if the exit status is 0.\n"
" --syslog Log command output to syslog, instead of stderr.\n"
"\n"
"Options controlling locking behavior:\n"
"\n"
" -n, --name NAME Set the job name. By default, the basename of COMMAND will\n"
" be used.\n"
" --no-lock Disable locking, allow running multiple instances at once.\n"
" --wait Wait for the lock to be released. If another instance of\n"
" this command is running, wait until it terminates (or\n"
" until the lock timeout expires).\n"
" --lock-timeout SECONDS\n"
" Wait at most the specified amount of time for the instance\n"
" lock to be released, failing with an error if after SECONDS\n"
" the lock could not be obtained. If set to 0 (the default),\n"
" wait forever. Specifying this option implies --wait.\n"
"\n",
PACKAGE, PACKAGE_VERSION, PACKAGE
);
return 2;
}
......@@ -298,6 +328,22 @@ int main(int argc, char **argv) {
}
}
/* Check for option consistency. */
if (!locking) {
if (lock_wait) {
fprintf(stderr, "Error: --wait and --no-lock should not be specified together\n");
exit(2);
}
if (lock_timeout > 0) {
fprintf(stderr, "Error: --no-lock and --lock-timeout should not be specified together\n");
exit(2);
}
} else {
if (lock_timeout > 0) {
lock_wait = 1;
}
}
if (optind >= argc) {
fprintf(stderr, "Error: no command specified\n");
exit(2);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment