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:
import click @click.group() @click.option("--environment", help="cluster to use") def app(): ... @app.command() @click.option("--file-path", required=True) def run(): # if a user runs "$ app --environment development run --file-path test.py" # we should retrieve the command as is # and store it somewhere # Question: how do I get the complete command? ...
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:
import click import sys @click.group() @click.option("--environment", help="cluster to use") def app(): save_command(sys.argv) def save_command(args: list[str]): ...
I’d recommend scrubbing the command of any potential secrets before sending them over, of course.