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 148925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:02:43+00:00 2026-05-11T09:02:43+00:00

What would be an easy expression to process command line arguments if I’m expecting

  • 0

What would be an easy expression to process command line arguments if I’m expecting anything like 001 or 999 (let’s limit expectations to 001…999 range for this time), and few other arguments passed, and would like to ignore any unexpected?

I understand if for example I need to find out if ‘debug’ was passed among parameters it’ll be something like that:

if 'debug' in argv[1:]:   print 'Will be running in debug mode.' 

How to find out if 009 or 575 was passed?

All those are expected calls:

python script.py python script.py 011 python script.py 256 debug python script.py 391 xls python script.py 999 debug pdf 

At this point I don’t care about calls like that:

python script.py 001 002 245 568 python script.py some unexpected argument python script.py 0001 python script.py 02 

…first one – because of more than one ‘numeric’ argument; second – because of… well, unexpected arguments; third and fourth – because of non-3-digits arguments.

  • 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. 2026-05-11T09:02:44+00:00Added an answer on May 11, 2026 at 9:02 am

    As others answered, optparse is the best option, but if you just want quick code try something like this:

    import sys, re  first_re = re.compile(r'^\d{3}$')  if len(sys.argv) > 1:      if first_re.match(sys.argv[1]):         print 'Primary argument is : ', sys.argv[1]     else:         raise ValueError('First argument should be ...')      args = sys.argv[2:]  else:      args = ()  # ... anywhere in code ...  if 'debug' in args:     print 'debug flag'  if 'xls' in args:     print 'xls flag' 

    EDIT: Here’s an optparse example because so many people are answering optparse without really explaining why, or explaining what you have to change to make it work.

    The primary reason to use optparse is it gives you more flexibility for expansion later, and gives you more flexibility on the command line. In other words, your options can appear in any order and usage messages are generated automatically. However to make it work with optparse you need to change your specifications to put ‘-‘ or ‘–‘ in front of the optional arguments and you need to allow all the arguments to be in any order.

    So here’s an example using optparse:

    import sys, re, optparse  first_re = re.compile(r'^\d{3}$')  parser = optparse.OptionParser() parser.set_defaults(debug=False,xls=False) parser.add_option('--debug', action='store_true', dest='debug') parser.add_option('--xls', action='store_true', dest='xls') (options, args) = parser.parse_args()  if len(args) == 1:     if first_re.match(args[0]):         print 'Primary argument is : ', args[0]     else:         raise ValueError('First argument should be ...') elif len(args) > 1:     raise ValueError('Too many command line arguments')  if options.debug:     print 'debug flag'  if options.xls:     print 'xls flag' 

    The differences here with optparse and your spec is that now you can have command lines like:

    python script.py --debug --xls 001 

    and you can easily add new options by calling parser.add_option()

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

Sidebar

Ask A Question

Stats

  • Questions 147k
  • Answers 147k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer declare @s varchar(50); set @s = 'aaaaa/bbbbb/ccccc/ddddd/eeeee' /* last one:… May 12, 2026 at 9:12 am
  • Editorial Team
    Editorial Team added an answer Don't use in-place sed. Instead: gentest_SOURCES = gentest-seded.cc gentest-seded.cc :… May 12, 2026 at 9:12 am
  • Editorial Team
    Editorial Team added an answer Are you loading the partial via AJAX? If so, put… May 12, 2026 at 9:12 am

Related Questions

What would be an easy expression to process command line arguments if I'm expecting
If I need generate a fairly large dataset using LINQ and it may take
In my code I'd like to make my repositories IQueryable. This way, the criteria
I need to extract the src element from all image tags in an HTML
i'm using JCL's expression evaluator TEvaluator (a marvelous creation donated by barry kelly). (THANK

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.