PHPÔÚcliģʽϽÓÊÕ²ÎÊýÓÐÁ½ÖÖ½¨
1.ʹÓÃargvÊý×e
2.ʹÓÃgetopt½¨
1.ʹÓÃargvÊý×e
ÀýÈ磺ÐeÒªÖ´ÐÐÒ»¸ophp£¬²¢´«µÝÈý¸o²ÎÊý£¨type=news, is_hot=1, limit=5)
´´½¨test.php
<?php
print_r($argv);
?>
ÔÚÃuÁiÐÐÖ´ÐÐ
php test.php news 1 5
Êa³o£º
Array
(
[0] => test.php
[1] => news
[2] => 1
[3] => 5
)
¿ÉÒÔ¿´µ½argv[0]Ϊµ±Ç°Ö´ÐеÄphpÎļþÃu³Æ£¬¶øargv[1]~argv[3]ÔoÊÇ´«µÝµÄ²ÎÊýµÄÖµ
argv[1]µÈÓÚtypeµÄÖµ
argv[2]µÈÓÚis_hotµÄÖµ
argv[3]µÈÓÚlimitµÄÖµ
ÕaÑu¿ÉÒÔ¸u¾ÝargvÊý×eÀ´»ñÈ¡´«µÝµÄ²ÎÊý½øÐкoÐøµÄ´¦Ài²Ù×÷¡£
ȱµa£º
ʹÓÃargvÊý×e£¬¿ÉÒÔ°´Ë³Ðo»ñÈ¡´«µÝµÄ²ÎÊý¡£µ«»ñÈ¡ºo£¬ÐeÒª×oÒ»¸o¶ÔÓ¦´¦Ài£¬ÉÏÀýÖÐÐeÒª°Ñargv[1]¶ÔÓ¦type²ÎÊý,argv[2]¶ÔÓ¦is_hot²ÎÊý,argv[3]¶ÔÓ¦limit²ÎÊý¡£¶øÈç¹uÔÚ´«µÝµÄ¹ý³ÌÖУ¬²ÎÊý˳Ðoд´i£¬Ôo»aµ¼Ö³ÌÐo³o´i¡£
ÀýÈ磺
<?php
$param = array();
$param['type'] = $argv[1];
$param['is_hot'] = $argv[2];
$param['limit'] = $argv[3];
print_r($param);
?>
Ö´ÐÐ
php test.php news 1 5
Êa³o£º
Array
(
[type] => news
[is_hot] => 1
[limit] => 5
)
¶ø´«µÝ˳Ðo²»Í¬£¬»ñÈ¡µ½µÄ²ÎÊýÊýÖµ»a²»Í¬£¬µ¼ÖºoÐø³ÌÐo³o´i
Ö´ÐÐ
php test.php 1 5 news
Êa³o£º
Array
(
[type] => 1
[is_hot] => 5
[limit] => news
)
Òo´ËÔÚʹÓÃargvÊý×e´«µÝ²ÎÊýʱ£¬ÐeҪעÒa²ÎÊý´«µÝµÄ˳Ðo¡£
2.ʹÓÃgetopt½¨
getopt ´ÓÃuÁiÐвÎÊýÁбiÖлñÈ¡Ñ¡Ïi
array getopt ( string $options [, array $longopts ] )
²ÎÊý£º
options
¸Ã×Öu´®ÖеÄÿ¸o×Öu»a±»µ±×oÑ¡Ïi×Öu£¬Æ¥Åa´«Èe½Å±¾µÄÑ¡ÏiÒÔµ¥¸oÁ¬×Öu(-)¿ªÍ¡£ ±ÈÈ磬һ¸oÑ¡Ïi×Öu´® ¡°x¡± ʶ±ðÁËÒ»¸oÑ¡Ïi -x¡£ Ö»ÔÊÐi a-z¡¢A-Z ºÍ 0-9¡£
longopts
Ñ¡ÏiÊý×e¡£´ËÊý×eÖеÄÿ¸oÔªËØ»a±»×÷ΪѡÏi×Öu´®£¬Æ¥ÅaÁËÒÔÁ½¸oÁ¬×Öu(¨C)´«Èeµ½½Å±¾µÄÑ¡Ïi¡£ ÀýÈ磬³¤Ñ¡ÏiÔªËØ ¡°opt¡± ʶ±ðÁËÒ»¸oÑ¡Ïi ¨Copt¡£
options ¿ÉÄÜ°uº¬ÁËÒÔÏÂÔªËØ£º
µ¥¶ÀµÄ×Ö*u£¨²»½ÓÊÜÖµ£(C)
ºoÃae¸uËaeðºÅµÄ×Ö*u£¨´ËÑ¡ÏiÐeÒªÖµ£(C)
ºoÃae¸uËaeÁ½¸oðºÅµÄ×Ö*u£¨´ËÑ¡ÏiµÄÖµ¿ÉÑ¡£(C)
Ñ¡ÏiµÄÖµÊÇ×Öu´®ºoµÄµÚÒ»¸o²ÎÊý¡£Ëu²»½eÒaֵ֮ǰÊÇñÓпոñ¡£
options ºÍ longopts µÄ¸ñʽ¼¸ºoÊÇÒ»ÑuµÄ£¬Î¨Ò»µÄ²»Í¬Ö®´¦ÊÇ longopts ÐeÒªÊÇÑ¡ÏiµÄÊý×e£¨Ã¿¸oÔªËØΪһ¸oÑ¡Ïi£(C)£¬¶ø options ÐeÒªÒ»¸o×Öu´®£¨Ã¿¸o×ÖuÊǸoÑ¡Ïi£(C)¡£
´«ÖµµÄÖ¸ou¿ÉÒÔʹÓÿոñ»o=¡£
¿ÉÑ¡ÏiµÄÖµ²»½ÓÊÜ¿Õ¸ñ×÷Ϊָou£¬Ö»ÄÜʹÓÃ=×÷Ϊָou¡£
*µ»ØÖµ
´Ëº¯Êý»aµ»ØÑ¡Ïi/²ÎÊý¶Ô£¬Ê§°Üʱµ»Ø FALSE¡£
Ñ¡ÏiµÄ½aÎo»aÖÕÖ¹ÓÚÕÒµ½µÄµÚÒ»¸o*ÇÑ¡Ïi£¬Ö®ºoµÄÈκζ«Î÷¶¼»a±»¶ªÆu¡£
1.ʹÓÃoptionsʵÀý
a,b,c ΪÐeÒªÖµ
d Ϊ¿ÉÑ¡Öµ
e Ϊ²»½ÓÊÜÖµ
<?php
$param = getopt('a:b:c:d::e');
print_r($param);
?>
Ö´ÐÐ
php test.php -a 1 -b 2 -c 3 -d=4 -e 5
Êa³o£º
Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
[e] =>
)
2.ʹÓÃlongoptsʵÀý
type,is_hot ΪÐeÒªÖµ
limit Ϊ¿ÉÑ¡Öµ
expire Ϊ²»½ÓÊÜÖµ
<?php
$longopt = array(
'type:',
'is_hot:',
'limit::',
'expire'
);
$param = getopt('', $longopt);
print_r($param);
?>
Ö´ÐÐ
php test.php --type news --is_hot 1 --limit=10 --expire=100
Êa³o£º
Array
(
[type] => news
[is_hot] => 1
[limit] => 10
[expire] =>
)
*3.ÕÒµ½µÚÒ»ÇÑ¡Ïi£¬ºoÃaeºoÂÔʵÀý**
<?php
$longopt = array(
'type:',
'is_hot:',
'limit::',
'expire'
);
$param = getopt('', $longopt);
print_r($param);
?>
Ö´ÐÐ
php test.php --type news --is_hots 1 --limit=10 --expire=100
Êa³o:
Array
(
[type] => news
)
ÒoΪis_hots²»ÊÇÑ¡ÏiÖµ£¨¶¨ÒaµÄÊÇis_hot£(C)£¬ËuÒÔ´ÓÕaÀi¿ªÊ¼Ö®ºoµÄ²ÎÊý£¬¶¼±»¶ªÆu¡£
×ܽa:
ʹÓÃargvÊý×e´«²ÎÊý£¬½¨¼oµ¥£¬ÊµÏÖ*½±a¡£²ÎÊýµÄ˳Ðo²»ÄÜ´i£¬²ÎÊý»ñÈ¡ºoÐeÒª×o¶ÔÓ¦´¦Ài¡£
ʹÓÃgetopt½¨£¬¿ÉʹÓòÎÊýÃu£¬²ÎÊý˳Ðo¿ÉËaeÒa£¬±È½Ï¹ae*¶¡££¨½¨ÒeʹÓã(C)
ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu±¾ÎĵÄÄÚÈݶԴo¼ÒµÄѧϰ»oÕß¹¤×÷ÄÜ´øÀ´Ò»¶¨µÄ°iÖu£¬Í¬Ê±Ò²Ï£Íu¶a¶aÖ§³Ö½Å±¾Ö®¼Ò!