Why might you choose `click` (or `typer`) over `argparse`?
IntermediateConceptOS, filesystem, subprocess & CLIs
Answer
click gives decorators for options and commands, nested command groups, envvar= defaults, type=click.Choice([...]) and click.Path(exists=True) validation, prompts and confirmation (@click.confirmation_option), colored output, and click.testing.CliRunner for tests without spawning processes. typer builds on click using type hints.
@click.command()
@click.option("--region", envvar="AWS_REGION", required=True)
@click.option("--dry-run/--no-dry-run", default=True)
def main(region, dry_run): ...
Trade-off: a third-party dependency, which matters for scripts dropped onto bare servers.
More on OS, filesystem, subprocess & CLIs
`os.path` vs `pathlib`: which do you use and why?BeginnerHow do you read environment variables safely?BeginnerHow do you walk a directory tree and find files?BeginnerWhat does `shutil` give you that `os` does not?BeginnerHow do you run an external command correctly with `subprocess`?BeginnerWhy is `shell=True` dangerous, and when is it acceptable?Intermediate