perl的Getopt::Long和pod::usage ?

来源:

http://www.cnblogs.com/itech/archive/2012/08/07/2627267.html

代码:

需要显式地定义变量且初始化。例如optionX。

如果没有定义变量且显式初始化,且没有在命令行指定选项,则选项对应的变量将为未定义。

 1 #!/bin/perl-5.8.3/bin/perl$
 2 use warnings;
 3 use strict;
 4
 5 use Data::Dumper;
 6 use Getopt::Long;
 7 use Pod::Usage;
 8
 9 our $g_opts;
10 our $optionX=‘‘; #if not defined in command line, it will be empty string
11 sub parse_opts{
12     my $result = GetOptions(
13                     "optionA=s" => \$g_opts->{‘optionA‘},#string
14                     "optionB=s" => \$g_opts->{‘optionB‘},#string
15                     "optionC=i" => \$g_opts->{‘optionC‘},#integer
16                     "optionD=f" => \$g_opts->{‘optionD‘},#float
17                     "optionX=f" => \$optionX,
18                     "optionY=f" => \$optionY,
19                     "verbose"   => \$g_opts->{‘verbose‘},#flag
20                     "quiet"     => sub { $g_opts->{‘verbose‘} = 0 },
21                     "help|?"    => \$g_opts->{‘help‘}
22                   );
23     if(!($g_opts->{‘optionA‘})){
24         &pod2usage( -verbose => 1);#exit status will be 1
25     }
26     if($g_opts->{‘help‘}){
27         &pod2usage( -verbose => 1);#exit status will be 1
28     }
29 }
30
31 &parse_opts();
32 print("\n$optionX\n");
33 print($optionY); #if not defined in command line, it will be undefined
34 print($g_opts->{"optionB"});
35
36 foreach my $key (keys %{$g_opts}){
37   if(!$g_opts->{$key}) {next;}
38   print($key . "=" . $g_opts->{$key} . "\n");
39   }
40 41 exit(0);
42 43
44 45 __END__46 47 =head1 NAME
48 49 sample - Using Getopt::Long and Pod::Usage
50 51 =head1 SYNOPSIS
52 53 sample [options] [args ...]
54 55 Options:56 57    -optionA         optionA
58    -optionB         optionB
59    -optionC         optionC
60    -optionD         optionD
61    -verbose         verbose
62    -quiet           noverbose
63    -help            brief help message
64 65 =head1 OPTIONS
66 67 =over 868 69 =item B<-help>70 71 Print a brief help message and exits.72 73 =back
74 75 =head1 DESCRIPTION
76 77 B<This program> will read the given input file(s) and do something
78 useful with the contents thereof.79 80 =cut
时间: 2024-07-28 17:14:18

perl的Getopt::Long和pod::usage ?的相关文章

写的一个perl脚本,用于发送远程MySQL命令

想写一些简化管理操作的脚本,下面是基础脚本之一. 对于一个从来没使用过perl脚本的我来说还是有些难度的,直接上代码. 此脚本用于发送远程MySQL命令并且接收结果,功能比较简单,后面会渐渐完善. #!/usr/bin/perl use Getopt::Long; use DBI; Getopt::Long::GetOptions( 'host|h=s' => \$host, 'user|u=s' => \$user, 'password|pw=s' => \$password, 'po

awk,perl,python的命令行参数处理

Python,Perl,Bash命令行参数 Part I 日常经常性的和Perl,Python,Bash打交道,但是又经常性的搞混他们之间,在命令行上的特殊性和index的区别,Python真的是人性化到家了,但是命令行上就很原始,但是很有分类感.还是会喜欢Perl一点(至少还可以动脑子,不至于生锈),感觉Perl是个杂合子,而python像胡彦斌,bash像韩红(最近看我是歌手第三季,嘻嘻).Bash就是家常菜了,虽然Perl可以大部分代替Bash的功能,像grep sort map等,但是某

【转载】perl接受传递参数的方法

#! /usr/bin/perl use Getopt::Std;use warnings;use strict; sub read_from_sh($) { my $file = shift; my @files = (); open F, $file or die "Could not open $file: $!"; while (<F>) { next if /^\s*$/; push @files, $_; } close F or die "Could

使用getopt函数对命令行短形参进行处理

今天,在看man 9 style时见到了用来处理命令行短形参getopt函数,很是诧异 -- 要知道,我处理命令行参数都是用手动的!于是我如获至宝,赶紧学习学习.getopt的原型被放在unistd.h中,它的原型是这样的:int getopt(int argc, char * const argv[], const char *optstring)这里的argc,argv就是main函数的形参,需要调用是只要把main形参给传过去就ok了.后面的optstring是一个集合,包括了所有可能的短

[Perl] 利用Geopt::Long模块接收命令行参数

当perl程序需要处理用户输入的参数时,有两种方式: 第一种方法是:使用@ARGV数组 第二种方法是:调用Getopt::Long模块,该方法方便而强大.本文主要介绍该方法. Getopt::Long 在Perl中的调用实例: #!/usr/bin/perl use Getopt::Long; my ($verbose, $monitor, $debug, $test);my @libs = ();my %flags = (); GetOptions ( 'verbose+' => \$verb

How to get all the members in user group by using LDAP in Perl?

About LDAP: LDAP stands for Lightweight Directory Access Protocol. It is usually used to fetch (and sometimes update) data in a directory of people. Using Net::LDAP module in Perl can provide a way to interact with this database. Perl script to get t

一文看懂命令行参数的用法——Python中的getopt神器

一文看懂命令行参数的用法--Python中的getopt神器 参考原文:Python模块之命令行参数解析 - 每天进步一点点!!! - 博客园 https://www.cnblogs.com/madsnotes/articles/5687079.htmlpython getopt使用 - tianzhu123的专栏 - CSDN博客 https://blog.csdn.net/tianzhu123/article/details/7655499在运行程序时,可能需要根据不同的条件,输入不同的命令

Error: Package: 1:nodejs-6.11.1-1.el7.x86_64 (epel)

failed: [456] (item=[u'git', u'nodejs']) => {"changed": true, "failed": true, "item": ["git", "nodejs"], "msg": "Error: Package: 1:nodejs-6.11.1-1.el7.x86_64 (epel)\n           Requires:

(转) Linux(Centos7)yum安装最新mysql

原文:http://blog.csdn.net/gebitan505/article/details/54613549 环境 CentOS 7.1 (64-bit system) MySQL 5.6.24 CentOS 安装 参考:http://www.waylau.com/centos-7-installation-and-configuration/ 依赖 MySQL 依赖 libaio,所以先要安装 libaio yum search libaio # 检索相关信息 yum install