0

I'm parsing command line options using getopt_long based on the example from the man page. That example does something a bit sneaky, it includes two flag-setting options in long_options but does not list those in the short form parameter string in getopt_long.

Poking about, this answer clarifies what happens in this case, if they enter the short-form it does not set the flag. And leads to my question: If you have a flag-setting switch, and you want to have both long and short forms for that switch on the command line...

  1. Did I miss an option in the short-form string to set flags, sort of like how : works for indicating a parameter follows? Like "v?1" or something?

  2. Failing that, is it best-practices to not do it in the long-form as well, and just return 'v'? Or would "the average code" use the flag-set and have separate code in the switch?

I'd like my code to be "as standard as possible", whatever that means, which is my I'm curious about (2).

Maury Markowitz
  • 9,082
  • 11
  • 46
  • 98
  • 1
    If you want to have a single-character option and a long option that do the same thing, you need an entry for it in both the single-character option string argument, and the long option array argument. The long option would usually return the same character as the short option unless you need to tell them apart for some reason. – Shawn Nov 26 '20 at 20:02
  • Ok so that seems like option (2). – Maury Markowitz Nov 26 '20 at 20:18

0 Answers0