]> xn--ix-yja.es Git - alex.git/commitdiff
Add advice about subprocess
authoralex <alex@pdp7.net>
Fri, 22 Apr 2022 18:49:46 +0000 (20:49 +0200)
committeralex <alex@pdp7.net>
Fri, 22 Apr 2022 18:49:46 +0000 (20:49 +0200)
programming/python/creating_nice_python_cli_tools.md

index d57eb0ebbb0c196e035d107c469f33ef7eb10a95..6f4663892c67cbbd1bd1735d951ec9e2c3518835 100644 (file)
   It has decent support for [sub-commands](https://docs.python.org/3/library/argparse.html#sub-commands), and the linked document describes a very nice pattern to define functions for sub-commands, under "One particularly effective way of handling sub-commands..."
 * Remember that the standard [json](https://docs.python.org/3/library/json.html) module is built-in.
   You can use it to add a mode to your tool that generates JSON output instead of human-readable output, for easy automation of your tool, maybe using [jq](https://stedolan.github.io/jq/) or [fx](https://github.com/antonmedv/fx).
+* Use the standard [subprocess](https://docs.python.org/3/library/subprocess.html) module to execute other commands.
+  * Remember never to use `shell=True`, so among other things, your tool will work correctly with files using spaces in their names.
+  * Use `check=True` so if the subprocess fails, an exception will be raised.
+    This is likely the best default behavior, although the error is a bit ugly, this normally prevents ugly problems and it's a safe option.
+
 
 You can find examples for many of those techniques in my [repos](https://github.com/alexpdp7?tab=repositories&q=&type=&language=python&sort=).