Python Tip #120 (of 365):
-
Python Tip #120 (of 365):
Optimize for CLI user experience, not just developer experience
By default in argparse, the names that end users see for arguments are coupled to the attribute names that you as a Python script maintainer see.
Decouple them when needed.
You can control the argument names that a user sees with metavar:
This uses the attribute args.path but show users FILENAME in help text:
parser.add_argument("path", type=Path, metavar="FILENAME")
🧵 (1/2)
-
Python Tip #120 (of 365):
Optimize for CLI user experience, not just developer experience
By default in argparse, the names that end users see for arguments are coupled to the attribute names that you as a Python script maintainer see.
Decouple them when needed.
You can control the argument names that a user sees with metavar:
This uses the attribute args.path but show users FILENAME in help text:
parser.add_argument("path", type=Path, metavar="FILENAME")
🧵 (1/2)
Also consider adding a description to describe your script and help to each argument:
parser = argparse.ArgumentParser(
description="Compute emoji-hash for input text/file",
)
parser.add_argument(
"path",
type=Path,
metavar="FILENAME",
help="Input file to emoji-hash",
)Developer experience DOES matter but don't forget the users.
You're welcome to leave these customizations to the finishing touches of your Python CLI, but don't forget them!
🧵 (2/2)
-
R relay@relay.infosec.exchange shared this topic