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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:37:55+00:00 2026-05-23T10:37:55+00:00

Using argparse, is there a way to accept a range of numbers and convert

  • 0

Using argparse, is there a way to accept a range of numbers and convert them into a list?

For example:

python example.py --range 0-5

Is there some way input a command line argument in that form and end up with:

args.range = [0,1,2,3,4,5]

And also have the possibility to input --range 2 = [2]?

  • 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-23T10:37:55+00:00Added an answer on May 23, 2026 at 10:37 am

    You could just write your own parser in the type argument, e.g.

    from argparse import ArgumentParser, ArgumentTypeError
    import re
    
    def parseNumList(string):
        m = re.match(r'(\d+)(?:-(\d+))?$', string)
        # ^ (or use .split('-'). anyway you like.)
        if not m:
            raise ArgumentTypeError("'" + string + "' is not a range of number. Expected forms like '0-5' or '2'.")
        start = m.group(1)
        end = m.group(2) or start
        return list(range(int(start,10), int(end,10)+1))
    
    parser = ArgumentParser()
    parser.add_argument('--range', type=parseNumList)
    
    args = parser.parse_args()
    print(args)
    
    ~$ python3 z.py --range m
    usage: z.py [-h] [--range RANGE]
    z.py: error: argument --range: 'm' is not a range of number. Expected forms like '0-5' or '2'.
    
    ~$ python3 z.py --range 2m
    usage: z.py [-h] [--range RANGE]
    z.py: error: argument --range: '2m' is not a range of number. Expected forms like '0-5' or '2'.
    
    ~$ python3 z.py --range 25
    Namespace(range=[25])
    
    ~$ python3 z.py --range 2-5
    Namespace(range=[2, 3, 4, 5])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
I'm using argparse in Python 2.7 for parsing input options. One of my options
I am using the argparse package of Python 2.7 to write some option-parsing logic
Using NLTK and WordNet , how do I convert simple tense verb into its
I've been using argparse for a Python program that can -process , -upload or
I am using argparse to handle cmd args, I wanna if there is no
I'm using argparse and I want to display a list in the help text
I am using argparse in my python program and I want to call a
I'm trying to pass a list of arguments to a python script using the
I need some help regarding using argparse. What I want to achieve is that

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.