Package: django-typer
django-typerUseTyperto define the CLI for yourDjangomanagement commands.
Provides a TyperCommand class that inherits fromBaseCommandand allows typer-style annotated parameter types. All of the BaseCommand functionality is
preserved, so that TyperCommand can be a drop in replacement.django-typer makes it easy to:Define your command CLI interface in a clear, DRY and safe way using type hintsCreate subcommand and group command hierarchies.Use the full power of Typer’s parameter types to validate and parse command line inputs.Create beautiful and information dense help outputs.Configure the rendering of exception stack traces using rich.Install shell tab-completion supportfor TyperCommands and normal Django commands forbash,zsh,fishandpowershell.Create custom and portable shell tab-completions for your CLI parameters.Refactor existing management commands into TyperCommands because TyperCommand is interface
compatible with BaseCommand.Please refer to thefull documentationfor more information.InstallationClone django-typer from GitHub or install a release offPyPI:pipinstalldjango-typerrichis a powerful library for rich text and
beautiful formatting in the terminal. It is not required, but highly recommended for the
best experience:pipinstall"django-typer[rich]"Adddjango_typerto yourINSTALLED_APPSsetting:INSTALLED_APPS=[...'django_typer',]Basic ExampleFor example TyperCommands can be a very simple drop in replacement for BaseCommands. All of the
documented features ofBaseCommandwork!fromdjango_typerimportTyperCommandclassCommand(TyperCommand):defhandle(self,arg1:str,arg2:str,arg3:float=0.5,arg4:int=1):"""
      A basic command that uses Typer
      """Multiple Subcommands ExampleOr commands with multiple subcommands can be defined:importtypingastfromdjango.utils.translationimportgettext_lazyas_fromtyperimportArgumentfromdjango_typerimportTyperCommand,commandclassCommand(TyperCommand):"""
   A command that defines subcommands.
   """@command()defcreate(self,name:t.Annotated[str,Argument(help=_("The name of the object to create."))],):"""
      Create an object.
      """...@command()defdelete(self,id:t.Annotated[int,Argument(help=_("The id of the object to delete."))]):"""
      Delete an object.
      """...Grouping and Hierarchies ExampleOr more complex groups and subcommand hierarchies can be defined. For example this command
defines a group of commands called math, with subcommands divide and multiply. The group
has a common initializer that optionally sets a float precision value. We would invoke this
command like so:./manage.pyhierarchymath--precision5divide102.14.76190./manage.pyhierarchymathmultiply10220.00Any number of groups and subcommands and subgroups of other groups can be defined allowing
for arbitrarily complex command hierarchies.importtypingastfromfunctoolsimportreducefromdjango.utils.translationimportgettext_lazyas_fromtyperimportArgument,Optionfromdjango_typerimportTyperCommand,groupclassCommand(TyperCommand):help=_("A more complex command that defines a hierarchy of subcommands.")precision=2@group(help=_("Do some math at the given precision."))defmath(self,precision:t.Annotated[int,Option(help=_("The number of decimal places to output."))]=precision,):self.precision=precision@math.command(help=_("Multiply the given numbers."))defmultiply(self,numbers:t.Annotated[t.List[float],Argument(help=_("The numbers to multiply"))],):ifnumbers:returnf"{reduce(lambdax,y:x*y,[1,*numbers]):.{self.precision}f}"@math.command()defdivide(self,numerator:t.Annotated[float,Argument(help=_("The numerator"))],denominator:t.Annotated[float,Argument(help=_("The denominator"))],floor:t.Annotated[bool,Option(help=_("Use floor division"))]=False,):"""
      Divide the given numbers.
      """iffloor:returnstr(numerator//denominator)returnf"{numerator/denominator:.{self.precision}f}"