Package: typer-cli-forked
Typer CLIRunTyperscripts with completion, without having to create a package, usingTyper CLI.Note on the forkThis is a temporary fork of [https://pypi.org/project/typer-cli/] whilethis PRhas not been merged and released.For general uses, use the main typer-cli library.DescriptionThere is an optional utility tool calledTyper CLI, additional toTyperitself.It's main feature is to provide ✨ completion ✨ in the Terminal for your own small programs built withTyper....without you having to create a complete installable Python package.It's probably most useful if you have a small custom Python script usingTyper(maybe as part of some project), for some small tasks, and it's not complex/important enough to create a whole installable Python package for it (something to be installed withpip).In that case, you can installTyper CLI, and run your program with thetypercommand in your Terminal, and it will provide completion for your script.You can also useTyper CLIto generate Markdown documentation for your ownTyperprograms 📝.Documentation:https://typer.tiangolo.com/typer-cli/Source Code for Typer CLI:https://github.com/tiangolo/typer-cliTyperorTyper CLITyperis a library for building CLIs (Command Line Interface applications).You useTyperin your Python scripts. Like in:importtyperdefmain():typer.echo("Hello World")if__name__=="__main__":typer.run(main)Typer CLIis a command line application to run simple programs created withTyper, with completion in your terminal 🚀.You useTyper CLIin your terminal, to run your scripts (as an alternative to callingpythondirectly). Like in:$typermy_script.pyrunHello WorldBut you never import anything fromTyper CLIin your own scripts.UsageInstallInstallTyper CLI:$python-mpipinstalltyper-cli---> 100%Successfully installed typer-cliThat creates atypercommand you can call in your terminal, much likepython,git, orecho.You can then install completion for it:$typer--install-completionzsh completion installed in /home/user/.bashrc.Completion will take effect once you restart the terminal.Sample scriptLet's say you have a script that usesTyperinmy_custom_script.py:fromtypingimportOptionalimporttyperapp=typer.Typer()@app.command()defhello(name:Optional[str]=None):ifname:typer.echo(f"Hello{name}")else:typer.echo("Hello World!")@app.command()defbye(name:Optional[str]=None):ifname:typer.echo(f"Bye{name}")else:typer.echo("Goodbye!")if__name__=="__main__":app()For it to work, you would also installTyper:$python-mpipinstalltyper---> 100%Successfully installed typerRun with PythonThen you could run your script with normal Python:$pythonmy_custom_script.pyhelloHello World!$pythonmy_custom_script.pyhello--nameCamilaHello Camila!$pythonmy_custom_script.pybye--nameCamilaBye CamilaThere's nothing wrong with using Python directly to run it. And, in fact, if some other code or program uses your script, that would probably be the best way to do it.⛔️ But in your terminal, you won't get completion when hittingTABfor any of the subcommands or options, likehello,bye, and--name.Run withTyper CLIHere's whereTyper CLIis useful.You can also run the same script with thetypercommand you get after installingtyper-cli:$typermy_custom_script.pyrunhelloHello World!$typermy_custom_script.pyrunhello--nameCamilaHello Camila!$typermy_custom_script.pyrunbye--nameCamilaBye CamilaInstead of usingpythondirectly you use thetypercommand.After the name of the file, add the subcommandrun.✔️ If you installed completion forTyper CLI(for thetypercommand) as described above, when you hitTAByou will have ✨ completion for everything ✨, including all the subcommands and options of your script, likehello,bye, and--name🚀.If mainBecauseTyper CLIwon't use the block with:if__name__=="__main__":app()...you can also remove it if you are calling that script only withTyper CLI(using thetypercommand).Run other filesTyper CLIcan run any script withTyper, but the script doesn't even have to useTyperat all.Typer CLIcould even run a file with a function that could be used withtyper.run(), even if the script doesn't usetyper.run()or anything else.For example, a filemain.pylike this will still work:defmain(name:str="World"):"""Say hi to someone, by default to the World."""print(f"Hello{name}")Then you can call it with:$typermain.pyrun--helpUsage: typer run [OPTIONS]Say hi to someone, by default to the World.Options:--name TEXT--help       Show this message and exit.$typermain.pyrun--nameCamilaHello CamilaAnd it will also have completion for things like the--nameCLI Option.Run a package or moduleInstead of a file path you can pass a module (possibly in a package) to import.For example:$typermy_package.mainrun--helpUsage: typer run [OPTIONS]Options:--name TEXT--help       Show this message and exit.$typermy_package.mainrun--nameCamilaHello CamilaOptionsYou can specify one of the followingCLI options:--app: the name of the variable with aTyper()object to run as the main app.--func: the name of the variable with a function that would be used withtyper.run().DefaultsWhen your run a script with theTyper CLI(thetypercommand) it will use the app from the following priority:An app object from the--appCLI Option.A function to convert to aTyperapp from--funcCLI Option(like when usingtyper.run()).ATyperapp in a variable with a name ofapp,cli, ormain.The firstTyperapp available in the file, with any name.A function in a variable with a name ofmain,cli, orapp.The first function in the file, with any name.Generate docsTyper CLIcan also generate Markdown documentation for yourTyperapplication.Sample script with docsFor example, you could have a script like:importtyperapp=typer.Typer(help="Awesome CLI user manager.")@app.command()defcreate(username:str):"""Create a new user with USERNAME."""typer.echo(f"Creating user:{username}")@app.command()defdelete(username:str,force:bool=typer.Option(...,prompt="Are you sure you want to delete the user?",help="Force deletion without confirmation.",),):"""Delete a user with USERNAME.If --force is not used, will ask for confirmation."""ifforce:typer.echo(f"Deleting user:{username}")else:typer.echo("Operation cancelled")@app.command()defdelete_all(force:bool=typer.Option(...,prompt="Are you sure you want to delete ALL users?",help="Force deletion without confirmation.",)):"""Delete ALL users in the database.If --force is not used, will ask for confirmation."""ifforce:typer.echo("Deleting all users")else:typer.echo("Operation cancelled")@app.command()definit():"""Initialize the users database."""typer.echo("Initializing user database")if__name__=="__main__":app()Generate docs with Typer CLIThen you could generate docs for it withTyper CLI.You can use the subcommandutils.And then the subcommanddocs.$typersome_script.pyutilsdocsOptions:--name TEXT: The name of the CLI program to use in docs.--output FILE: An output file to write docs to, like README.md.For example:$typermy_package.mainutilsdocs--nameawesome-cli--outputREADME.mdDocs saved to: README.mdSample docs outputFor example, for the previous script, the generated docs would look like:awesome-cliAwesome CLI user manager.Usage:$awesome-cli[OPTIONS]COMMAND[ARGS]...Options:--install-completion: Install completion for the current shell.--show-completion: Show completion for the current shell, to copy it or customize the installation.--help: Show this message and exit.Commands:create: Create a new user with USERNAME.delete: Delete a user with USERNAME.delete-all: Delete ALL users in the database.init: Initialize the users database.awesome-cli createCreate a new user with USERNAME.Usage:$awesome-clicreate[OPTIONS]USERNAMEOptions:--help: Show this message and exit.awesome-cli deleteDelete a user with USERNAME.If --force is not used, will ask for confirmation.Usage:$awesome-clidelete[OPTIONS]USERNAMEOptions:--force / --no-force: Force deletion without confirmation.  [required]--help: Show this message and exit.awesome-cli delete-allDelete ALL users in the database.If --force is not used, will ask for confirmation.Usage:$awesome-clidelete-all[OPTIONS]Options:--force / --no-force: Force deletion without confirmation.  [required]--help: Show this message and exit.awesome-cli initInitialize the users database.Usage:$awesome-cliinit[OPTIONS]Options:--help: Show this message and exit.LicenseTyper CLI, the same asTyper, is licensed under the terms of the MIT license.