11

Let's say I provide the following long option arguments structure:

static const struct option long_opts[] = {
    { "version",   no_argument,       NULL, 'v' },
    { "help",      no_argument,       NULL, 'h' },
    { NULL, 0, NULL, 0 } 
};

How can I specify an additional option, named '--myoption', but without the short form? So I would be able to call only:

./binary --myoption

I need this because I ran out of letters.

kaspersky
  • 3,959
  • 4
  • 33
  • 50
  • 2
    Specify a single character such as `'\0'` that the user cannot readily type on the command line? There's also `getopt_long_only()` — see the [manual](http://www.gnu.org/software/libc/manual/html_node/Getopt.html#Getopt). – Jonathan Leffler Mar 17 '14 at 20:43
  • But how I provide then the optstring? When calling getopt_long shouldn't I provide a pattern? – kaspersky Mar 17 '14 at 20:46
  • Reading the manual a little bit, the call interface is `int getopt_long (int argc, char *const *argv, const char *shortopts, const struct option *longopts, int *indexptr)`. The string `shortopts` has the same structure as for plain `getopt()`; it you pass an empty string, there will be no short options. – Jonathan Leffler Mar 17 '14 at 20:51

1 Answers1

17

If you don't put that option into shortopts then no short option for that parameter will be used. E.g.:

#define MYOPT 1000

static struct option long_options[] = {
    {"myopt", no_argument, 0, MYOPT },
}

[...]

c = getopt_long(argc, argv, "", long_options, &option_index);
switch (c) {
case MYOPT:
    /* Do stuff. */
    break;
}
ldx
  • 3,984
  • 23
  • 28