Is this a good way of implementing an optional command line argument?
I'm very new to C so this might be an obvious problem to solve, but this is my way of checking for an optional command line argument, and changing mode based on that. Is this a safe solution?
int mode = 0; // default mode 0: relative pathing; 1 is exact pathing
int force = 0; // default force 0: dont overwrite; 1 to force writing
if (argv[1] == NULL || argv[2] == NULL) {
printf("mv: expected 2 arguments, \"mv [source] [dest]\"");
return 1;
}
if (strcmp(argv[1], "-f") == 0) {
char ch;
printf("force overwrite? (y/N): ");
fflush(stdout);
ch = getchar();
if (ch == 'y' || ch == 'Y') {
force = 1;
} else {
return 1;
}
}
if (1) {
printf("mode: %i\n", mode);
printf("force: %i\n", force);
printf("argc: %i\n", argc);
for (size_t i = 0; argv[i]; i++) {
printf("%s\n", argv[i]);
}
}