0

Does anyone know how to specify mutually exclusive options with argp in C? Is there any trick one could use?

EDIT

What are mutually exclusive options?

Say you have some command line utility. You type utility --help and the output looks like this

utility [-a|-b] 

Options -a and -b are mutually exclusive because one cannot specify them together, i.e. specifying -a excludes the use of -b. The same holds for -b. If it is used then one cannot specify -a. In other words only the following is possible:

utility -a or utility -b.

From the argp documentation it doesn't seem that it is possible to specify this kind of option. So the question is what tricks do people use to specify this kind of option? I'm sure the need to this arose more than once in someone's experience.

flashburn
  • 4,180
  • 7
  • 54
  • 109
  • Do you mean `argc` and `argv`? – lost_in_the_source Jul 14 '16 at 22:39
  • Do you mean `argp` which is `glibc` specific? An example program, such as [this](http://crasseux.com/books/ctutorial/argp-example.html)? – user3078414 Jul 14 '16 at 22:46
  • @EdHeal https://www.gnu.org/software/libc/manual/html_node/Argp.html – flashburn Jul 14 '16 at 23:32
  • @stackptr I meant argp, here is the doc, https://www.gnu.org/software/libc/manual/html_node/Argp.html – flashburn Jul 14 '16 at 23:32
  • @user3078414 yes, that's what I mean. Here is the manual, https://www.gnu.org/software/libc/manual/html_node/Argp.html – flashburn Jul 14 '16 at 23:33
  • @flashburn, please make sure to edit, reword, clarify your question more precisely and include some of your code to show own research effort. Your question is somewhat loosely asked indeed, but no one would really benefit from having it discarded and deleted. – user3078414 Jul 15 '16 at 08:27
  • @user3078414 I'm not really sure how else to reword it. I think people who create command line interfaces on regular basis know what the question is about. My own research effort boiled down to simply reading the manual. No mutual exclusion was mentioned there, which made me assume there is some trick. I would appreciate any input on how to reword the question. – flashburn Jul 15 '16 at 17:55
  • Please, use a little effort to at least show that you know what you're asking. If you have some code that you've tried to make work, please post it. It is important that your question reaches those who can answer. Can you be more precise about mutually exclusive options in your question? – user3078414 Jul 15 '16 at 18:06

2 Answers2

1

A program that has two mutually exclusive options, -a and -b, usually do not stop the user from specifying both on the command line.

Instead, the program would parse -a and then set some internal variable, maybe opt_ab = 1. When later getting to parsing -b, the variable would be set opt_ab = 0. It would be the last option on the command line that took effect.

This is how it's done in the vast majority of command line tools. You have, for example:

$ ls -1 -C

vs.

$ ls -C -1
Kusalananda
  • 14,885
  • 3
  • 41
  • 52
0

I don't know if argp has the feature you want, but are you sure you want it? You're going to have to describe the relationship, and provide a message, no?

Traditionally, getopt only parsed arguments. You couldn't specify mandatory, optional, or which go with what. The getopt loop went over argv, and dealt with each option (and argument) in turn. Once that was done, you examine the results and deal with any conflicts.

switch(ch) {
  case 'a': foo = true; break;
  case 'b': bar = true; break;
}
if( foo && bar ) { 
  errx(EXIT_FAILURE, "foo and bar cannot be used together");
}

That looks simple and understandable. I wouldn't want to try to express it in the option-string grammar.

James K. Lowden
  • 7,574
  • 1
  • 16
  • 31