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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:05:20+00:00 2026-06-11T08:05:20+00:00

I would like to be able to use the global options from an argparse.ArgumentParser

  • 0

I would like to be able to use the global options from an argparse.ArgumentParser object to override/augment the defaults values for a sub-command.

An example would be that displayed help reflects the global updates, i.e., for the following toy example:

import argparse
import os
import sys

# Global parser and options.
parser = argparse.ArgumentParser(
    formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument("--user", 
                    dest="user", 
                    default=os.environ.get("USER"),
                    help="Override the setting of the $USER variable.")

# Sub-command parser and options.
subparsers = parser.add_subparsers()

command = subparsers.add_parser(
    "command",
    formatter_class=argparse.ArgumentDefaultsHelpFormatter)

command.add_argument("--config",
                     dest="config",
                     default="~%s/config" % os.environ.get("USER"),
                     help="The config file.")

options = parser.parse_args()

Ideally when when I run this in help mode I would get,

> python example.py --user thing command --help
usage: example.py command [-h] [--config CONFIG]

optional arguments:
  -h, --help       show this help message and exit
  --config CONFIG  The config file. (default: ~thing/config)

i.e., the config file path is user specific (thing). I realize that I could change the default config to be "~%(user)s/config" and then resolve this at run-time with the options namespace, however I would like the help to be more explicit.

I gather an alternative solution would be to try to parse the arguments once to obtain the global options, i.e.,

if "--help" in sys.argv:

    # Parse the command minus the help to obtain the global options. 
    args = sys.argv[1:]
    args.remove("--help")

    # Update the defaults with the global options.
    options = parser.parse_args(args) 
    command.set_defaults(config="~%s/config" % options.user)

    # Re-parse the options.
    parser.parse_args()

Though this seems somewhat hacky. Is there a better approach/solution?

  • 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-11T08:05:21+00:00Added an answer on June 11, 2026 at 8:05 am

    After defining the global options, but before defining the subcommands, call parse_known_args to find out what the value of --user is. Then, finish defining the subparser commands, using the value of --user to define the default of --config, before
    calling parse_args to parse all options on the command line.

    This is a little different from your alternative, but it keeps all the command-line processing inside the argparse object.

    (I trimmed down your code a little just to shorten it for this answer.)

    import os
    import argparse
    
    # Global preparser and options.
    preparser = argparse.ArgumentParser(add_help=False)
    preparser.add_argument("--user", dest="user", default=os.environ.get("USER"))
    
    # ****** NEW *******
    options, _ = preparser.parse_known_args() # Ignore what we haven't defined yet
    user = options.user                       # Use this to define the default of --config
    
    parser = argparse.ArgumentParser(parents=[ preparser ], add_help=True,
                       formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    
    # Sub-command parser and options.
    subparsers = parser.add_subparsers()
    
    command = subparsers.add_parser("command")
    
    # ****** MODIFIED *******
    command.add_argument("--config", dest="config", default="~%s/config" % (user,))
    
    options = parser.parse_args()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to use the same Session variable when transferring
I would like to be able to use the same drawable to represent both:
I would like to be able to use the created functions and objects I
I'm new to the iPhone and I would like to be able to use
This is really just for my own use: I would like to be able
I would like to understand why you might want to use the global:: prefix.
Would like to be able to set colors of headings and such, different font
I would like to be able to output the name of a variable, along
I would like to be able to print in the logs a message for
I would like to be able to show a personal message to abusers of

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.