如何像python argparse一样在php中获得getop()的高级用法 [英] How to get advanced usage of getop() in php like python argparse

查看:114
本文介绍了如何像python argparse一样在php中获得getop()的高级用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在Python中使用 argparse模块来检测选项/参数,并打印用法等.我试图从本地代码或使用一些轻量级的库/框架在PHP中获得相同的结果,而无需编写很多包装行.

I commonly use argparse module in Python to detect options/parameters, print usage, etc. I am trying to get the same results in PHP from native code or using some lightweight library/framework without writing a lot of wrapping lines.

通过研究,我发现 getop()的本机PHP实现,这很像 getop()的C实现,并且仅限于某些用途的框架(专有选项,前缀选项,参数冲突处理程序,选择,元变量等).我想找到一种无需使用原始argv另一个改进的库 for getop()"来解析参数.

From my research, I've just found getop()'s native PHP implementation, that is quite like getop()'s C implementation and very limited as a framework for certain uses (exclusive options, prefix options, parameter conflict handler, choices, metavars, etc). I would like to find an easy way to get the same results in PHP without using raw argv, reinventing the wheel or implementing "yet another improved library for getop()" to parse arguments.

这是argparse示例在Python中的外观:

This is how the argparse example looks in Python:

from argparse import ArgumentParser, ONE_OR_MORE, ArgumentDefaultsHelpFormatter

__version__ = '3.14'
description = 'some description'

parser = ArgumentParser(
                         prog                  = 'someprogram',
                         description           = description,
                         epilog                = None,
                         parents               = [],
                         formatter_class       = ArgumentDefaultsHelpFormatter,
                         prefix_chars          = '-',
                         fromfile_prefix_chars = None,
                         argument_default      = None,
                         conflict_handler      = 'error',
                         add_help              = True,
                        )

parser.add_argument('-o', '--output',
                    action         = 'store',
                    metavar        = '/path/to/output',
                    dest           = 'output',
                    choices       = None,
                    help           = 'Set the output directory')

parser.add_argument('-q', '--quiet',
                    action         = 'store_true',
                    dest           = 'quiet', 
                    default        = False,
                    help           = 'Don\'t print status messages to stdout')

parser.add_argument(option_strings = ['FILES'], 
                    metavar        = 'FILES', 
                    nargs          = ONE_OR_MORE,
                    type=str,
                    dest           = 'FILES',
                    help           = 'One or more files to process')

parser.add_argument('-v', '--version', 
                    action  ='version',
                    version ='%(prog)s {version}'.format(version = __version__),
                    help    = 'Shows the program version')

args = parser.parse_args()

在PHP中,您可以转换相同的行为,但是需要很多行才能完全模拟Python之类的参数解析器.

In PHP you can translate the same behaviour but you need a lot of lines to fully emulate an argument parser like Python's.

PS:请不要把这个问题当作python vs php,我可能是同一个ruby vs php,java vs php或其他.这就是如何使用本机库以另一种不同的语言很好地,高效地移植代码.

PS: Please, don't take this question as python vs php, I could be the same ruby vs php, java vs php or other. It is just about how porting code well and efficient in another different language using native libs.

推荐答案

我刚刚发现了与python argparse库几乎相似的东西. php库Getopt.PHP 偏向于模拟具有argparse中多个功能和相同行为的解析器.

I just found a nearly aproximation to python argparse library. The php library Getopt.PHP partialy emulates a parser with several features and same behaviour in argparse.

例如add_options()〜= addOptions()带有位置或必需参数的方法,自动用法生成,检索值,错误处理等.

For example add_options() ~= addOptions() method with positional or required arguments, automatic usage generation, retrieval values, error handling, etc.

目前,它仍然需要一些更高级的功能,例如冲突处理程序,选择或metavar,仅解析已知值,参数排除,nargs等.但是我可以轻松地扩展它或将其用作良好的实现基础.

At this moment, it still needs some work for more advanced features like conflict handler, choices or metavar, parse known values only, parameter exclusion, nargs, etc. But I can easily extended or use it as good implementation base start.

我问题中的Python示例代码几乎可以翻译为(具有缺少的功能)为:

The Python example code in my question could be nearly translated (with missing features) to:

$getopt = new Getopt;
$getopt->addOptions(array(
    array('o', 'output', Getopt::OPTIONAL_ARGUMENT, 'Set the output directory'),
    array('q', 'quiet', Getopt::OPTIONAL_ARGUMENT, 'Don\'t print status messages to stdout'),
    array('v', 'version', Getopt::OPTIONAL_ARGUMENT, 'Shows the program version'),
    array('FILES', NULL, Getopt::REQUIRED_ARGUMENT),
));

$getopt->parse();

这篇关于如何像python argparse一样在php中获得getop()的高级用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