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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:54:16+00:00 2026-05-26T18:54:16+00:00

I’d like to use the ArgumentError exception in the argparse module in Python, but

  • 0

I’d like to use the ArgumentError exception in the argparse module in Python, but I can’t figure out how to use it. The signature says that it should be called as ArgumentError(argument, message), but I can’t figure out what argument should be. I think it should be some part of the parser object, but I couldn’t find any documentation for it.

  • 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-26T18:54:16+00:00Added an answer on May 26, 2026 at 6:54 pm

    From the source documentation:

    ArgumentError: The exception raised by ArgumentParser objects when there are errors with the parser’s actions. Errors raised while parsing the command-line are caught by ArgumentParser and emitted as command-line messages.

    The argument parameter of the constructor is the Action object from which the exception is raised. Usually there is no need to raise it outside of an Action subclass, and when subclassing it, there is no need to raise it explicitly; you usually raise ValueError (or whatever is appropriate) instead.

    Float values between 0 and 1

    In regards to your comment, that you only want to accept floating point values between 0 and 1. For this you should use the ability to define custom types. You can do it for example like this:

    def percentFloat (string):
        value = float(string)
        if value < 0 or value > 1:
            raise argparse.ArgumentTypeError('Value has to be between 0 and 1')
        return value
    
    parser = argparse.ArgumentParser()
    parser.add_argument('test', type=percentFloat)
    parser.parse_args()
    

    Note that this is also safe against non-floats as float(string) will raise a ValueError for non-floats which also triggers an invalid type error by the argparse module. ArgumentTypeError is just a way to specify a custom error message.

    Mutually exclusive parameters

    For mutually exclusive parameters, you should use argparse.add_mutually_exclusive_group.

    Parameter dependency

    Parameter dependency isn’t really something that should be done by the argument parser. For semantic details, you should instead simply do it yourself:

    args = parser.parse_args()
    if args.w and not args.p:
        parser.error('-p is required when -w is set.')
    

    You can use ArgumentParser.error to raise custom error message which will interrupt the program and print to the console.

    But of course in such a basic situation it would make a lot more sense just to implicitly guess -p if possible.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.