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

  • Home
  • SEARCH
  • 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 6989373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:12:12+00:00 2026-05-27T19:12:12+00:00

Python2.7 argparse only accepts optional arguments (prefixed) in mutually exclusive groups: parser = argparse.ArgumentParser(prog=’mydaemon’)

  • 0

Python2.7 argparse only accepts optional arguments (prefixed) in mutually exclusive groups:

parser = argparse.ArgumentParser(prog='mydaemon')
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument('--start', action='store_true', help='Starts %(prog)s daemon')
action.add_argument('--stop', action='store_true', help='Stops %(prog)s daemon')
action.add_argument('--restart', action='store_true', help='Restarts %(prog)s daemon')

$ mydaemon -h

usage: mydaemon [-h] (--start | --stop | --restart)

optional arguments:
  -h, --help  show this help message and exit
  --start     Starts mydaemon daemon
  --stop      Stops mydaemon daemon
  --restart   Restarts mydaemon daemon

Is there a way to make argparse arguments behaves like traditional unix daemon control:

(start | stop | restart) and not (--start | --stop | --restart) ?
  • 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-27T19:12:13+00:00Added an answer on May 27, 2026 at 7:12 pm

    For all the abilities and options in argparse I don’t think you’ll ever get a “canned” usage string that looks like what you want.

    That said, have you looked at sub-parsers since your original post?

    Here’s a barebones implementation:

    import argparse
    
    parser = argparse.ArgumentParser(prog='mydaemon')
    sp = parser.add_subparsers()
    sp_start = sp.add_parser('start', help='Starts %(prog)s daemon')
    sp_stop = sp.add_parser('stop', help='Stops %(prog)s daemon')
    sp_restart = sp.add_parser('restart', help='Restarts %(prog)s daemon')
    
    parser.parse_args()
    

    Running this with the -h option yields:

    usage: mydaemon [-h] {start,stop,restart} ...
    
    positional arguments:
      {start,stop,restart}
        start               Starts mydaemon daemon
        stop                Stops mydaemon daemon
        restart             Restarts mydaemon daemon
    

    One of the benefits of this approach is being able to use set_defaults for each sub-parser to hook up a function directly to the argument. I’ve also added a “graceful” option for stop and restart:

    import argparse
    
    def my_stop(args):
        if args.gracefully:
            print "Let's try to stop..."
        else:
            print 'Stop, now!'
    
    parser = argparse.ArgumentParser(prog='mydaemon')
    
    graceful = argparse.ArgumentParser(add_help=False)
    graceful.add_argument('-g', '--gracefully', action='store_true', help='tries to terminate the process gracefully')
    sp = parser.add_subparsers()
    sp_start = sp.add_parser('start', help='Starts %(prog)s daemon')
    sp_stop = sp.add_parser('stop', parents=[graceful],
                        description='Stops the daemon if it is currently running.',
                        help='Stops %(prog)s daemon')
    sp_restart = sp.add_parser('restart', parents=[graceful], help='Restarts %(prog)s daemon')
    
    # Hook subparsers up to functions
    sp_stop.set_defaults(func=my_stop)
    
    # Uncomment when my_start() and 
    # my_restart() are implemented
    #
    # sp_start.set_defaults(func=my_start)
    # sp_restart.set_defaults(func=my_restart)
    
    args = parser.parse_args()
    args.func(args)
    

    Showing the “help” message for stop:

    $ python mydaemon.py stop -h
    usage: mydaemon stop [-h] [-g]
    
    Stops the daemon if it is currently running.
    
    optional arguments:
      -h, --help        show this help message and exit
      -g, --gracefully  tries to terminate the process gracefully
    

    Stopping “gracefully”:

    $ python mydaemon.py stop -g
    Let's try to stop...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using argparse in python to parse commandline arguments: parser = ArgumentParser() parser.add_argument(--a) parser.add_argument(--b)
I am trying to write a parser class derived from the Python argparse ArgumentParser
When I use subcommands with python argparse, I can get the selected arguments. parser
In python2.7, the argparse module has an add_argument method which can take a variable
I am using python argparse with the following argument definition: parser.add_argument('path', nargs=1, help='File path
Using Python2.4 I want to capture output from a mysql command. One caveat is
How can use Python2.5 with to write scripts in vim? I'm using vim 7.2
I'm running python2.5 and trying to use the astLib library to analyse WCS information
Or should I just stick with Python2.5 for a bit longer?
As some of you may know in python2.7/3.2 we'll get OrderedDict with PEP372 however

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.