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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:35:44+00:00 2026-05-27T00:35:44+00:00

I think this must be easy but I do not get it. Assume I

  • 0

I think this must be easy but I do not get it.

Assume I have the following arparse parser:

import argparse

parser = argparse.ArgumentParser( version='pyargparsetest 1.0' )
subparsers = parser.add_subparsers(help='commands')

# all
all_parser = subparsers.add_parser('all', help='process all apps')

# app
app_parser = subparsers.add_parser('app', help='process a single app')
app_parser.add_argument('appname', action='store', help='name of app to process')

How can I identify, which subparser was used?
calling:

print parser.parse_args(["all"])

gives me an empty namespace:

Namespace()
  • 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-27T00:35:44+00:00Added an answer on May 27, 2026 at 12:35 am

    Edit: Please see quornian’s answer to this question, which is better than mine and should be the accepted answer.

    According to the argparse documentation the result of parser.parse_args(...) will “only contain attributes for the main parser and the sub parser that was selected”. Unfortunately this may not be enough information to determine which sub parser was used. The documentation recommends using the set_defaults(...) method on the sub parser to solve this problem.

    For example, I’ve added calls to set_defaults() to your code:

    import argparse
    
    parser = argparse.ArgumentParser( version='pyargparsetest 1.0' )
    subparsers = parser.add_subparsers(help='commands')
    
    # all
    all_parser = subparsers.add_parser('all', help='process all apps')
    all_parser.set_defaults(which='all')
    
    # app
    app_parser = subparsers.add_parser('app', help='process a single app')
    app_parser.add_argument('appname', action='store', help='name of app to process')
    app_parser.set_defaults(which='app')
    

    Now if you run

    print parser.parse_args(["all"])
    

    The result is

    Namespace(which='all')
    

    Check out the add_subparsers() documentation for more information and another example.

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

Sidebar

Related Questions

I think this is a simple question, but I can not find the answer
Not sure exactly how to explain this but Imaging you have a table with
I must admin this is kind of funny even though I think I understand
I think this is likely to be a generic .NET assembly loading question, but
i think this is a bit of a noob question, but here goes. I
I think this is going to be one of those simple-when-you-see-it problems, but it
I know this probably is not the easiest thing to do, but I am
Im pretty sure the answer must be quite easy, but for some reason I
I know this is partially subjective, but hopefully with enough specifics I can get
This seems like it should be easy yet I must be missing something. I

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.