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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:07:35+00:00 2026-06-07T04:07:35+00:00

Currently I want Python’s argparse module to only print out ‘1 – 65535’ rather

  • 0

Currently I want Python’s argparse module to only print out ‘1 – 65535’ rather than {1, 2, 3, … 65535}, but the documentation doesn’t seem to provide any method for this. Any suggestions?

  • 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-07T04:07:36+00:00Added an answer on June 7, 2026 at 4:07 am

    You can alter the way defaults are formatted by setting the formatter_class option.

    I’d subclass the HelpFormatter class to alter the way it formats your choices values. This class is officially an “implementation detail” but I doubt it’ll change much with newer python versions.

    The _metavar_formatter method formats the {1, 2, ..., 65535} string and your subclass could override that:

    class RangeChoiceHelpFormatter(HelpFormatter):
        def _metavar_formatter(self, action, default_metavar):
             if action.metavar is not None:
                 result = action.metavar
             elif action.choices is not None:
                 result = '{%s .. %s}' % (min(action.choices), max(action.choices])
             else:
                 result = default_metavar
    
              def format(tuple_size):
                  if isinstance(result, tuple):
                      return result
                  else:
                      return (result, ) * tuple_size
              return format
    

    Another option is to not use the choices argument for such a large range, and instead define a new argument type.

    This is just a callable, passed a string, that raises argparse.ArgumentTypeError, TypeError or ValueError if the string cannot be converted to the target type, or the converted value otherwise:

    class IntRange(object):
        def __init__(self, start, stop=None):
            if stop is None:
                start, stop = 0, start
            self.start, self.stop = start, stop
    
        def __call__(self, value):
            value = int(value)
            if value < self.start or value >= self.stop:
                raise argparse.ArgumentTypeError('value outside of range')
            return value
    

    You can use this as the argument type like this:

    parser.add_argument('foo', type=IntRange(1, 65536))
    

    and adjust your help message to indicate what values are acceptable.

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

Sidebar

Related Questions

I want to add a parameter to a URL but currently it isnt showing
I'm currently building a Python webapp on the Google App Engine and I want
I currently use a C extension library for Python, but it seems to have
I'm currently trying to do some introspection on a DLL with python. I want
I'm currently teaching myself python (coming from no programming background, but a lot of
Occasionally I want lazy module loading in Python. Usually because I want to keep
I'm currently writing a serialization module in Python that can serialize user defined classes.
I currently have a Java application running on Google App Engine, but I want
I currently want to make my project accessible not only via back-end code (C#,
I'm currently learning Python and I'm trying to make a small scraper but I'm

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.