8

I've just been asked for the first time in a code review to check the return code from a call to the GetOptions() function of the Getopt::Long Perl module.

I cannot remember ever seeing such a test for the GetOptions() function.

So is there a specific reason why people don't generally check the return code of this function?

DVK
  • 126,886
  • 32
  • 213
  • 327
Rob Wells
  • 36,220
  • 13
  • 81
  • 146

1 Answers1

11

One reason that people don't check the return value of the GetOptions function is that they want to process unspecified options without using Getopt::Long (by parsing @ARGV directly after GetOptions is called). Or, maybe they just want to ignore unspecified options. Or, maybe they are unaware that the GetOptions function can fail.

I always check the return value because I like to catch typos on the command line. A standard way to check makes use of the Pod::Usage Core module (see the POD for example code). See also: The Dynamic Duo --or-- Holy Getopt::Long, Pod::UsageMan!

toolic
  • 57,801
  • 17
  • 75
  • 117
  • 2
    I usually check the return value of `GetOptions` if I want to make the programm `die` on errors when parsing command line arguments. Like `die unless GetOptions(...)`. If not `pass_trough` is still an option to catch the rest of the arguments in `@ARGV`. – matthias krull Jul 17 '12 at 16:23
  • 1
    Cheers @toolic for the very useful answer. I didn't think of misspelled options. And big thanks for the link to the article providing more info abut why I should add more pod to my Perl! – Rob Wells Jul 17 '12 at 17:29