我正在编写一个程序,并且我想接受命令行参数,但是当我传递任何参数时,它就像总是调用 start 一样。我的代码如下。任何帮助表示赞赏。
//compile to iservices
// Ilkotech Services - C Core
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s start|shutdown|reload\n",argv[0]);
}
else {
if (strcmp(argv[1],"start") != 0) {
services_start();
}
else if (strcmp(argv[1],"shutdown") != 0) {
services_shutdown();
}
else if (strcmp(argv[1],"reload") != 0) {
services_reload();
}
else {
printf("%s is not a valid argument\n",argv[1]);
printf("Usage: %s start|shutdown|reload\n",argv[0]);
exit(1);
}
}
return 0;
}
int services_start() {
printf("Started.\n");
return 0;
}
int services_shutdown() {
printf("Shutting down!\n");
return 0;
}
int services_reload() {
printf("Reloading services configuration.\n");
return 0;
}