`
izuoyan
  • 浏览: 8945239 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Perl getopts Howto--如何使用perl中的getopts

阅读更多

Perl getopts Howto

This howto comes with no guaratees other than the fact that these code segments were copy/pasted from code that I wrote and ran successfully.

<style type="text/css"> <!-- .pod PRE { background : #eeeeee; border : 1px solid #888888; color : black; padding-top : 1em; white-space : pre; } .pod H1, H2 { background : transparent; color : #ff7300; } --></style>

Process options passed to a program using getopts().

Make a global hash to store the options. Use the standard Getopt module. Make a string of one-character options. A character preceeding a colon takes an argument. The getopts function takes two arguments: a string of options, and a hash reference. For each command line option (aka switch) found, getopts sets $opt{x} (where x is the switch name) to the value of the argument, or 1 if no argument was provided.

Example

    #
    # Globals
    #
    use vars qw/ %opt /;

    #
    # Command line options processing
    #
    sub init()
    {
        use Getopt::Std;
        my $opt_string = 'hvdf:';
        getopts( "$opt_string", \%opt ) or usage();
        usage() if $opt{h};
    }

    #
    # Message about this program and how to use it
    #
    sub usage()
    {
        print STDERR << "EOF";

    This program does...

    usage: $0 [-hvd] [-f file]

     -h        : this (help) message
     -v        : verbose output
     -d        : print debugging messages to stderr
     -f file   : file containing usersnames, one per line

    example: $0 -v -d -f file

    EOF
        exit;
    }

    init();

    print STDERR "Verbose mode ON.\n" if $opt{v};
    print STDERR "Debugging mode ON.\n" if $opt{d};

AUTHOR

Alex BATKO <abatko AT cs.mcgill.ca>

分享到:
评论

相关推荐

    解析鱼中的CLI选项。-Linux开发

    Getopts Getopts是鱼类的命令行选项解析器。 getopts -ab1 --foo = bar baz | 而读-l键值开关$ key ...curl -Lo〜/ .config / fish / functions / getopts.fish --create-dirs git.io/getopts用法研究以下内容中的输出

    getopts:解析CLI参数

    基于已经使用了数十年的,Getopts合理的默认值可帮助您编写看上去和感觉上像是真正的交易的CLI工具。 $ example --type=module -o main.js * .{js,json} import getopts from "getopts" const options = getopts ...

    plugin-getopts:鱼类的getopts的Unix兼容实现

    getopts 友好 :fish:用法Add ` getopts ` to your ` $fish_plugins ` or import directly into your library via `import plugins/ getopts `.前提每个实用程序/函数都需要处理参数。 通常会发生的是,每个函数都会...

    Advanced Bash-Scripting Guide <>

    在Perl 脚本中使用eval 命令来强制变量替换 11-15. 使用set 来改变脚本的位置参数 11-16. 重新分配位置参数 11-17. Unset 一个变量 11-18. 使用export 命令传递一个变量到一个内嵌awk 的脚本中 11-19. 使用getopts ...

    node-getopts:符合POSIXGNU的CLI参数解析器

    getopts 负责CLI参数解析和验证,因此您不必这样做。强调遵循POSIX / GNU约定 严格的输入验证错误被收集到一个数组...解析选项/参数,使用过滤器,并检查错误const { opts , args , errors } = getopts ( { opts : [

    Linux高级bash编程

    在Perl脚本中使用eval命令来强制变量替换 11-15. 使用set来改变脚本的位置参数 11-16. 重新分配位置参数 11-17. Unset一个变量 11-18. 使用export命令传递一个变量到一个内嵌awk的脚本中 11-19. 使用getopts命令来...

    Shell脚本中使用getopts处理多命令行选项

    今天小编就为大家分享一篇关于Shell脚本中使用getopts处理多命令行选项,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    2009 达内Unix学习笔记

    输出重定向,意思就是说,将原来屏幕输出变为文件输出,即将内容输到文件中。 输入重定向。 本来命令是通过键盘得到输入的,但是用小于号,就能够使命令从文件中得到输入。 \ 表示未写完,回车换行再继续。 * ...

    GetOpts:阅读Wolfram语言(又名Mathematica)中的命令行选项

    阅读Wolfram语言(又名Mathematica)中的命令行选项 文献资料 GetOps[argv, spec]根据spec列表提取argv list的值。 argv是代表命令行参数(程序名称除外)的字符串列表。 具有长和短选项值的选项的受支持格式为: -...

    linux-getoptsfish的命令行选项解析器

    getopts - fish的命令行选项解析器

    typedopts:Rust中具有类型意识的命令行解析器

    Rust中可识别类型的命令行解析基本原理尽管getopts可用于Rust平台,但它不是很实用,因为必须检查生成的属性映射是否存在,然后将其转换为所需的类型,从而导致许多选项和模式匹配。 Typedopts在上用法使用此库,您...

    erlang19安装包

    kernel: Add net_kernel:setopts/2 and net_kernel:getopts/2 to control options for distribution sockets in runtime. asn1: Compiling multiple ASN.1 modules in the same directory with parallel make (make ...

    validateInput:一个简单的“getOpts”类型脚本来验证输入参数。-matlab开发

    可以使用 isfield 来检查输入参数是否已传递,或者如果该字段为 false,则是否启用了 'force' 选项。 例子: varargin={'p','s',10,'name','john doe'}; validParameters={{'size','s'},{'print','p'},'name'}; ...

    shell教程-30章,下了之后会让你大吃一惊,相当好

    14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...

    LINUX与UNIX_Shell编程指南(下)

    26.5.2 在脚本中使用logger命令 303 26.6 小结 305 第27章 几个脚本例子 306 27.1 pingall 306 27.2 backup_gen 306 27.3 del.lines 312 27.4 access.deny 313 27.5 logroll 316 27.6 nfsdown 317 27.7 小结 317 第...

    Linux与unix shell编程指南

    14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...

    Shell脚本学习笔记

    7.1.2 在命令行中使用多个编辑器命令 121 7.1.3 从文件读取编辑器命令 121 7.2 gawk程序 122 7.2.1 gawk命令格式 122 7.2.2 自命令行读取程序脚本 122 7.2.3 使用数据字段变量 123 7.2.4 在程序脚本中使用多个命令 ...

    LINUX与UNIX SHELL编程指南 高清PDF

    14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...

    功能齐全,功能最丰富的Vue.js终端模拟器-JavaScript开发

    功能使用getopts解析参数支持异步命令浏览历史记录(使用↑/↓)和自动完成解析器(使用↹)使用插槽自定义终端搜索历史记录(使用Ctrl + r)安装$ npm i vue-command --save用法让我们从一个非常简单的例子开始简单...

Global site tag (gtag.js) - Google Analytics