process CLI args with getopt#12
Conversation
|
NOTE: I'm at work (no access to a compiler and all that stuff) so I can only speak on a high level. So, please feel free to answer at your leisure.
Your list of reasons gave me pause because of the implications of using The current call syntax being: And the current (overly) simple loop construct supports this: // -Parse the command line options.
for (int i = 0; i < argc; i++) {
if (argv[i] && strlen(argv[i]) > 1) {
if (argv[i][0] == '-' && argv[i][1] == 'f') { file_string = argv[++i]; }
if (argv[i][0] == '-' && argv[i][1] == 'd') { default_string = argv[++i]; }
if (argv[i][0] == '-' && argv[i][1] == 'n') { keyvalue_output = 1; }
if (argv[i][0] != '-') { arg_string = argv[i]; }
}
}But your statement of 'allowing items in *any* order' is making me wonder if I should ask for clarification because if we were to move one line up like this: // -Parse the command line options.
for (int i = 0; i < argc; i++) {
if (argv[i] && strlen(argv[i]) > 1) {
if (argv[i][0] != '-') { arg_string = argv[i]; }
if (argv[i][0] == '-' && argv[i][1] == 'f') { file_string = argv[++i]; }
if (argv[i][0] == '-' && argv[i][1] == 'd') { default_string = argv[++i]; }
if (argv[i][0] == '-' && argv[i][1] == 'n') { keyvalue_output = 1; }
}
}The program can be called in a lot of different ways, like: Where as using And, of course my simple loop construct doesn't address the second reason ('proper error') because my simple loop construct just ignores the unknown (and this alone could probably be reason enough to move to At any rate: thank you. I see you found a few other errors (spelling and whatnot). Spelling errors in my stuff is probably enough to keep several people busy full time. :) |
I'm not following your line of reasoning here, but I've added ./test/getopt.sh to this PR now which tests that identical results are achieved no matter which way the arguments are specified in. I also updated the docs accordingly, and extended the makefile so that |
|
Please do not go to too much trouble on my account. But I was only wondering if arguments in *any* position is important. When using a POSIX/BSD If I remember right, GNU It doesn't bother me which is used but at the same time we don't need all the features of test_getopt.c #include <unistd.h>
#include <stdio.h>
/**
* This code should act as below because POSIX getopt(3) acts as:
* "
* When all options have been processed (i.e., up to the first
* non-option argument), getopt() returns -1.
* "
* EXAMPLE RUN:
* [test]cc -o test_getopt test_getopt.c
* [test]./test_getopt -n -f file.in -d default.in key=value
* filestring: file.in
* defaultstring: default.in
* argstring: key=value
* showkey: 1
* [test]./test_getopt key=value -n -f file.in -d default.in
* filestring: (null)
* defaultstring: (null)
* argstring: key=value
* showkey: 0
* [test]
*/
int main(int argc, char *argv[]) {
char *file_string = NULL; /* Used to store config file name */
char *default_string = NULL; /* Used to store default config file name. */
char *arg_string = NULL; /* Used to store the argument string. */
int keyvalue_output = 0;
int opt;
while ((opt = getopt(argc, argv, "f:d:n")) != -1) {
switch (opt) {
case 'f': file_string = optarg; break;
case 'd': default_string = optarg; break;
case 'n': keyvalue_output = 1; break;
default:
fprintf(stderr, "Usage: %s -f <configuration file> [-d <defaults file>] [-n] [key[=value]]\n", argv[0]);
}
}
if (optind < argc) arg_string = argv[optind];
printf(" filestring: %s\n defaultstring: %s\n argstring: %s\n showkey: %d\n",
file_string,
default_string,
arg_string,
keyvalue_output);
}test_customargparser.c #include <unistd.h>
#include <stdio.h>
#include <string.h>
/**
* EXAMPLE RUN:
* [test]cc -o test_customargparser test_customargparser.c
* [test]./test_customargparser -n -f file.in -d default.in key=value
* filestring: file.in
* defaultstring: default.in
* argstring: -n
* showkey: 1
* [test]./test_customargparser key=value -n -f file.in -d default.in
* filestring: file.in
* defaultstring: default.in
* argstring: key=value
* showkey: 1
* [test]
*/
int main(int argc, char *argv[]) {
char *file_string = NULL; /* Used to store config file name */
char *default_string = NULL; /* Used to store default config file name. */
char *arg_string = NULL; /* Used to store the argument string. */
int keyvalue_output = 0;
// -Parse the command line options.
for (int i = 0; i < argc; i++) {
if (argv[i] && strlen(argv[i]) > 1) {
if (argv[i][0] != '-') { arg_string = argv[i]; }
if (argv[i][0] == '-' && argv[i][1] == 'f') { file_string = argv[++i]; }
if (argv[i][0] == '-' && argv[i][1] == 'd') { default_string = argv[++i]; }
if (argv[i][0] == '-' && argv[i][1] == 'n') { keyvalue_output = 1; }
}
}
printf(" filestring: %s\n defaultstring: %s\n argstring: %s\n showkey: %d\n",
file_string,
default_string,
arg_string,
keyvalue_output);
} |
Uh oh!
There was an error while loading. Please reload this page.