本文介绍了python中的任意命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许使用任意命令行参数。如果用户向我提供了如下所示的命令行

I want to allow arbitrary command line arguments. If the user provides me with a command line that looks like this

myscript.py --a valueofa --b valueofb posarg1 posarg2

我知道 a 是通过 valueofa b valueofb 一起传递,并且我有最后两个位置参数。

I know that a was passed with valueofa, b passed with valueofb and that I have these last two positional arguments.

我一直使用optparse,您可以为其精确指定要查找的参数。但是我希望用户能够从命令行定义任意的宏。当然,有一个python模块,它比我写的任何东西都更优雅。什么是?

I've always used optparse, for which you specify exactly which arguments to look for. But I want the user to be able to define arbitrary "macros" from the command line. Surely there's a python module that does it more elegantly than anything I'd write. What is?

推荐答案

可悲的是,您。如果需要支持,则需要编写自己的选项解析器=(。

Sadly, you can't. If you have to support this, you'll need to write your own option parser =(.

这篇关于python中的任意命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!