2016-05-31 5 views
-1

言語依存関係を削除するには、Perlでコマンドライン引数を処理するためにモジュールまたはコードを使用する必要があります。以前は、Python Argparseモジュールを使用してコマンドライン引数を処理していました。PythonプログラミングでArgparseモジュールを置き換えるPerlモジュールはありますか?

argparseモジュールと私のPythonコード:

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 

私はPerlコードに上記のPythonコードを変換する方法を教えてください。ここで

+1

はあなたを持っていますまだ始まった? Perlのコマンドラインオプションの通常の疑いはhttps://metacpan.org/pod/Getopt::Longです。 – simbabque

+0

私は最初にGetopt :: ArgParseを使用しようとしましたが、モジュールのインストールに失敗し、Getopt :: Longの作業を開始しました。あなたの提案をありがとう。 – Arijit

+2

それはなぜそれが失敗したかと一緒に質問にはいるはずです。あなたの質問が今のところ、それは話題にならないように閉じられるでしょう。 – simbabque

答えて

4

は引数解析のための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 
関連する問題