On Tue, Aug 15, 2017 at 10:56:07AM +0100, Daniel P. Berrange wrote: > On Tue, Aug 15, 2017 at 11:47:28AM +0200, Markus Armbruster wrote: > > Eduardo Habkost <ehabk...@redhat.com> writes: > > > > > It makes command-line parsing and generation of help text much > > > simpler. > > > > There's really no excuse for parsing command line arguments by hand in > > Python. > > > > > The optparse module is deprecated since Python 2.7, but argparse > > > is not available in Python 2.6 (the minimum Python version > > > required for building QEMU). > > > > We have a few uses of argparse in the tree. Are they okay? > > > > We also use getopt in places. Perhaps we should pick one way to parse > > command lines and stick to it. > > I just came up against this problem with keycodemapdb which I'm adding as > a submodule to qemu. I used argparse there because it is the modern > recommended API for python >= 2.7. I examined possibilty of using > optparse instead, but it lacks key features - in particular the idea > of 'subcommands' is entirely missing from optparse. > > The 'argparse' module is part of python core, but is *also* available > as a standalone module that is implemented in terms of optparse. > > So, I would suggest that we just copy the 'argparse' module into the > QEMU 'scripts/thirdparty' directory. > > Then in any files which need argparse, you can do this > > try: > import argparse > except: > import os, sys > sys.path.append(os.path.join(os.path.dirname(__file__), "thirdparty")) > import argparse
What about: try: import argparse except: from thirdparty import argparse (I think we could move all our Python modules [qemu.py, qmp.py] to ./scripts/qemu/, so this would become "qemu.thirdparty"). > > so that it tries to use the argparse provided by python, and falls back > to pulling in the one in our scripts/thirdparty directory. > > When we finally bump our min python to 2.7, we can simply drop this > compat code for the import statement. This avoids need for us to > re-write code to use a deprecated API, with a worse feature set. Sounds good to me. -- Eduardo