简体   繁体   中英

How to use C++ getopt_long() if I have more than 26 options?

This is a rather hypothetical question, but let's say I have 3 long parameters that begin with the same letter.

--parse or -p
--prune or -r
--pivot or -i

Eventually I'll start running out of single letters that make sense, more over, it's hard assign something meaningful in GNU getopt_long() configuration.

{"parase", no_argument,       0, 'p'},
{"prune",  no_argument,       0, 'r'},
{"pivot",  required_argument, 0, 'i'}

What is the best practice in these situations?

You don't have to use printable characters for the val member. Moreover, it's int , not char . It should just assist you with identifying the option. (meaning, you don't have to have corresponding shorty).

您可以选择大写和小写,为您提供52个短选项,但是许多接受长选项的程序甚至不会为某些选项分配短选项,这迫使用户仅使用长选项,这没问题。

The digits 0-9 can be used to. Imagine a data compression program which would either accept -compression=[0-9] , or just -[0-9] (I don't know if this works with getopt, though).

Also, uppercase can be used, too.

I understand that's a hypothetical question, but with so many options, the program probably does too much or needs a configuration file.

I would advise you to take a look at boost program options . It is a general-purpose library for getting command line arguments. Very easy and pleasant to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM