您的位置 首页 php

PHP中如何通过getopt解析GNU C风格命令行选项

在 PHP 中,当我们在获取命令行参数时,可以通过遍历$argv来获取,其实呢是有规范可循的,也就是 GNU C-style parser for command line options 。

比如使用命令wget下载文件时,使用下面的一些方式来指定option都可以

wget http://mengkang.net/a.jpg -O b.jpgwget http://mengkang.net/a.jpg -Ob.jpg --tries=3 -bwget http://mengkang.net/a.jpg -Ob.jpg --tries=3 -bvd

我们整理下command line options的规则,首先参数分为短参数名和完整参数名,而且一些还有映射关系。比如我们使用wget时,-O对应–output-document。

总结 options 使用规范

● 短选项名,有一个限制,只能是一个char字符,只能1字节,不能超过1字节,比如上面的第四个命令的最后一个参数就不知道是一个选项还是三个选项了。

● 短选项名用单个连字符(-)开始

● 短选项可以一个-后面跟多个选项名

● 长选项和短选项可以有映射关系,也可以没有

● 长选项名,则是多字节的的,两个连字符(–)开始

● 选项与实参之间,可以直接连接,也可以用空格隔开,还可以用等号连接

● 但是短选项用等号连接值,等号会被视为值的一部分(但是在 php 里面却又兼容了这一点)

● 选项分为没有值,必须传值,可选传值(也就是可传可不传)

在 PHP 中的使用

getopt ( string $options [, array $longopts [, int &$optind ]] ) : arrayhttps://www.php.net/manual/zh/function.getopt.php

$options 短参数字符列表,参数字符后面用:标识必须传值;参数字符后面用::标识可选传值;只有参数字符表示该参数(或者说选项)不接受传值

$longopts 长参数由于是多字节,所以必须是数组,否则没法分隔。长参数同样遵循上面::、:规则

php里面缺少结构体的支撑,相比c的长选项的配置更加简洁,但也缺少了长短选项的映射关系配置。

$shortOpts = "O:Vv::dh";$longOpts = ["output-document:","version","verbose::", "debug", "help"]; $options = getopt($shortOpts, $longOpts);var_export($options);php getopt.php -Oa.jpg array (  'O' => 'a.jpg',)php getopt.php -O=a.jpgarray (  'O' => 'a.jpg',)php getopt.php -O a.jpg                                 array (  'O' => 'a.jpg',)php getopt.php -O=a.jpg -dhVarray (  'O' => 'a.jpg',  'd' => false,  'h' => false,  'V' => false,)php getopt.php -O=a.jpg -dhV -vvvarray (  'O' => 'a.jpg',  'd' => false,  'h' => false,  'V' => false,  'v' => 'vv',)

上面例子中我的短参数和长参数是对应的,但是没有数据结构来表示他们的对应关系(在C里面有option结构体来做这个对应关系的管理),所以我们两个都传的话,程序两个值会收到,然后我们自己判断短参数和长参数使用哪个。

php getopt.php -O=a.jpg -dhV -vvv --output-document b.jpg --debugarray (  'O' => 'a.jpg',  'd' => false,  'h' => false,  'V' => false,  'v' => 'vv',  'output-document' => 'b.jpg',  'debug' => false,)

这样写是不规范的,尽量避免这样的写法。

php getopt.php -O=a.jpg -dhVvvv  array (  'O' => 'a.jpg',  'd' => false,  'h' => false,  'V' => false,  'v' => 'vv',)php getopt.php -O=a.jpg -dhvvvVarray (  'O' => 'a.jpg',  'd' => false,  'h' => false,  'v' => 'vvV',)

总结了这个GNU C command line options 使用的套路,命令使用起来就更溜了,不会懵逼为什么linux下各种工具使用的案例写法“千奇百怪”了。

在C里面并不支持短选项和值用等号连接,在PHP里却是可以的,需要注意。

111

推荐:《PHP教程》

以上就是PHP中如何通过getopt解析GNU C风格命令行选项的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:PHP中如何通过getopt解析GNU C风格命令行选项

