[英]How to pass directory path as arguments from command line using perl?
My question as follows: 我的问题如下:
I struck with how to pass the command line arguments instead of passing directory path using perl . 我很想知道如何传递命令行参数,而不是使用perl传递目录路径。
Example suppose if am executing the file as follows: 示例假定是否按以下方式执行文件:
./welcome.pl -output_dir "/home/data/output"
My code: 我的代码:
#!/usr/local/bin/perl
use strict;
use warnings 'all';
use Getopt::Long 'GetOptions';
GetOptions(
'output=s' => \my $output_dir,
);
my $location_dir="/home/data/output";
print $location_dir;
Code explanation: 代码说明:
I tried to print the contents in the $output_dir.so i need to pass the command line arguments inside the variable (ie $location_dir
) instead of passing path directly how can i do it? 我试图在$ output_dir.print中打印内容,所以我需要传递变量内的命令行参数(即
$location_dir
),而不是直接传递路径,我该怎么办?
use strict;
use warnings 'all';
use File::Basename qw( basename );
use Getopt::Long qw( GetOptions );
sub usage {
if (@_) {
my ($msg) = @_;
chomp($msg);
print(STDERR "$msg\n");
}
my $prog = basename($0);
print(STDERR "$prog --help for usage\n");
exit(1);
}
sub help {
my $prog = basename($0);
print(STDERR "$prog [options] --output output_dir\n");
print(STDERR "$prog --help\n");
exit(0);
}
Getopt::Long::Configure(qw( posix_default )); # Optional, but makes the argument-handling consistent with other programs.
GetOptions(
'help|h|?' => \&help,
'output=s' => \my $location_dir,
)
or usage();
defined($location_dir)
or usage("--output option is required\n");
print("$location_dir\n");
Or course, if the argument is indeed required, then why not just use ./welcome.pl "/home/data/output"
instead of an not-really optional parameter. 或者,当然,如果确实需要该参数,那么为什么不只使用
./welcome.pl "/home/data/output"
而不是一个不是真的可选参数。
use strict;
use warnings 'all';
use File::Basename qw( basename );
use Getopt::Long qw( GetOptions );
sub usage {
if (@_) {
my ($msg) = @_;
chomp($msg);
print(STDERR "$msg\n");
}
my $prog = basename($0);
print(STDERR "$prog --help for usage\n");
exit(1);
}
sub help {
my $prog = basename($0);
print(STDERR "$prog [options] [--] output_dir\n");
print(STDERR "$prog --help\n");
exit(0);
}
Getopt::Long::Configure(qw( posix_default )); # Optional, but makes the argument-handling consistent with other programs.
GetOptions(
'help|h|?' => \&help,
)
or usage();
@ARGV == 1
or usage("Incorrect number of arguments\n");
my ($location_dir) = @ARGV;
print("$location_dir\n");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.