Arijit Arijit - 1 year ago 95
Perl Question

Is there any Perl module to replace Argparse module in python programming

Earlier I used argparse python module but to remove the language dependency I am using only perl and I need a same kind of module in Perl for that I need your help.

My Python code with argparse module

import argparse

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

So I need help to convert the above python code to Perl code.
Thanks in advance.

Answer Source

Here's a basic example using Getopt::Long, pretty much Perl's de-facto standard for argument parsing:

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;

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

Call like this: --port 7800 --ip

Or: -p 7800 -i
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download