文章地址:https://www.zhihuclub.com/2558.shtml

关于作者: 智云科技

热门文章

评论已关闭

30条评论

  1. Note the dull puffy yellowed skin coarse sparse hair prominent tongue. Viagra Delivered To Po Box

  2. For example, chronic lymphocytic leukemia cells survive well in a lymph node niche in vivo but rapidly undergo apoptosis ex vivo

  3. Representative pictures were taken of each matrigel well quadrant, and the number of cells that had invaded into the matrigel was determined by counting the number of stained nuclei using ImageJ The mouse genome contains two Pgp genes Abcb1a and Abcb1b

  4. combivent ibuprofen minimum lethal dose The park is closed to swimmers and is ideal for non certified divers because the only shark encounters will be from inside a cage

  5. Ablation of VEGF1 in obese animals reduced breast cancer growth and lung metastasis by decreasing M2 macrophage polarization and affecting glucose metabolism Fig

  6. lapatinib will increase the level or effect of aripiprazole by affecting hepatic intestinal enzyme CYP3A4 metabolism Even the smallest changes in hormone levels can result in menstrual cycle abnormalities

  7. And a start that has seen United lose three of their opening six games in the Premier League has only supported the belief that he needs to strengthen Letter to the FDA Calling For Black Box Warnings for the Drug Vorapaxar as a Condition of Approval HRG Publication 2178 Public Citizen urged the Food and Drug Administration that if it decides to approve the drug vorapaxar, as a condition of approval the agency should require a black box warning in the drug s label

  8. Neonatal gynecomastia 38 Occurs in 90 of neonates due to placental transfer of maternal estrogens Gynecomastia is bilateral, sex independent, and spontaneously resolves within a few weeks or months Nukatsuka M, Saito H, Noguchi S, Takechi T

  9. Discomfort during the first few days following LASIK surgery, such as mild irritation and light sensitivity, is normal and to be expected Using alternative agents such as mefloquine or doxycycline can change this cost component

  10. Fauser has received fees and grant support from the following companies Organon, Schering Plough, Merck Serono, Ferring, Wyeth, Schering, Ardana, Andromed, Pantharei Bioscience and PregLem butalbital and perphenazine both increase sedation

  11. 0, will be placed in Stage B1 and not prescribed pimobendan even though the dog actually has a severely enlarged left atrium, a loud murmur, and an abnormally high VHS value, because despite all of those things, the dog s left ventricle measurement is below 1

  12. Transplanted in the mean age, the device is subtle dysmorphic disorder I take 1 pill a day 850 mg which it takes my appetite and I eat fruits slot and drink plenty of water

  13. Obama canceled a planned summit meeting with Putin after Russia granted temporary asylum to Snowden To address this possibility, we generated an additional mouse line in which ChR2 is conditionally expressed in neuropeptide receptor Y receptor 2 Npy2r positive A mechanociceptors

  14. It should be emphasized that hypopituitarism is frequently characterized by non specific symptoms, such as fatigue and loss of energy

  15. The tumor secreted prolactin which in essence screwed up my T levels as well as most of my other hormones

  16. Outcome LVEF 40 LVEF 40 Unadjusted Adjusted Unadjusted Adjusted HR 95 CI P Value HR 95 CI P Value HR 95 CI P Value HR 95 CI P Value Ultrafiltration Time to first of death, rehospitalization, unscheduled outpatient emergency department visit 1

  17. Oliver sdTdSOlfaOONFcF 5 20 2022 Therefore, clinical therapy is limited to radiation and or surgical excision for already formed HO, which is associated with an extremely high recurrence rate radiologically 82 100, clinically 17 58 and frequent complications 1, 2, 3

  18. 96, 97 Figure 51 13 illustrates activation of dacarbazine via N methyl oxidation by a microsomal P450 enzyme

  19. Thank you for your question and thank you for the very long history The main symptoms of ototoxicity are hearing loss, vertigo and tinnitus

网站地图