Tools for Writing Python CLI Applications

Written by pizzapanther | Published 2019/01/18
Tech Story Tags: python | cli | terminal | development | python-cli-applications

TLDRvia the TL;DR App

If you’re a Python developer you have probably used many command line tools and maybe even had to write a few yourself. Writing CLI tools can be anywhere from really fun to really frustrating. Here are a few of my favorite tools that can help you write a command line applications in Python. Every project has different goals and requirements, so there isn’t one clear winner. However, hopefully this guide will help you choose one that fits your needs.

argparse

argparse is part of the standard library and gives you a way to parse command-line options, arguments, and sub-commands. For me argparse is a little more on the frustrating end of the spectrum of development. However, argparse is much improved over older standard library options, and since it is part of the standard library it’s always there with no extra installation needed. If you are looking to not include many dependencies in your code, or you have few very options to parse, then argparse is a good choice. Also since it is part of the Python standard library you will probably run into this module sometime in your career. So it won’t hurt to get familiar with argparse.

click

click sells itself as the “Command Line Interface Creation Kit” and it definitely lives up to it. I find click to be less frustrating to use than argparse but in addition to ease of use, it includes many more tools. So while it is easier in the beginning of using it, it is paradoxically harder to learn because of it’s sheer size. If you want to build an advanced CLI application, click probably has a feature to help you. It includes tools like confirmation prompts, password prompts, variables parsed from the Environment and command line, printing in color, paging support, waiting for a keypress, and more. click is a really great tool to write beautiful and advanced CLI applications.

fire

fire is the newest of these three modules and fills a great sweet spot for writing CLI applications. fire helps you write CLI applications with the fewest lines of extra code. With argparse and click you end up writing many lines to configure and document your application. fire on the other hand will take any existing Python code you have and make it a command line tool. It auto documents the tool based on function or class inputs.

I find fire extremely awesome for taking existing code and making it into a CLI tool. Also if you start off with fire, I find it forces you into a good architectural style so your code can be both a CLI tool and a library to be imported. So if you are looking for something extremely easy to use with a moderate feature set, fire is your pick.

TLDR

  • argparse: Use argparse to lessen your dependencies and for simple to moderately complex CLI applications.
  • click: Use click if you are looking for something that can be easy to start with but also gives you tools to write really complex CLI applications.
  • fire: Use fire if you want the easiest solution to creating a CLI application or you want to take some existing code and turn it into a CLI application.

Written by pizzapanther | Father, web developer, and pizza maker; Software Entomologist @ Cognitive Space
Published by HackerNoon on 2019/01/18