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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:40:55+00:00 2026-05-25T17:40:55+00:00

My script defines one main parser and multiple subparsers. I want to apply the

  • 0

My script defines one main parser and multiple subparsers. I want to apply the -p argument to some subparsers. So far the code looks like this:

parser = argparse.ArgumentParser(prog="myProg")
subparsers = parser.add_subparsers(title="actions")

parser.add_argument("-v", "--verbose",
                    action="store_true",
                    dest="VERBOSE",
                    help="run in verbose mode")

parser_create = subparsers.add_parser ("create", 
                                        help = "create the orbix environment")
parser_create.add_argument ("-p", 
                            type = int, 
                            required = True, 
                            help = "set db parameter")

# Update
parser_update = subparsers.add_parser ("update", 
                                        help = "update the orbix environment")
parser_update.add_argument ("-p", 
                            type = int, 
                            required = True, 
                            help = "set db parameter")

As you can see the add_arument ("-p") is repeated twice. I actually have a lot more subparsers. Is there a way to loop through the existing subparsers in order to avoid repetition?

For the record, I am using Python 2.7

  • 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-25T17:40:55+00:00Added an answer on May 25, 2026 at 5:40 pm

    Update by @hpaulj

    Due to changes in handling subparsers since 2011, it is a bad idea to use the main parser as a parent. More generally, don’t try to define the same argument (same dest) in both main and sub parsers. The subparser values will overwrite anything set by the main (even the subparser default does this). Create separate parser(s) to use as parents. And as shown in the documentation, parents should use add_help=False.


    Original answer

    This can be achieved by defining a parent parser containing the common option(s):

    import argparse
    
    parent_parser = argparse.ArgumentParser(description="The parent parser")
    parent_parser.add_argument("-p", type=int, required=True,
                               help="set db parameter")
    subparsers = parent_parser.add_subparsers(title="actions")
    parser_create = subparsers.add_parser("create", parents=[parent_parser],
                                          add_help=False,
                                          description="The create parser",
                                          help="create the orbix environment")
    parser_create.add_argument("--name", help="name of the environment")
    parser_update = subparsers.add_parser("update", parents=[parent_parser],
                                          add_help=False,
                                          description="The update parser",
                                          help="update the orbix environment")
    

    This produces help messages of the format:

    parent_parser.print_help()
    

    Output:

    usage: main.py [-h] -p P {create,update} ...
    The parent parser
    optional arguments:
      -h, --help       show this help message and exit
      -p P             set db parameter
    actions:
      {create,update}
        create         create the orbix environment
        update         update the orbix environment
    
    parser_create.print_help()
    

    Output:

    usage: main.py create [-h] -p P [--name NAME] {create,update} ...
    The create parser
    optional arguments:
      -h, --help       show this help message and exit
      -p P             set db parameter
      --name NAME      name of the environment
    actions:
      {create,update}
        create         create the orbix environment
        update         update the orbix environment
    

    However, if you run your program, you will not encounter an error if you do not specify an action (i.e. create or update). If you desire this behavior, modify your code as follows.

    <...>
    subparsers = parent_parser.add_subparsers(title="actions")
    subparsers.required = True
    subparsers.dest = 'command'
    <...>
    

    This fix was brought up in this SO question which refers to an issue tracking a pull request.

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

Sidebar

Related Questions

I am trying to write a Ruby script in one file. I would like
I desperately need some help on this one. I've created a <script> that closely
When using Android libraries if more than one project (main or libraries) defines the
I have my main initialization script which calls require() and one of the dependencies
I want my script to define an empty array. array values should be added
I have a script which creates a user-defined object like this: obj = new
I'm trying to make a little client-server script like many others that I've done
I have a simple script that should cause one of three divs to be
I have a project with one main Cocoa application, a bunch of plugins, and
I get the following error, when i run my script. fetch_scores.rb:20:in `<main>': undefined local

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.