Mastering Click: Writing Advanced Python Command-Line Apps

Last updated 1 year ago by Seb Vetter

python

Welcome to the second Click tutorial on how to improve your command-line tools and Python scripts. I’ll show you some more advanced features that help you when things are getting a bit more complex and feature rich in you scripts.

You might wonder why I suggest using Click over argparse or optparse. I don’t think they are bad tools, they both have their place and being part of the standard library gives them a great advantage. However, I do think that Click is much more intuitive and requires less boilerplate code to write clean and easy-to-use command-line clients.

I go into more details about that in the first tutorial and give you a comprehensive introduction to Click as well. I also recommend you to take a look at that if this is the first time you hear the name “Click” so you know the basics. I’ll wait here for you.

Now that we are all starting from a similar knowledge level, let’s grab a cup of tea, glass of water or whatever it is that makes you a happy coder and learner ✨. And then we’ll dive into discovering:

  • how you can read parameter values from environment variables,
  • we’ll then separate functionality into multiple sub-commands
  • and get the user to provide some input data on the command-line.
  • We’ll learn what parameter types are and how you can use them and we’ll look at contexts in Click to share data between commands.
Read full Article