Build a Python CLI with argparse

Veröffentlicht am: 08 Februar 2022
auf dem Kanal: Alfredo Deza
869
19

The `argparse` module is part of the Python standard library and meant to help you build robust command line tools. Once you need flags, switches, values, or positional arguments, it is very difficult to achieve that without using a framework.

Follow the GitHub repo with sample code: https://github.com/alfredodeza/argpar...

In this case, `argparse` can help build all you need to handle inputs in the terminal. We'll see how a single file named `size.py` that calculates file sizes can make use of `argparse` to handle the input in the terminal. Then, we will cover how error handling is done automatically when the expectations from `argparse` aren't met. And finally, we'll go through some of the help menu options that construct the body of the help output.

→ Why would you want to use argparse?
Since `argparse` is part of the Python standard library, there is no need for you to define or install any external dependencies. You are ensuring that any Python installation in any system will work correctly out of the gate.
A very basic Python CLI tool without dependencies using the argparse module

→ Next steps
Now that you know how to enhance a Python script into a more powerful one with `argparse`, you can try out other frameworks like the Click framework or something much simpler by using the `sys` module. I have examples and guides for the sys.argv module (https://github.com/alfredodeza/basic-...) and for using the Click framework (https://github.com/alfredodeza/click-....

00:00 Introduction
00:26 Basic script using sys.argv
01:36 Import argparse and start converting
01:53 Create a function for argparse
03:06 How argparse parses args and maps them to the script
04:32 Use parsed arguments in your script
04:57 Update to use the main() function with argparse
05:26 Error reporting with argparse
05:58 Help menu for free


Auf dieser Seite können Sie das Online-Video Build a Python CLI with argparse mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Alfredo Deza 08 Februar 2022 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 869 Mal angesehen und es wurde von 19 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!