I've been using cargs for a small project where I want to be cross-platform and still offer a reasonably full-featured command-line interface, and it's been working really well.
However, I seem to have a problem. If I compile this test program (foo.c):
#include <stdio.h>
#include "cargs.h"
static struct cag_option options[] = {
{ .identifier = 'h',
.access_letters = "h",
.access_name = "help",
.description = "show this help and exit" },
};
int main(int argc, char *argv[]) {
cag_option_context context;
cag_option_prepare(&context, options, CAG_ARRAY_SIZE(options), argc, argv);
while (cag_option_fetch(&context)) {
char identifier = cag_option_get(&context);
switch (identifier) {
case 'h':
printf("Help...\n");
break;
default:
printf("?\n");
break;
}
}
for (int i = context.index; i < context.argc; ++i) {
printf("arg %d: %s\n", i, context.argv[i]);
}
}
the non-option arguments seem to be permuted inconsistently. Normally the order is preserved:
$ gcc -Wall foo.c cargs.c
$ ./a.out foo bar
arg 1: foo
arg 2: bar
$ ./a.out -h foo bar
Help...
arg 2: foo
arg 3: bar
$ ./a.out foo -h bar
Help...
arg 2: foo
arg 3: bar
$ ./a.out foo bar -h
Help...
arg 2: bar
arg 3: foo
arg 2 is now "bar", whereas it used to be "foo".
As my program is trying to accept two non-option arguments which are input and output files, this breaks things. Would it be possible to have the order of non-option arguments preserved in all cases please?
If you need any more information please let me know.
Thanks.