Perl에서 Getopt::Std 사용하기
Filed in: IT Add comments
Unix스타일의 프로그램 파라미터 처리를 해주는 모듈이 Getopt인데
자주 사용하다 보니 codesnipet으로 사용하려고, 내가 사용하는 템플릿을 여기에 등록해 둔다.
main::HELP_MESSAGE(), main::VERSION_MESSAGE() 를 구현해 두면 – -help, – -version 옵션에 반응하고, getopts(c:hv)에 사용할 옵션들을 나열하면 된다.
위 예에서 c뒤에 : 의 의미는 파라미터를 받는 다는 의미이다.
입력된 파라미터는 $Getopt::Std::opt_c로 사용할 수 있다.
#!/usr/bin/perl # vim: set sw=4 ts=4 si et nu: use strict; use Getopt::Std;
######################################################## # Global settings ######################################################## $Getopt::Std::STANDARD_HELP_VERSION = 1; sub main::VERSION_MESSAGE(){ print "$0 Version 0.5 hyeonkwen@gmail.com"; } sub main::HELP_MESSAGE(){ print <<END; Description of this program. Usage: $0 OPTION DIRECTORY OPTION -c description of c option -h view help message -v view version information END }
######################################################## # main() ######################################################## sub main(){ if(!getopts("c:hv") || $Getopt::Std::opt_h){ main::HELP_MESSAGE(); exit; }elsif($Getopt::Std::opt_v){ main::VERSION_MESSAGE(); exit; } } main();