Chinaunix首页 | 论坛 | 博客
  • 博客访问: 855865
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-08-30 15:30:06

get the arguements of the scripts and then you can parse the arguements.
this is the same with import optparse.
  1. #!/usr/bin/perl -w
  2. #
  3. use strict;

  4. use Getopt::Std;
  5. sub init{

  6.     my %opt;
  7.     my $opt_string = 'hd:f:p:';
  8.     getopts("$opt_string", \%opt);

  9.     usage() if ( $opt{h} or not %opt);
  10. }
  11. sub usage{
  12.     print "useage of this file\n";
  13. }
  14. init();
the arguements will store in %opt. my $arg1 = $opt{d}.....
the : in $opt_string indicate that a value will follow the option. if no : follow the option, e.g. h, this indicate that no value should follow this arguement.

The getopts() function is similar, but you should pass to it the list of all switches to be recognized. If unspecified switches are found on the command-line, the user will be warned that an unknown option was given. The getopts() function returns true unless an invalid option was found.
so you can use
  1. getopts("$opt_string", \%opt) or usage();
to give out the usage.


阅读(669) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~