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

  • SEARCH
  • Home
  • 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 8728389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:37:47+00:00 2026-06-13T08:37:47+00:00

Problem: Need to parse some specific arguments which could be in any order, non

  • 0

Problem: Need to parse some specific arguments which could be in any order, non are optional: -h -d -src -dst

Am new to Python and have looked at the alternatives such as getopt and argparse but couldn’t get a working example so went custom as below;

argv=sys.argv[1:]
args=[]
for idx, arg in enumerate(argv):
    # if is arg
    if arg.startswith("-"):
        # find arg match
        for i in ("-h","-d:","-src:","-dst:"):
            # requires var
            if i == arg + ':' and idx < len(argv)-1:
                if not argv[idx+1].startswith("-"):
                    args.append((arg,argv[idx+1]))
                    break
            # no var
            elif i == arg:
                args.append((arg,""))
                break
    else:
        continue
# may contain duplicates
print(args)
# no dupes
print(set(args))

Can anyone suggest improvements and/or better examples to achieve the problem objective?

  • 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-06-13T08:37:48+00:00Added an answer on June 13, 2026 at 8:37 am

    in python 2.6 there is a module called optparse

    which does what you want.

    example from the docs:

    from optparse import OptionParser
    [...]
    parser = OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                      help="write report to FILE", metavar="FILE")
    parser.add_option("-q", "--quiet",
                      action="store_false", dest="verbose", default=True,
                      help="don't print status messages to stdout")
    
    (options, args) = parser.parse_args()
    

    another example:

    usage = "usage: %prog [options] arg1 arg2"
    parser = OptionParser(usage=usage)
    parser.add_option("-v", "--verbose",
                      action="store_true", dest="verbose", default=True,
                      help="make lots of noise [default]")
    parser.add_option("-q", "--quiet",
                      action="store_false", dest="verbose",
                      help="be vewwy quiet (I'm hunting wabbits)")
    parser.add_option("-f", "--filename",
                      metavar="FILE", help="write output to FILE")
    parser.add_option("-m", "--mode",
                      default="intermediate",
                      help="interaction mode: novice, intermediate, "
                           "or expert [default: %default]")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have some problem with utilizing TLF, i need to parse through the text
I need some suggestion on how to go about this specific android problem: activity
I have a problem right now, I need to parse an XML but some
I need to load and parse some JSON in flash, AS2. The problem is
Short explanation of my problem, I need to parse an object and put its
To solve some problem I need to compute a variant of the pascal's triangle
i got some problem and need help .. my plan : 1. get ip
I have multiple CSV files which I need to parse in a loop to
I'm trying to parse some information from an HTML page. The only problem is
I need to parse some XML that triggers an NSURLConnection. After the parsing finished,

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.