Some time ago, I was writing a small command line interpreter, with the help of the standard module cmd which offers minimal support for such tasks, and I had the problem that this module doesn't have a function to parse a command line to produce a list argv which can be passed to optparse for example.
I found a first solution with the third party module pygments which contains parsers for different languages, and I parsed the command line using pygment's bash parser.
However, I was not completely happy with this solution and I started looking for the C function wich builds argv for C programs. I finally found such a function in GNU libiberty library which is used by the gcc compiler.
This function was simple enough and I decided to write a pure python implementation of this function, using python's regular expressions and following closely the syntax rules used in libiberty's buildargv function (a 100% compatibility is not at all guaranteed)
This snippet is the result of this coding. The class Argv, which subclasses list, contains methods to transform a command line into a list of arguments with a behaviour similar to that of a C compiler. It also contains methods to write the argument list to a response file and read files to extract command arguments.
Here is the code, enjoy :)
Edited 7 Years Ago by Gribouillis: n/a