One file per command without globals #1127
ckutlu
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there! I recently started using
typer
extensively and so far very happy with all the bells and whistles it provides. Great work!Oftentimes I often write small CLI tools to make my life easier and use subcommands to provide categories of things that go well together. I started having a pattern for having one file per command in my projects that deviates a bit from the recommended way of doing it in the documentation (here). It began from my desire to avoid having typer app created at the top-level in modules mainly becase I may want to pass an argument to
typer.Typer(...)
and I may not want that argument to live in the global scope.This is how my folder structure looks like:
Contents of
__init__.py
:This tells me that every subcommand file I add should have a create function defined with the given signature. The type checking tool I use even flags where _ALL_SUBAPPS is defined if one of the subcommand modules does not comply to the protocol.
Contents of subcommand modules look like this:
Let me know what you think! If you find this useful and worthy of mentioning in the documentation, I'd be happy to make a PR for the docs at some point.
Cheers
Bonus: Testing
mytool subcommand --help
works for all subcommandsI added a convenience function to the
__init__
:This helped me write a simple integration test checking that at least "mytool subcommand{1,2} --help" works.
Beta Was this translation helpful? Give feedback.
All reactions