Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1106621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:49:15+00:00 2026-05-17T01:49:15+00:00

When using optparse i want to get the whole string after an option, but

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:49:16+00:00Added an answer on May 17, 2026 at 1:49 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.