about things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix argparse example to match actual pdsx code

- use tuple membership dispatch, not match/case
- add RawDescriptionHelpFormatter
- add async main pattern with sys.exit(asyncio.run())
- link to actual source

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

zzstoatzz 44b993de a97b8340

+31 -16
+31 -16
languages/python/patterns.md
··· 183 183 184 184 ## argparse for CLIs 185 185 186 - argparse is stdlib and sufficient for most CLIs. subparsers give you git-style commands: 186 + argparse is stdlib. subparsers with aliases give you unix-style commands: 187 187 188 188 ```python 189 - import argparse 189 + parser = argparse.ArgumentParser( 190 + description="my tool", 191 + formatter_class=argparse.RawDescriptionHelpFormatter, 192 + ) 193 + parser.add_argument("-r", "--repo", help="target repo") 190 194 191 - def main() -> int: 192 - parser = argparse.ArgumentParser(description="my tool") 193 - subparsers = parser.add_subparsers(dest="command") 195 + subparsers = parser.add_subparsers(dest="command") 194 196 195 - list_parser = subparsers.add_parser("list", aliases=["ls"]) 196 - list_parser.add_argument("collection") 197 - list_parser.add_argument("--limit", type=int, default=50) 197 + list_parser = subparsers.add_parser("list", aliases=["ls"]) 198 + list_parser.add_argument("collection") 199 + list_parser.add_argument("--limit", type=int, default=50) 200 + ``` 201 + 202 + dispatch with tuple membership to handle aliases: 203 + 204 + ```python 205 + args = parser.parse_args() 206 + 207 + if not args.command: 208 + parser.print_help() 209 + return 1 198 210 199 - args = parser.parse_args() 211 + if args.command in ("list", "ls"): 212 + return await cmd_list(args.collection, args.limit) 213 + ``` 200 214 201 - match args.command: 202 - case "list" | "ls": 203 - return do_list(args.collection, args.limit) 204 - case _: 205 - parser.print_help() 206 - return 1 215 + async entry point pattern: 207 216 217 + ```python 218 + async def async_main() -> int: 219 + # argparse and dispatch here 208 220 return 0 221 + 222 + def main() -> NoReturn: 223 + sys.exit(asyncio.run(async_main())) 209 224 ``` 210 225 211 - the pattern: parse once at the top, dispatch based on command, return exit codes. 226 + source: [pdsx/src/pdsx/cli.py](https://github.com/zzstoatzz/pdsx/blob/main/src/pdsx/cli.py) 212 227 213 228 ## module-level singletons 214 229