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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:23:10+00:00 2026-06-04T00:23:10+00:00

I am implementing a command line program which has interface like this: cmd [GLOBAL_OPTIONS]

  • 0

I am implementing a command line program which has interface like this:

cmd [GLOBAL_OPTIONS] {command [COMMAND_OPTS]} [{command [COMMAND_OPTS]} ...]

I have gone through the argparse documentation. I can implement GLOBAL_OPTIONS as optional argument using add_argument in argparse. And the {command [COMMAND_OPTS]} using Sub-commands.

From the documentation it seems I can have only one sub-command. But as you can see I have to implement one or more sub-commands. What is the best way to parse such command line arguments useing argparse?

  • 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-04T00:23:13+00:00Added an answer on June 4, 2026 at 12:23 am

    @mgilson has a nice answer to this question. But problem with splitting sys.argv myself is that I lose the nice help message Argparse generates for the user. So I ended up doing this:

    import argparse
    
    ## This function takes the 'extra' attribute from global namespace and
    ## re-parses it to create separate namespaces for all other chained commands.
    def parse_extra (parser, namespace):
      namespaces = []
      extra = namespace.extra
      while extra:
        n = parser.parse_args(extra)
        extra = n.extra
        namespaces.append(n)
    
      return namespaces
    
    argparser=argparse.ArgumentParser()
    subparsers = argparser.add_subparsers(help='sub-command help', dest='subparser_name')
    
    parser_a = subparsers.add_parser('command_a', help = "command_a help")
    ## Setup options for parser_a
    
    ## Add nargs="*" for zero or more other commands
    argparser.add_argument('extra', nargs = "*", help = 'Other commands')
    
    ## Do similar stuff for other sub-parsers
    

    Now after first parse all chained commands are stored in extra. I reparse it while it is not empty to get all the chained commands and create separate namespaces for them. And i get nicer usage string that argparse generates.

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

Sidebar

Related Questions

I would like to implement a command line interface for a Java application. This
I'm writing a program which takes filenames and options on the command line in
Now, I'm developing a command-line application somewhat like bash. For implementing the autocomplete functionality,
My application has a command line interface, and I'm thinking about using the GNU
I'm writing a Java class that's implementing an interface called Command, which contains the
I need to make a program, ioloop.c, which takes two command line parameters that
When implementing the ISerializable interface in C#, we provide a constructor which takes a
Am working on a command line python script - throughout the script, I have
I have a command line Ruby app I'm developing and I want to allow
I'm trying to develop a command line interface for a tool I'm writing. 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.