How can I parse the command line arguments?

I want to parse the argument list in a perl script, for example, I have this situation:

script.pl -h 127.0.0.1 -u user -p pass arg1 arg2 arg3

How can I perform to parse a list of arguments that are not parameters in an array and a parameter parameter in a scalar value?

Thank.

+3
source share
2 answers

Well, if these are the only things on the command line that are not specified as parameters, they should still be in @ARGV. So just use @ARGV.

use Getopt::Long;

# save arguments following -h or --host in the scalar $host
# the '=s' means that an argument follows the option
# they can follow by a space or '=' ( --host=127.0.0.1 )
GetOptions( 'host=s' => \my $host 
          , 'user=s' => \my $user  # same for --user or -u
          , 'pass=s' => \my $pass  # same for --pass or -p
          );

# @ARGV: [ qw<arg1 arg2 arg3> ]

cm. Getopt::Long

+10
source

The arguments of your program are stored in @ARGV.

, Getopt::Long, Perl.

: ", , ", , Getopt::Long, , , , ", , Getopt:: Long ".

. Getopt:: Long, , .

+12

Source: https://habr.com/ru/post/1770413/


All Articles