|
Nov
16
|
|
习惯于linux下用perl来处理一些文件文件和数据, 有时候就难免会写大量的脚本, 为脚本配上好的注释, 以及统一编码风格, 这是一个好的习惯.
只是需要敲入大量的重复代码. 其中对于输入参数的处理就是这样.
例如下面这段代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # This is a demo! if( @ARGV<1 ) { print STDERR "\n == INTRODUCTION ==\n"; print STDERR " This is a demo!\n"; print STDERR "\n == PARAMETERS ==\n"; print STDERR " -i <input> input text file\n"; print STDERR " -o <output> output file\n"; print STDERR " DEFAULT:output\n"; print STDERR "\n == USAGE ==\n"; print STDERR " $0 -i <input> \n"; print STDERR " $0 -i <input> -o output \n\n"; } my ( $temp, $input, # -i $output # -o ); while(@ARGV){ $temp = shift; if( $temp eq "-i" ){ $input = shift; } elsif( $temp eq "-o" ){ $output = shift; } else{ print STDERR "Warning: argument $temp igorned.\n"; } } if(!defined($input)){ print STDERR "Error: argument '-i' is needed.\n"; exit; } if(!defined($output)){ $output = "output" } ## End of handling parameters ## |
如果要修改参数, 可能还会牵扯到其他方面的修改. 其实我们只需要表格式地填写好各个参数的属性, 完全可以自动地生成这段代码.
这个比较地简单. 而如果我有一段格式对应的代码, 从中提取相应的参数, 这个会稍微复杂点. 不过有风骚的Regular expression, 这个就不是问题了.
Show图片了:
这篇文章来自 迷途知返(PWWANG.COM), 转载请注明出处。 版权说明
Leave a comment
| Trackback


