Python Language CLI subcommands with precise help output Native way (no libraries)

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

"""
usage: sub <command>

commands:

  status -  show status
  list   -  print list
"""

import sys

def check():
    print("status")
    return 0

if sys.argv[1:] == ['status']:
    sys.exit(check())
elif sys.argv[1:] == ['list']:
    print("list")
else:
    print(__doc__.strip())

Output without arguments:

usage: sub <command>

commands:

  status -  show status
  list   -  print list

Pros:

  • no deps
  • everybody should be able to read that
  • complete control over help formatting


Got any Python Language Question?