Search code examples
pythonoptparse

Python optparse and spaces in an argument


When using optparse i want to get the whole string after an option, but I only get part of it up to the first space.

e.g.:

python myprog.py --executable python someOtherProg.py

What I get in 'executable' is just 'python'.

Is it possible to parse such lines using optparse or do you have to use argparse to do it?

€: I have already tried enclosing it in "s. But after digging further into the code I found out that the subprocess invocation can't handle the argument.

The string with the commandline gets crammed into a list 'args'.

args = [self.getExecutable()] + self.getArgs().split()

It's like

"[python D:\\\workspace\\\myprog\\\src\\\myprog.py]"

That gives me the System can't find file exception. When I use

args[0]

it works. But I loose the arguments to the executable.

The subprocess module builds a cmdline from a list if it does not get a string in the first place, so I can't explain that behavior at the moment.


Solution

  • You can enclose them in quotes to make them work with the existing code.

    python myprog.py --executable "python someOtherProg.py"
    

    Is it possible to parse such lines using optparse or do you have to use argparse to do it?

    I don't know if/how you can do it with optparse as I haven't really worked with optparse.

    I can however help you out with argparse. Here is a quick example:

    #!/usr/bin/python
    import argparse, sys
    
    if __name__ == '__main__':
        parser = argparse.ArgumentParser(description = 'Demonstration of Argparse.')
        parser.add_argument('-e', '--executable', nargs = '+', help = 'List of executables')
        args = parser.parse_args(sys.argv[1:])
        print args.executable
    

    And usage:

    manoj@maruti:~$ python myprog.py --executable python someOtherProg.py
    ['python', 'someOtherProg.py']
    

    I'd also recommend switching from optparse to argparse. Optparse is deprecated since 2.7.