是否有一个Perl模块来替换Python编程中的Argparse模块?

时间:2016-05-31 09:16:22

标签: python perl

要删除语言依赖项,我需要使用任何模块或代码来处理Perl中的命令行参数。之前我使用Python Argparse 模块来处理命令行参数。

我的Python代码与argparse模块:

import argparse

default_database_config='config/database.config'
default_general_config = 'config/general.config'
commandLineArgumentParser = argparse.ArgumentParser()
commandLineArgumentParser.add_argument("-dconfig", "--dconfigaration", help="Database Configuration file name", default=default_database_config)
commandLineArgumentParser.add_argument("-gconfig", "--gconfigaration", help="General Configuration file name", default=default_general_config)
commandLineArguments = commandLineArgumentParser.parse_args()

database_config_file = commandLineArguments.dconfigaration
general_config_file = commandLineArguments.gconfigaration

如何将上述Python代码转换为Perl代码?

1 个答案:

答案 0 :(得分:5)

这是使用Getopt::Long的基本示例,这是Perl事实上的参数解析标准:

use Getopt::Long;

my $ip;
my $port;
my $foreground = 0; # defaults to 0 if not sent in
my $stdout = 1;     # defaults to 1 if not sent in
my $debug;

GetOptions(
    "ip=s"       => \$ip,         # string
    "port=i"     => \$port,       # int
    "fg"         => \$foreground, # bool, flag doesn't need a param
    "stdout"     => \$stdout,
    "debug=s"    => \$debug,
);

这样打电话:

script.pl --port 7800 --ip 127.0.0.1

或者:

script.pl -p 7800 -i 127.0.0.1
相关问题