来源:

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

代码:

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

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

 #!/bin/perl-5.8.3/bin/perl$
use warnings;
use strict; use Data::Dumper;
use Getopt::Long;
use Pod::Usage; our $g_opts;
our $optionX=''; #if not defined in command line, it will be empty string
sub parse_opts{
my $result = GetOptions(
"optionA=s" => \$g_opts->{'optionA'},#string
"optionB=s" => \$g_opts->{'optionB'},#string
"optionC=i" => \$g_opts->{'optionC'},#integer
"optionD=f" => \$g_opts->{'optionD'},#float
"optionX=f" => \$optionX,
"optionY=f" => \$optionY,
"verbose" => \$g_opts->{'verbose'},#flag
"quiet" => sub { $g_opts->{'verbose'} = },
"help|?" => \$g_opts->{'help'}
);
if(!($g_opts->{'optionA'})){
&pod2usage( -verbose => );#exit status will be 1
}
if($g_opts->{'help'}){
&pod2usage( -verbose => );#exit status will be 1
}
} &parse_opts();
print("\n$optionX\n");
print($optionY); #if not defined in command line, it will be undefined
print($g_opts->{"optionB"}); foreach my $key (keys %{$g_opts}){
if(!$g_opts->{$key}) {next;}
print($key . "=" . $g_opts->{$key} . "\n");
}
exit(); __END__ =head1 NAME
sample - Using Getopt::Long and Pod::Usage
=head1 SYNOPSIS
sample [options] [args ...]
Options: -optionA optionA
-optionB optionB
-optionC optionC
-optionD optionD
-verbose verbose
-quiet noverbose
-help brief help message
=head1 OPTIONS
=over =item B<-help> Print a brief help message and exits. =back
=head1 DESCRIPTION
B<This program> will read the given input file(s) and do something
useful with the contents thereof. =cut

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

  1. Perl 模块 Getopt::Std 和 Getopt::Long

    示例程序: getopt.pl; 1 2 3 4 5 6 7 8 #!/usr/bin/perl -w #use strict; use Getopt::Std; use vars qw($opt_a ...

  2. MySQL高可用方案MHA的部署和原理

    MHA(Master High Availability)是一套相对成熟的MySQL高可用方案,能做到在0~30s内自动完成数据库的故障切换操作,在master服务器不宕机的情况下,基本能保证数据的一 ...

  3. mysql MHA架构搭建过程

    [环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 系统 IP 主机名 备注 版本 xx系统 192.168.142. ...

  4. Orchestrator+Proxysql 实现自动导换+应用透明读写分离

    前面一篇博客我介绍了如何通过Orachestrator+脚本实现自动导换. 这一章,我来演示一下如何通过Orchestrator+Proxysql 实现自动导换+应用透明读写分离 总体架构 可选架构1 ...

  5. [Perl] Getopt 函数来接收用户参数的使用

    我们在linux常常用到一个程序需要加入参数,现在了解一下perl中的有关控制参数的函数.getopt.在linux有的参数有二种形式.一种是–help,另一种是-h.也就是-和–的分别.–表示完整参 ...

  6. python module getopt usage

    import getopt import sys def usage(): print 'this is a usage.' def main(): try: print sys.argv #sys. ...

  7. Perl:写POD文档

    官方手册:https://perldoc.perl.org/perlpod.html POD文档是perl的man文档,可以用perldoc输出,也可以直接用man输出.在开始下面的文章之前,请先粗略 ...

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

    Python,Perl,Bash命令行参数 Part I 日常经常性的和Perl,Python,Bash打交道,但是又经常性的搞混他们之间,在命令行上的特殊性和index的区别,Python真的是人性 ...

  9. perl 里面如何写出阅读友好的代码提示

    在我们使用别人写好的程序时,经常会使用-h 之类的东西查看一下简单的帮助手册或者说明信息: 对于perl 语言而言,写起来简单,经常随手一写,解决了当时的问题,但是过几天去看,你都不知道这个脚本该怎么 ...

随机推荐

  1. javaWEB总结(14):请求的转发和重定向

    通俗理解请求转发与重定向的流程 通俗的来讲:我们可以把请求转发和重定向当成是一种借钱的情景模式. (1)请求的转发:A向B借钱,B自己没有钱,但是向C借到了钱,并且把钱借给了A.A只向B请求了一次. ...

  2. openstack私有云布署实践【14.2 登录页dashboard-controller(办公网环境)】

    这一小节基本配置相同,但留意以下紫色部份的配置,当初为了管理方便,我们让办公网openstack的dashboard的登录桥接了科兴的dashboard,由此统一dashboard界面的登录地址   ...

  3. URAL 1525 Path

    #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> us ...

  4. zoj 2193 poj 2585 Window Pains

    拓扑排序. 深刻体会:ACM比赛的精髓之处不在于学了某个算法或数据结构,而在于知道这个知识点但不知道这个问题可以用这个知识去解决!一看题目,根本想不到是拓扑排序.T_T...... #include& ...

  5. 第六十九节,css入门基础

    css入门基础 学习要点: 1.使用CSS 2.三种方式 3.层叠和继承 本章主要探讨HTML5中CSS (层叠样式表),它是用来对HTML文档外观的表现形式进行排版和格式化. 一 使用CSS CSS ...

  6. HDU 2212 DFS

    Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...

  7. 【Python@Thread】threading模块

    theading模块的Thread类 属性: name 线程名 ident 线程标识符 daemon  布尔值,标示是否为守护线程 方法: __init__(target=None, name=Non ...

  8. CentOS/RedHat rpm方式安装Apache2.2

    注:所有RPM包均从网易镜像上下载 # rpm -ivh /home/apache/apr-1.3.9-5.el6_2.x86_64.rpm warning: /home/apache/apr-1.3 ...

  9. 浙大pat 1035题解

    1035. Password (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...

  10. C#获取数字证书

    string Thumbprint = "C2489D912F247C187AA14B1291A6fB612281225D"; X509Store store = new X509 ...