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

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

问题描述

我通常使用 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 实现,并且非常有限特定用途的框架(独占选项、前缀选项、参数冲突处理程序、选择、元变量等).我想找到一种简单的方法来在 PHP 中获得相同的结果,而无需使用原始 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.

目前,它仍然需要一些更高级的功能,如冲突处理程序、选择或元变量、仅解析已知值、参数排除、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();

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

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