1

I am trying to use getopt_long for my code. The optional_argument and required_argument options are working as desired but the no_Argument option is not working properly. This is how I am coding it.

struct option long_option[] = 
{
  {"auto",required_argument,0,'a'},
  {"help",no_argument,0,'h'},
  {"list",no_argument,0,'l'},
  {0,0,0,0}
};

Now if I compile and try:

./a.out --help 

--> shows all the help contents

./a.out --help 1234 

--> still shows all the help contents. It should prompt an error that argument not required??

alk
  • 69,737
  • 10
  • 105
  • 255
lokesharo
  • 305
  • 2
  • 11

1 Answers1

0

No, it is not considered an error. getopt_long interprets 1234 as a program argument.

It's clearer in this example:

rm -r -f dir1/ dir2/

Here -r and -f are options, and both dir1/ and dir2/ are program arguments. Note that if you execute rm file1.txt -f, it will try to delete a file named "-f".

MBlanc
  • 1,773
  • 15
  • 31
  • "*... it will try to delete a file named -f.*". This actully depends on the implementation of `rm`. In fact it doesn't on recent Linux. – alk Mar 02 '14 at 15:30
  • OK i get it but then what is the use of the no_argument option in the structure? For this thing to work will I have to use if/else loop in the "case 'h'" inside the switch loop to check if there is any program argument after --help/--list? – lokesharo Mar 03 '14 at 05:37