Member-only story
Advanced Python: Using decorators, argparse, and inspect to build Fastarg, a command line argument parser library
I would like to announce the initial release of Fastarg, an open source, lightweight argparse wrapper for building CLI tools.
https://github.com/travisluong/fastarg
https://pypi.org/project/fastarg/
There are no dependencies required to run Fastarg, as it is built on Python’s standard argparse library.
Fastarg follows a nearly identical API to Typer, another popular library for building CLI applications. Typer was the primary inspiration for Fastarg.
Fastarg was developed out of pure curiosity. I wanted to understand the magic of the @
decorator syntax that was so prevalent in many of the frameworks I used regularly.
This all led me down the rabbit hole of metaprogramming, introspection, and inevitably building the first prototype of Fastarg.
In this article, I will go over some of the lessons I learned in that journey. But first, I must give the disclaimer. Use the library and the information provided here at your own risk.
Decorators
The first concept I had to wrap my head around was decorators. In Python, functions are first class objects, which means they can be passed as parameters into other functions and wrapped with additional logic.