Loading video player…

Introducing Typer

00:00 Before building CLI apps with Typer, it’s worth understanding the basics of Typer. That’s what you get in this lesson, a type of crash course if you will.

00:08 But if you already know the basics of Typer, you can skip this lesson and move to the next one where you start creating the basic structure for your final to-do CLI application. You can start with creating a “Hello World” app with Typer.

00:22 Now in the current directory, create a file and call it main.py

00:29 and start by importing Typer.

00:33 Then you’ll be defining a main function def main(). This function takes no argument for now. And then in there you have typer.echo(

00:45 "Hello, World!").

00:49 Finally make the script executable by checking if __name__ == "__main__" and then run typer .run() passing it your main function and you can save this file.

01:08 And in your terminal, you can execute this by running python3 main.py and you should see “Hello, World!” printed in your console confirming that your Typer CLI tool now works.

01:22 This is a very basic type of CLI application where all you’re doing is echoing or printing out the text “Hello, World!”. Next, we’ll be understanding arguments and options with command-line applications and Typer.

01:36 And to do that, you make your app a bit more interactive by adding arguments and options. In Typer and other command-line applications, arguments are defined as function parameters, which are required by default and are passed in a specific order to the command-line application.

01:55 And then CLI options, on the other hand, are optional parameters, which often have default values as they’re not always required and they are parameters passed to the command-line application with a specific name that starts with double dash or a single dash.

02:13 Now to see that in action, you start by changing your main function to greet function and accept an argument name type-annotated to signify that this accepts only string values and then an age option type-annotated to signify that it accepts only integer values.

02:33 This will be a Typer option so you can accept a default value setting that to 20. This right here is type hinting or annotation and you will see a lot of it in this course.

02:47 Type hinting or annotation is a way to specify the expected data types of variables, function arguments, and return values. Typer relies heavily on type hinting and annotation, and you’re going to be using a lot of it throughout this course. Back to your function, you can change the typer.echo( to now use this passed argument and option with `f”Hello, {name}, you are {age}

03:17 years old!".

03:22 This is now a greet function that greets a user and tells them how old they are. Then you change your typer.run() command to take the greet function instead. Remember to save the file.

03:36 Now if you save and try to run this as is like you did before, you should get an error saying missing argument name. That is because the name argument you’ve specified is required and the program fails without it.

03:52 To see exactly what your CLI requires, you can take advantage of the automatic documentation provided by Typer. To see this, you can run python3 main.py -- help and you should see details about your CLI application, the arguments, and the options.

04:12 Now that you know this, you can run this again and pass the name argument like say python3 main.py–name` Joe`. You should get “Hello, Joe, you’re 20 years old.” This without passing any age parameter.

04:28 This is because if the optional age parameter is not passed, it uses the default value you’ve set, which is 20. But you can also run and pass the age value with python3 main.py --name Joe –age 30` and now you get “Hello, Joe, you are 30 years old,” confirming the use of our argument and option.

04:56 Finally, let’s discuss CLI application commands. Typer allows you to create CLI programs with several commands. These commands are used to carry out certain named tasks and specific functions.

05:10 An example of this, if you’ve ever worked with Git, is say git push. Git in this case will be installed as a CLI application and then push is the command passed to carry out the git push function.

05:24 Before creating apps with commands, you need to understand that Typer has basic and then explicit applications. What you’ve worked with or seen so far are basic Typer applications, which under the hood Typer actually converts into explicit applications for you.

05:43 But if you want to start building more robust CLI applications that have several arguments, options, and commands, you’ll need to use the explicit Typer application format.

05:52 And to do that, instantiate a Typer app. So above your greet function you can have app equals typer.Typer() and you’ll reference this as your main Typer app.

06:06 When running the app on the if __name__ == "__main__": you can then call the app instead. Now let’s make your function a command. To do that, you’d add a decorator on top of the greet function like so.

06:22 With this @app.command() decorator Typer now makes this greet function a CLI command. One thing to note—by default, if you have just one single function and a single Typer command, Typer allows you to call the function just as you would in a basic Typer application and things should work fine.

06:41 I get the same result even though I’ve now introduced a new greet command. But when you start to have multiple commands in your application, I’ll just add another command.

06:52 Let’s say def goodbye(name: str) pass in just a name argument.

07:00 Again, making this a Typer command,

07:06 and this command also using typer.echo( will just tell the user “Goodbye”.

07:16 If you run this like you did before, you’ll get an error “No such command ‘Joe’”, meaning that something is not right and you need to call the command and to check what your application now requires, you can go back to the help documentation with python3 main.py --help.

07:34 You should now see what your application needs, a list of all the available commands that you have, greet and goodbye.

07:43 Now, if you run and call the greet command python3 main.py greet --name Joe --age 30, you get “Hello, Joe, you’re 30 years old,” and now it works.

07:56 If you need to alter the command name, for example, you wanted something shorter, you can pass an alias in the decorator. In this case, I want to change my goodbye command to just something shorter like bye.

08:11 And if you check the help again, you should now see the command name has been changed

08:18 and you can now call that command python3 main.py bye Joe and you should have “Goodbye, Joe.”

08:30 Great. You’ve got the basics of command-line application and building them with Typer. Next, you’ll proceed to building your to-do command-line application by first defining the project structure.

Become a Member to join the conversation.