DEV Community

Cover image for Building Command-Line Applications with Python
Kelvin Wangonya
Kelvin Wangonya

Posted on • Updated on • Originally published at wangonya.com

Building Command-Line Applications with Python

You can do a lot of amazing things with Python. When I started teaching myself Python last year, one of the most interesting things I discovered was how relatively easy it was to create CLI apps. Since I learn best by doing, I went ahead to create norris-cli among other apps, and discovered the possibilities were endless 😄.

Building these kinds of projects is a great way to learn, and it's super fun. To improve my skills and help others learn, I'll be doing a whole series on "Building CLI Apps with Python". I'll start with the basics and go step by step. In the end, we'll put what we've learned into practice by creating a practice project*. Of course, what we'll make won't be very useful, but we'll learn enough in the process to go ahead and make something truly useful.

What I'll cover:

  • Setting things up (Python 3.x, pip, Virtualenv, Click, etc)
  • Commands, options & arguments
  • Using Setuptools
  • Prompting users for input
  • CRUD (with an online API) (Practice project)
    • Working with data structures
    • Writing to local files
  • Testing with pytest
  • Posting a package on Pypi

I hope both the experienced and those just starting out with Python will find this series useful.

Update

* I initially intended to do two practice projects but it seemed redundant so I settled on making the CRUD section the project.

Top comments (7)

Collapse
 
thefern profile image
Fernando B 🚀

I am doing a similar series though not entirely the same.

dev.to/kodaman2/part-1-introductio...

Like yourself I started doing cli utilities at work and they've become life savers, and I've learned tons while doing them and also getting user feedback from coworkers is nice for ux.

Two questions for you:

  • Do you use cookiecutter?
  • How do you like click?

The reason I ask is that I am so used to using argparse, but might give click a try later on.

Collapse
 
notsag profile image
Maxime Gaston

Click makes you save a lot of time by using decorators :
@click.command()
@click.option('--parameter', '-p', help='your parameter')
def myfunction(myparameter):
print(myparameter)

I'll stop here not to spoil @wangonya 's article 😉

Still great topic!

Collapse
 
wangonya profile image
Kelvin Wangonya

Hey! Your series looks interesting. I'll be following along :)

  • No, I've actually never used cookiecutter.
  • I absolutely love Click, mainly because it's what I'm most familiar with :). I've not used argparse before.
Collapse
 
thefern profile image
Fernando B 🚀 • Edited

Cookiecutter gives you quick prompts and generates the whole skeleton for cli app, in less than a couple of minutes you'll have a full directory with files like readme, license, setup.py, etc. It also generates a click.py file if you choose 1 on the click prompt, but I don't use it for the time being since I do it through argparse :) I wonder if I can change the prompts to generate an argparse driven py file instead, onto stackoverflow lol.

Thanks for the feedback on click.

Thread Thread
 
wangonya profile image
Kelvin Wangonya

I'll definitely try it out. Thanks for the tip!

Collapse
 
j_mplourde profile image
Jean-Michel Plourde

I'm learning Python and this kind of project is perfect to learn more. I intend to learn how to mkae a python CLI, make requests to an API and package it on pypi.

Collapse
 
wangonya profile image
Kelvin Wangonya

Well then, you're in luck :D Stay tuned