【问题标题】:Unknown options in perl Getopt::Longperl Getopt::Long 中的未知选项
【发布时间】:2014-03-26 05:42:20
【问题描述】:

如何使用 Getopt::Long 识别未知选项?

我尝试了'',但它没有按预期工作..考虑:

use Modern::Perl;
use Getopt::Long;

my $help='';
GetOptions ('help' => \$help,'<>' => \&usage);
usage() if $help;

usage() if @ARGV != 1;

my $fn=pop;

say "FileName: $fn";

sub usage {
    say "Unknown option: @_" if ( @_ );
    say "Usage: $0 <filename>";
    say "       $0 --help";
    say "";
    exit
}

我想打印Unknown option 仅当有一个无法识别的选项(在这种情况下,除--help 之外的任何其他选项)。但现在它认为文件名是一个无法识别的选项..

【问题讨论】:

  • 您是说要打印Unknown option 以获取--foo-foo 之类的内容,但将foo 视为文件名?

标签: perl getopt-long


【解决方案1】:

如果GetOptions 失败,请调用您的usage 函数。 Getopt::Long 将为您打印Unknown option(到 STDERR):

use Modern::Perl;
use Getopt::Long;

my $help='';
GetOptions ('help' => \$help) or usage();
usage() if $help;

usage() if @ARGV != 1;

my $fn=pop;

say "FileName: $fn";

sub usage {
    say "Usage: $0 <filename>";
    say "       $0 --help";
    say "";
    exit
}

【讨论】:

    【解决方案2】:

    开始使用 pod 文档作为核心模块 Getopt::LongPod::Usage 可以很好地协同工作。无需创建辅助方法即可获得您想要的行为:

    这是一个示例脚本:

    #!/usr/bin/perl
    
    use File::Basename qw(basename);
    use Getopt::Long qw(GetOptions);
    use Pod::Usage qw(pod2usage);
    use Readonly;
    use version;
    
    use strict;
    use warnings;
    
    Readonly my $VERSION => qv('0.0.1');
    Readonly my $EXE => basename($0);
    
    GetOptions(
        'version'   => \my $version,
        'usage'     => \my $usage,
        'help|?'    => \my $help,
        'man'       => \my $man,
    ) or pod2usage(-verbose => 0);
    pod2usage(-verbose => 0) if $usage;
    pod2usage(-verbose => 1) if $help;
    pod2usage(-verbose => 2) if $man;
    
    if ($version) {
        print "$EXE v$VERSION\n";
        exit;
    }
    
    ## Check for File
    pod2usage("$EXE: No filename specified.\n") unless @ARGV;
    
    my $file = $ARGV[0];
    pod2usage("$EXE: $file is a directory.\n") if -d $file;
    pod2usage("$EXE: $file is not writable.\n") if !-w $file;
    
    
    #....
    print "Hello World\n";
    #....
    
    1;
    
    __END__
    
    =head1 NAME
    
    hello.pl - Mirrors a script using pod
    
    =head1 SYNOPSIS
    
    ./hello.pl [FILE]
    
    
    =head1 OPTIONS
    
    =over 4
    
    =item --version
    
    Print the version information
    
    =item --usage
    
    Print the usage line of this summary
    
    =item --help
    
    Print this summary.
    
    =item --man
    
    Print the complete manpage
    
    =back
    
    
    =head1 DESCRIPTION
    
    Sometimes a programmer just enjoys a bit of documentation.
    They can't help themselves, it makes them feel accomplished.
    
    =head1 AUTHOR
    
    Written by A Simple Coder
    

    输出:

    >perl hello.pl --test
    Unknown option: test
    Usage:
        ./hello.pl [FILE]
    

    【讨论】:

    • +1 谢谢!我开始学习 perl,所以我不知道 POD 文档。看起来很棒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2015-07-05
    相关资源
    最近更新 更多