docopt / docopt.c

C-code generator for docopt language.
MIT License
318 stars 46 forks source link

Support for positional and ... arguments #40

Closed jkcko closed 6 years ago

jkcko commented 6 years ago

I needed handling of positional arguments as well as one or more arguments so I made some changes which you can find here.

https://github.com/jkcko/docopt.c/commit/23a68dd845985bf1cdeed1cc94dbae9e48a19e49

The Command type now includes a pointer to an Argument array which lists the positional arguments for the command.

Additional Argument arrays with prefix docopt_ are generated for each command.

parseargcmd will now assign value in the matching argument of elements->arguments and the positional docopt arrays. argument->value = argument value argument->count = number of arguments on the command line including this one (for counting ...) argument->array = points to argv for this argument (for ...)

Usage for positional arguments would be something like.

   cfg command <arg1> <arg2> <arg3>

   if (arg.arg1 != NULL) {
       printf("arg1 %s\n", arg.arg1);
   }
   if (arg.arg2 != NULL) {
       printf("arg2 %s\n", arg.arg2);
   }
   if (arg.arg3 != NULL) {
       printf("arg3 %s\n", arg.arg3);
   }

Usage for ... would be something like.

   cmd command <list-item>...

    if (docopt_command[0].count > 1) {
        /* more than one */
        char **array = docopt_command[0].array;
        for (i=0; i < count; i++) {
            list_item[i] = array[i];
        }
    }
    else {
        /* one */
        char *strp = args->command;
    }

Caveat: [options] combined with positional arguments is not supported.

jkcko commented 6 years ago

I've made a number of improvements to my fork and it now works with [options] either before or after the required positional arguments. The naval fate example now works.

jkcko commented 6 years ago

This is a duplicate of #32.