App::Radã¨ããCLIãã ãã¶è¯ã
App::Rad ã¨ããã³ãã³ãã©ã¤ã³ãã¼ã«ãããã®ã§ããã
æ軽ã«ãµãã³ãã³ãä½ãã¦ã ãã¶ã¦ãã¼ãªæããªã®ã«
æ¥æ¬èªæ
å ±ãå
¨ããªãã®ã§ç´¹ä»ãã¦ã¿ã¾ãã
åºæ¬ç³»
ã¾ãã¯ãrad.plãã«ãbuchoãã¨ãããµãã³ãã³ããä½ã£ã¦ã¿ããã¨æãã¾ãã
use App::Rad; App::Rad->run; sub bucho { return "Hello Bucho!"; }
ãã£ãããã ãã§ããã¾ãã¯å®è¡ãã¦ã¿ã¾ãããã
$ perl rad.pl Usage: rad.pl command [arguments] Available Commands: bucho help show syntax and available commands
ããæãã«ãã«ããåºã¾ããã
å®ç¾©ãããµãã«ã¼ãã³ãbuchoããåæã«ãµãã³ãã³ãã¨ãã¦èªèããã¦ãã¾ããã
buchoã³ãã³ããå®è¡ãã¦ã¿ã¾ãããã
$ perl rad.pl bucho Hello Bucho!
å®è¡ããã¾ãããç°¡åï¼
help
buchoã³ãã³ãããã«ãã«åºãããã§ãããã
ãããªæãã§æ¸ãã¦ã¿ã¾ãããã
use App::Rad; App::Rad->run; sub bucho :Help(give a nice compliment) { return "Hello Bucho!"; }
å¤ãªæ§æã§ããããã«ããå®è¡ãã¦ã¿ã¾ãããã
$ perl rad.pl Usage: rad.pl command [arguments] Available Commands: bucho give a nice compliment help show syntax and available commands
ãã«ããã¤ããï¼
setup
setupã³ãã³ãã¯ã¹ã¯ãªããã®åæåå¦çã®ãããªãã®ã§ãã
ããã¤ãä¾ãæãã¦ã¿ã¾ãããã
以ä¸ã¯å
ã»ã©ã®buchoã³ãã³ããå®ç¾©ãããã®ã§ãã
sub setup { my $c = shift; $c->register_commands( { bucho => 'give a nice compliment!', }); }
ãã¡ãããããã«æ¸ãã¦ããã°:Helpã¿ãããªå¤ãªæ§æãããã¾ããã
ã¾ãããµãã«ã¼ãã³å
¨ã¦ãã³ãã³ãã¨ãã¦ç»é²ããããªããã¨ãããã§ãããã
ãã®æã«ã¯ãã®ããã«æ示çã«æå®ããã®ãè¯ãã§ãããã
éã«ãã³ãã³ãã¨ãã¦èªèãããªããã¨ããè¨å®ãããã¾ãã
以ä¸ã¯ã_ãã§å§ã¾ããµãã«ã¼ãã³ãã³ãã³ãã¨ãã¦ç»é²ããã¾ããã
sub setup { my $c = shift; $c->register_commands( { -ignore_prefix => '_' } ); }
ãã ããã³ãã³ã管çã«ã¤ãã¦ã¯
æåã«ç´¹ä»ããã使ãã³ãã³ããç»é²ãããæ¹å¼ããªã¹ã¹ã¡ã§ãã
ãªããªãuseããã¨ãã«Exportããããµãã«ã¼ãã³ãèªåã§ç»é²ããã¦ãã¾ãããã
é¤å¤æ¹å¼ã ã¨ã«ãã¼ããããªããªã£ã¦ããããã§ãã*1
ã¡ãªã¿ã«ã³ãã³ããåé¤ãããunregister_command ã
ã¨ãããµãã«ã¼ãã³ããã£ãããã¾ãã
å¼æ°ï¼ããªãã¼ã·ã§ã³
å¼æ°ãæ±ãã«ã¯optionsã使ãã¾ã*2ã
$ perl rad.pl create --id=aaa ... sub create { my $c = shift; print $c->options->{id}, "\n"; # aaa }
ããªãã¼ã·ã§ã³ãè¡ãããå ´åãgetoptã使ãæ¹æ³ã¨
èªåã§ãã§ãã¯ããæ¹æ³ãèãããã¾ãã
ã好ã¿ã§ã©ããã
# getopt sub create { my $c = shift; $c->getopt( 'id=s', 'version=i' ) or $c->execute('usage') or return undef; } # Data::Validator sub create { my $c = shift; my $args = eval { state $rule = Data::Validator->new( account_id => { isa => 'Str'}, ); $rule->validate($c->options); }; if($@) { $c->execute('usage') or return undef; } }
å ±æãã¼ã¿
stashã使ãã¨å¤æ°ãå ±æã§ãã¾ãã
use Redis::hiredis; sub setup { my $c = shift; my $redis = Redis::hiredis->new(); $redis->connect('127.0.0.1', 6379); # stashã«å ¥ãã $c->stash->{redis} = $redis; } sub create { my $c = shift; ⦠# stashããåãåºã $c->stash->{redis}->command('set foo bar'); }
ä»ã®ä¾¿å©ã¡ã½ãã
pre_processãpost_processãã¨ã¦ã便å©ã§ãã
ããã©ã«ãã®åä½ãå¤ãã£ã¦ãã¾ãå¯è½æ§ãããã®ã§
æ°ãã¤ãã¦ä½¿ãå¿
è¦ãããã§ãããã*3
example
DynamoDBãæä½ããç°¡æCRUDã³ãã³ããä½ã£ã¦ã¿ã¾ãã
# ä¾1ï¼createãµãã³ãã³ãå®è¡ perl dynamodb_crud.pl create --id=1 --name=tori
ç°¡æãããã¤ãã¼ã«ã®ãã³ãã¬ã®ãããªãã®ã
# ä¾1ï¼deployãµãã³ãã³ããdevelopmentã¢ã¼ãã§å®è¡ perl deploy.pl deploy --env=development
ãã®ä»æ³¨æç¹
ããã©ã«ãã§ã¯ãµãã³ãã³ãã®æ»ãå¤ãæ¨æºåºåãããããã*4
ä½ãåºåããããªãå ´åã¯ãã¡ãã¡
return undef;
ããªãã¨ããã¾ããã
æå¾ã«
ä»ã¾ã§ã³ãã³ãã©ã¤ã³ãã¼ã«ã¨ããã°Getopt::Longã§ãããã
ç°¡åãªã¹ã¯ãªãããªããæ軽ã«ããã¾ãã
Getopt::Longã®ä»£ããã¨ãã¦ãã¢ãªãªã®ã§ã¯ãªãã§ããããã
defaultã¡ã½ããã«å¦çãæ¸ãã°ãµãã³ãã³ããªãã§ãåãã¾ããã*5
use App::Rad; App::Rad->run(); sub default { rand(10) }
ç´¹ä»ããããªãã£ãæ©è½ãããããããã®ã§
ããã¥ã¡ã³ãããåç
§ãã ããã
http://search.cpan.org/~garu/App-Rad-1.04/lib/App/Rad.pm
*1:Data::Dumperã®Dumperã³ãã³ããªã©
*2:ããã ãã§ã¯ããã¾ããã詳ããã¯ãã«ãã
*3:ããã©ã«ãã®åä½ã¯ããã¥ã¡ã³ãã«è¼ã£ã¦ããã³ã¼ãã§ã
*4:post_processããªã¼ãã¼ã©ã¤ãããã°å¤ããã¾ã
*5:ããã¯ãdefaultã®æ¬æ¥ã®ä½¿ãæ¹ã§ã¯ãªããããªæ°ããã¾ããâ¦