Getopts to flag bad options without dash

前端 未结 3 778
再見小時候
再見小時候 2021-01-22 11:35
Getopt::Long::Configure(\"no_pass_through\");
my %opts = ();
GetOptions(\\%opts,
           \'opt1=s\',
           \'opt2=s\',
           \'opt3\'
          );
         


        
3条回答
  •  日久生厌
    2021-01-22 12:12

    Getopt::Long operates only on options: arguments starting with hyphens. Without passthrough (no_pass_through is the default) it will remove them from @ARGV, leaving any non-option arguments for you to handle. If you expected no non-option arguments, you could determine that options were passed incorrectly if any arguments remain after calling GetOptions.

    die "Usage: $0 [--opt1=string] [--opt2=string] [--opt3]\n" if @ARGV;
    

    The return value of GetOptions is also useful, as it will indicate whether any unrecognized or invalid options were found.

    GetOptions(...) or die "Usage: $0 ...\n";
    

提交回复
热议问题