How can I allow undefined options when parsing args with Getopt

前端 未结 3 1266
故里飘歌
故里飘歌 2020-12-29 06:26

If I have a command line like:

my_script.pl -foo -WHATEVER

My script knows about --foo, and I want Getopt to set variable

3条回答
  •  余生分开走
    2020-12-29 06:44

    I think the answer here, sadly though, is "no, there isn't a way to do it exactly like you ask, using Getopt::Long, without parsing @ARGV on your own." Ether has a decent workaround, though. It's a feature as far as most people are concerned that any option-like argument is captured as an error. Normally, you can do

    GetOptions('foo' => \$foo) 
        or die "Whups, got options we don't recognize!";
    

    to capture/prevent odd options from being passed, and then you can correct the user on usage. Alternatively, you can simply pass through and ignore them.

提交回复
热议问题