Skip to content
Advertisement

collecting CLI usage data with python click

I’m a platform engineer building a python CLI app (using click) that teams in the same company could use to launch data processing jobs on our internally managed cluster.

I want to build a functionality to collect usage data i.e., record the commands submitted to the app and store them in a database.

Something like this:

JavaScript

Is it possible? Is there an established pattern for collecting CLI usage data in python?

Advertisement

Answer

No matter if you use click or typer or argparse or nothing at all, you can find the whole command line in sys.argv.

For a click.group() app, you could store the command line in the group callback:

JavaScript

I’d recommend scrubbing the command of any potential secrets before sending them over, of course.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement