mirror of
https://github.com/pygos/init.git
synced 2024-12-22 15:20:49 +01:00
Cleanup command error handling in "service"
- Add helper for checking number of arguments - Add helper for printing "please read help" message Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
parent
f97add9041
commit
b9d829bc9a
4 changed files with 36 additions and 10 deletions
|
@ -31,18 +31,15 @@ static int cmd_disable(int argc, char **argv)
|
|||
char *linkname, *ptr;
|
||||
struct stat sb;
|
||||
|
||||
if (argc < 2 || argc > 3) {
|
||||
fputs("Wrong number of arguments for `disable'.\n"
|
||||
"Try `service help disable' for more information.\n",
|
||||
stderr);
|
||||
if (check_arguments(argv[0], argc, 2, 3))
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for (ptr = argv[1]; isalnum(*ptr) || *ptr == '_'; ++ptr)
|
||||
;
|
||||
|
||||
if (*ptr != '\0') {
|
||||
fprintf(stderr, "Invalid service name '%s'\n", argv[1]);
|
||||
tell_read_help(argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,18 +31,15 @@ static int cmd_enable(int argc, char **argv)
|
|||
int ret = EXIT_FAILURE;
|
||||
struct stat sb;
|
||||
|
||||
if (argc < 2 || argc > 3) {
|
||||
fputs("Wrong number of arguments for `enable'.\n"
|
||||
"Try `service help enable' for more information.\n",
|
||||
stderr);
|
||||
if (check_arguments(argv[0], argc, 2, 3))
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for (ptr = argv[1]; isalnum(*ptr) || *ptr == '_'; ++ptr)
|
||||
;
|
||||
|
||||
if (*ptr != '\0') {
|
||||
fprintf(stderr, "Invalid service name '%s'\n", argv[1]);
|
||||
tell_read_help(argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,23 @@ void usage(int status)
|
|||
exit(status);
|
||||
}
|
||||
|
||||
void tell_read_help(const char *cmd)
|
||||
{
|
||||
fprintf(stderr, "Try `%s help %s' for more information.\n",
|
||||
__progname, cmd);
|
||||
}
|
||||
|
||||
int check_arguments(const char *cmd, int argc, int minc, int maxc)
|
||||
{
|
||||
if (argc >= minc && argc <= maxc)
|
||||
return 0;
|
||||
|
||||
fprintf(stderr, "Too %s arguments for `%s'\n",
|
||||
argc > maxc ? "many" : "few", cmd);
|
||||
tell_read_help(cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
command_t *cmd;
|
||||
|
|
|
@ -60,6 +60,21 @@ extern command_t *commands;
|
|||
*/
|
||||
void usage(int status) NORETURN;
|
||||
|
||||
/*
|
||||
Write a message to stderr that advises the user how to consult the
|
||||
help text for a specific command.
|
||||
*/
|
||||
void tell_read_help(const char *cmd);
|
||||
|
||||
/*
|
||||
Check if the argument count is within specified bounds (minc and maxc
|
||||
inclusive). If it is, return 0.
|
||||
|
||||
If it isn't, complain about a wrong number of arguments for a
|
||||
command (cmd), tell the user to consult the help text and return -1.
|
||||
*/
|
||||
int check_arguments(const char *cmd, int argc, int minc, int maxc);
|
||||
|
||||
/*
|
||||
To implement a new command, add a global, static instance of a
|
||||
command_t (or derived) structure to a C file and pass it to this
|
||||
|
|
Loading…
Reference in a new issue