English 中文(简体)
Perl Getopt:Long Related Question - 相互排斥突击队
原标题:
  • 时间:2009-05-20 16:10:59
  •  标签:

我有以下手稿:


my $directory;
my @files;
my $help;
my $man;
my $verbose; 

undef $directory;
undef @files;
undef $help;
undef $man;
undef $verbose;

GetOptions(
           "dir=s" => $directory,  # optional variable with default value (false)
           "files=s" => @files,    # optional variable that allows comma-separated
                                # list of file names as well as multiple 
                    # occurrenceces of this option.
           "help|?" => $help,      # optional variable with default value (false)
           "man" => $man,          # optional variable with default value (false)
           "verbose" => $verbose   # optional variable with default value (false)
          );

    if (@files) {
    @files = split(/,/,join( , , @files));
    }

处理相互排斥的指挥线论点的最佳方式是什么? 我只想让用户只进入“-dir”或“-files”的指挥线论点,而不是两者。 难道有什么办法可以让Getopt这样做?

感谢。

问题回答

我并不认为在Getopt有办法:虽然如此,但很容易在你自己实施(我假定有一种使用功能,能够把一个能够告诉使用者如何使用方案):

die usage() if defined $directory and @files;

为什么不仅如此:

if ($directory && @files) {
  die "dir and files options are mutually exclusive
";
}

你可以简单地检查这两种变量中是否存在价值。

if(@files && defined $directory) {
    print STDERR "You must use either --dir or --files, but not both.
";
    exit 1;
}

或者,如果你想简单地忽略第一次——dir或————文件之后确定的任何选择,你就能够指出这两个功能。

#!/usr/bin/perl

use Getopt::Long;

my $directory;
my @files;
my $mode;
my $help;
my $man;
my $verbose; 

GetOptions(
    "dir=s" => &entries,    # optional variable with default value (false)
    "files=s" => &entries,  # optional variable that allows comma-separated
                             # list of file names as well as multiple 
                             # occurrences of this option.
    "help|?" => $help,      # optional variable with default value (false)
    "man" => $man,          # optional variable with default value (false)
    "verbose" => $verbose   # optional variable with default value (false)
);

sub entries {

   my($option, $value) = @_;

    if(defined $mode && $mode ne $option) {
        print STDERR "Ignoring "--$option $value" because --$mode already specified...
";
    }
    else {
        $mode = $option unless(defined $mode);
        if($mode eq "dir") {
            $directory = $value;
        }
        elsif($mode eq "files") {
            push @files, split(/,/, $value);
        }
    }

    return;

}

print "Working on directory $directory...
" if($mode eq "dir");
print "Working on files:
" . join("
", @files) . "
" if($mode eq "files");

您可以通过<条码>做到这一点。 Getopt:Long:Descriptive。 缩略语不同于<代码>Getopt:Long,但如果你重印使用摘要,则有助于通过为你做所有工作来减少重复。

在此,我添加了一个称为<代码>来源的隐选,即<代码>$->dir或>.files,视提供哪种选择而定,并将执行。 制约你们。 给出的数值将载于$opt->dir$opt->files,以两者中任何一种为准。

my ( $opt, $usage ) = describe_options(
     %c %o ,
    [ "source" => hidden => {
         one_of  => [
            [ "dir=s" => "Directory" ],
            [ "files=s@" => "FilesComma-separated list of files" ],
        ]
    } ],
    [ "man" => "..." ],          # optional variable with default value (false)
    [ "verbose" => "Provide more output" ],   # optional variable with default value (false)
    [],
    [  help|?  => "Print usage message and exit" ],
);
print( $usage->text ), exit if ( $opt->help );

if ($opt->files) {
    @files = split(/,/,join( , , @{$opt->files}));
}

“The main> for the >, : all the >, : : < > > > >, <>> >, with>, with each, per one per one per a own/2007/5 as with ::L<><>>>>>:

use strict;
use warnings;
use Getopt::Long;

my($directory,@files,$help,$man,$verbose);

GetOptions(
   dir=s    => sub {
    my($sub_name,$str) = @_;
    $directory = $str;

    die "Specify only --dir or --files" if @files;
  },

  # optional variable that allows comma-separated
  # list of file names as well as multiple 
  # occurrences of this option.
   files=s  => sub {
    my($sub_name,$str) = @_;
    my @s = split  , , $str;
    push @files, @s;

    die "Specify only --dir or --files" if $directory;
  },    

  "help|?"  => $help,
  "man"     => $man,
  "verbose" => $verbose,
);

use Pod::Usage;
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
=head1 NAME

sample - Using Getopt::Long and Pod::Usage

=head1 SYNOPSIS

sample [options] [file ...]

 Options:
   -help            brief help message
   -man             full documentation

=head1 OPTIONS

=over 8

=item B

Print a brief help message and exits.

=item B

Prints the manual page and exits.

=back

=head1 DESCRIPTION

B will read the given input file(s) and do something
useful with the contents thereof.

=cut




相关问题
热门标签