Skip to content

Instantly share code, notes, and snippets.

@zacharysyoung
Created August 31, 2025 20:59
Show Gist options
  • Select an option

  • Save zacharysyoung/42b2e3ba44cedf1be0544f0bf095d915 to your computer and use it in GitHub Desktop.

Select an option

Save zacharysyoung/42b2e3ba44cedf1be0544f0bf095d915 to your computer and use it in GitHub Desktop.
SO-7869345
import argparse
def my_stop(args):
args.stop = True
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")
sp_stop.set_defaults(func=my_stop)
args = parser.parse_args()
args.func(args)
print(args.stop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment