0

I have to restrict when the user gives "--" as an option. For example:

./test --

should thrown an error. For the sake of parsing I am using getopt

How to achieve this using getopt?

alk
  • 69,737
  • 10
  • 105
  • 255
samairtimer
  • 826
  • 2
  • 12
  • 28
  • Can you please try to [add more text](http://stackoverflow.com/posts/23738016/edit)? I find it very hard to understand your question. – unwind May 19 '14 at 12:42
  • The [`getopt`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html) function doesn't support double-dashes for options at all. Or are you meaning the option terminator sequence? Please *edit your question* to clarify. – Some programmer dude May 19 '14 at 12:42
  • I will try hinting about getopt_long, but it might not be what the questioner wanted to know about. – perh May 19 '14 at 12:47
  • @JoachimPileborg: Yes it does. `--` marks the end of option arguments, even if the next subsequent argument looks like it would be an option. – R.. GitHub STOP HELPING ICE May 19 '14 at 12:53

1 Answers1

3

getopt is intended for implementing programs which conform to the POSIX Utility Syntax Guidelines where -- has a special meaning of causing all subsequent arguments to be treated as non-options even if they have the form of options (e.g. rm -- -f to remove a file named -f). There's no explicit way to suppress this behavior. You could try checking whether argv[optind-1] is "--" after getopt finishes, but this would give false positives in cases where the "--" was the argument to an option that takes an argument (something like -f -- where -f needs an argument) which you might need to work around.

If you really want argument handling that does not conform to the Utility Syntax Guidelines you might be better off rolling your own from scratch.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711