从命令行随机解析Python中的文件

Randomly parsing files in Python from the command line

当我用 C 编程时,我想出了一种方法来以任何顺序为我的程序解析命令行参数,但我只需要指定文件是什么。 所以我的输入是这样的:./exe.out -i <inputfile> -o <outputfile> -a <someotherfile> etc... 这可能会像这样混淆:./exe.out -o <outputfile> -i <inputfile> -a <someotherfile> etc...

现在我需要在 python 中执行此操作。意思是,有一个方法可以处理程序开头的命令行参数和 return 主程序的文件位置。

例如 python test.py -i <inputfile> -o <outputfile> -a <someotherfile> etc...

这能做到吗?我找到了一种方法,但顺序必须精确,这对我的项目不会

您正在寻找 optparse(Python 2.7 之前)或 argparse(Python 2.7 或更高版本)。