2

I'm writing a python script that acts as a wrapper for GCC. Most of the options that GCC takes are straightforward to handle with argparse but I'm struggling with the "-Wl,option" option. I want it to store everything after the comma so I tried the following:

parser = argparse.ArgumentParser()
parser.add_argument("-Wl,", help="Option to pass to linker.")
known, unknown = parser.parse_known_args()
print(known)
print(unknown)

However, if I run the script as follows:

python foo.py -Wl,foo

I get the following output:

Namespace(E=False, S=False, Wl,=None, c=False, optimization=None, shared=False, target=None)
['-Wl,foo']

which indicates that it didn't recognize the -Wl option.

I could change the add_argument line to read:

parser.add_argument("-W", help="Option to pass to linker.")

This works, storing "l,foo" in the W option, but GCC uses -W for warning flags and I want to keep those separated from the -Wl options.

I could run through the list of unknown args and handle them that way but I was hoping there was a more elegant solution to the problem. Any tips?

4

2 回答 2

0

在将它们传递给 argparse 之前,您可以按照简单的方法.replace(',',', ')或在所有 args 上做一些事情。.replace(',',',=')我认为其中一个应该可以解决问题。

于 2013-06-26T19:11:03.420 回答
0

您应该可以使用 CustomAction 解决此问题。http://pymotw.com/2/argparse/有一个很好的例子来说明如何做到这一点。

于 2013-06-26T21:33:55.767 回答