我目前使用Python编写脚本,但出于以下几个原因,我希望尝试Ruby。在过去的一周里,我研究了大量的示例代码,并阅读了很多文档。我关注的一个问题是Ruby中缺少适当的命令行参数解析库。红宝石专家,别生我的气-也许我不知道。所以我才来这里。
在Python中,我习惯于使用argparse,在我看来,这是非常完美的(也许适合我的需要)。然而不幸的是,OptionParser不允许argparse所做的灵活性和特性。
我现正特别研究以下的限制因素:
test的程序的一个非常小的选项列表。
用法: test -h -d
我可以写一些代码,比如:实现一个opts.on( "-a",“-alpha”,"implement“) do #.结束等等。但是,我没有办法使a、b和c相互排斥,除非我对它们进行编码并进行一些错误处理。例如:
测试-ab #应该通过一个错误
在Python中,我可以非常容易地做到这一点:创建命令行参数解析器对象cmd_line_parser = argparse.ArgumentParser() #创建相互排斥的组cmd_line_group =argparse.ArgumentParser1. Secondly, I've no way of pairing `-d` with `-a` unless I specifically write code for this permutation. This is insane.
2. I've to write the `[OPTION]` list myself; I've no way to know if I am wrong or right unless and until I do a blackbox testing for all possible input permutations and map them to the blackbox list.
3. Moreover, compulsory arguments again need to be handled using special code.
在Ruby中使用optparse或其他库是否容易处理这些约束?
发布于 2013-03-18 22:34:17
有一个多多普库,它同时具有Python实现。test程序的规范是:
usage: test [-h] [-a | -b | -c] [-d] [<filename>]a、b、c选项互斥(-ab产生错误),它支持组合选项:-ad、-da等。
若要强制使用filename参数,请执行以下操作:
usage: test [-h] [-a | -b | -c] [-d] <filename>https://stackoverflow.com/questions/15487628
复制相似问题