如何像 myscript.py -i input.txt 这样在 python 3 中获取参数?

How to take arguments in python 3 like myscript.py -i input.txt?

我希望我的脚本采用如下参数:

python myscript.py -i input.fastq -q 23 -l 30 -o outfile.fastq

不喜欢

python myscript.py input.fastq 23 30 outfile.fastq

在 python 中是否有可用的软件包?

您可以为此使用 argparse:

例如python myscript.py -s customscriptvalue

    import argparse

   # Parse arguments
    parser = argparse.ArgumentParser(description='Script options')  
    parser.add_argument('-s', '--script', help='Custom Script to execute', default=None)
    result = parser.parse_args()

    # Treat Arguments in your script
    arguments = dict(result._get_kwargs())
    if arguments['script'] is not None:
        scriptMode = True
        scriptPath = arguments['script']

https://docs.python.org/3/library/argparse.html