【问题标题】:Are there other ways of validating command line args in python3?是否有其他方法可以在 python3 中验证命令行参数?
【发布时间】:2020-03-07 13:53:57
【问题描述】:

是否还有其他更有效的方法来验证命令行参数而不需要外部模块和 argparse?

import sys
import getopt

argv = sys.argv[1:]
try:
    opts, args = getopt.getopt(argv, "h", ["help", "version", "number="])
except getopt.GetoptError as error:
    print(error)
    exit(1)
if args:
    exit(1)
print(opts)
print(args)

所以我会这样做,但是可以吗?我是 python 新手,想尽可能多地使用 python 功能

【问题讨论】:

  • 查看 argparse 模块。来自docs.python.org/3.7/library/getopt.html:“getopt 模块是一个命令行选项解析器,其 API 旨在让 C getopt() 函数的用户熟悉。不熟悉 C getopt() 函数或想编写的用户更少的代码并获得更好的帮助和错误消息应该考虑使用 argparse 模块。”
  • 你真的不应该使用getop,使用更高级别和更全功能的东西,至少argparse甚至是第三方的东西,比如click
  • clickdocopt 等外部库也可能值得研究。
  • 感谢您编辑您的问题,但为什么“没有 argparse”?这显然是标准库中内置的可行答案。如果我们不了解您的限制条件,我们将无法帮助您。
  • 您的问题有点难以理解,您基本上是在问“还有其他解析参数的方法”,但是您的要求拒绝所有其他方式...我不确定您是什么期待。

标签: python command-line-arguments getopt


【解决方案1】:

Python 中有几个很好的库来支持命令行解析和参数验证。

我过去曾尝试过Argparse,效果很好。您可以参考this 的回答来了解如何使用 Argparse。

希望对你有帮助!

【讨论】:

    【解决方案2】:

    您应该查看 Python 的内置 argparse。从命令行手动解析复杂的命令会省去很多麻烦。您可以强制某些参数为某种类型或值。

    使用示例:

    import sys
    import argparse
    
    PHASES = ['clean', 'package', 'install', 'test', 'deploy']
    ALT_PHASES = ['docs', 'demos', 'update']
    
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'phase',
        help="the target phase",
        choices=PHASES + ALT_PHASES
    )
    parser.add_argument(
        '--skip-demos',
        help="skip packaging and deployment of demos",
        action='store_const',
        const=str
    )
    parser.add_argument(
        '--skip-docs',
        help="skip generation and deployment of user's guide",
        action='store_const',
        const=str
    )
    parser.add_argument(
        '--skip-tests',
        help="skip tests",
        action='store_const',
        const=str
    )
    parser.add_argument(
        '--skip-wheels',
        help="skip wheel generation",
        action="store_const",
        const=str
    )
    parser.add_argument(
        '--update',
        help="update the source code in the virtual environment; do not make the wheels",
        action="store_const",
        const=str
    )
    
    def main(args):
        parsed_args = parser.parse_args(args)
        print(parsed_args.phase) # prints the phase
    
    
    if __name__ == "__main__":
        main(sys.argv[1:])
    

    输入无效参数时的示例输出:

    $ python3 build.py hello
    usage: build.py [-h] [--skip-demos] [--skip-docs] [--skip-tests]
                    [--skip-wheels] [--docs-branch DOCS_BRANCH]
                    [--skip-formatting] [--int-tests] [--update]
                    {clean,package,install,test,deploy,docs,demos,update}
    build.py: error: argument phase: invalid choice: 'hello' (choose from 'clean', 'package', 'install', 'test', 'deploy', 'docs', 'demos', 'update')
    

    【讨论】:

    • 问题是我不能使用 argparse :c
    • @dhvcc,为什么不呢?你为什么不把它包括在你的问题中?请阅读How to Ask
    猜你喜欢
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    相关资源
    最近更新 更多