C is for command line interfaces - Python A to Z Dark _ □ ✕
  • Posts
  • About
  • Debug
Post

C is for command line interfaces - Python A to Z

Published
2026-08-04 00:00 UTC (precision: second)
Source feed
Juha-Matti Santala - Community Builder. Dreamer. Adventurer. [exclude this blog]
Link
https://hamatti.org/posts/c-is-for-command-line-interfaces-python-a-to-z/
Canonical
https://hamatti.org/posts/c-is-for-command-line-interfaces-python-a-to-z
Length
5693 runes
Extracted text
C is for command line interfaces - Python A to Z Aug 4th, 2026 by Juha-Matti Santala (@hamatti@hamatti.org) This was published in categories: blaugust-2026 blaugust python-a-to-z python cli Code in this blog post was written with versions: Python: 3.14 Python A-Z is a blog series about Python. Each day, I share insights, ideas and examples for different parts of Python development that match with the letter of the day. Blaugust is an annual blogging festival in August where the goal is to write a blog post every day of the month. Python is a great language for writing scripts that are executed from the command line. I’m a big fan of command line interfaces and love building them for all sorts of needs both personally and when I’m part of a team. Command Line Interface Guidelines An integral part of command line software is dealing with arguments and options. In this post, I’ll focus on the technical part within the Python code but I highly recommend reading Command Line Interface Guidelines. It’s an open source guide to “help you write better command-line programs, taking traditional UNIX principles and updating them for the modern day”. I keep it at hand and always refer to it when I’m designing my interfaces. Simple arguments through sys.argv For the simplest use cases, accessing the list of arguments through sys.argv is the best option. I usually use it at the beginning while I’m still forming the ideas of what the tool will look like or when the script only requires one or two positional arguments. Let’s say we run a script with the following: python script.py 2 file.txt To access those arguments, we write script.py to be: import sys script_name = sys.argv[0] # script.py copies = sys.argv[1] # 2 filename = sys.argv[2] # file.py You can’t trust the user to always provide the right amount of arguments though so it’s a good idea to add some checks and instruct the user how to use it properly. import sys if __name__ == "__main__": arguments = sys.argv if len(arguments) < 3: print("""Two arguments required for the amount of copies and the filename. Usage; script.py <amount of copies> <filename>""") sys.exit(1) copies = arguments[1] filename = arguments[2] This provides a top level check AND documentation at the same time and is a great habit to get into. Documentation-first approach with docopt Speaking of documentation, docopt and its maintained Python implementation docopt-ng provide a documentation-driven way for declaring the command line interface. Defining the interface first leads to better user experience. In my experience, starting with implementation details often leads to either suboptimal interfaces or spaghetti code to try to mend an implementation to interface later. If you start by thinking how the user should interact with the application, then the rest follows to fill the gaps in an elegant way. Using docopt, you write the usage pattern in your docstring and let the tool figure out what arguments and options it needs to derive from it (example from docs): """Naval Fate. Usage: naval_fate.py ship new <name>... naval_fate.py ship <name> move <x> <y> [--speed=<kn>] naval_fate.py ship shoot <x> <y> naval_fate.py mine (set|remove) <x> <y> [--moored | --drifting] naval_fate.py (-h | --help) naval_fate.py --version Options: -h --help Show this screen. --version Show version. --speed=<kn> Speed in knots [default: 10]. --moored Moored (anchored) mine. --drifting Drifting mine. """ from docopt import docopt if __name__ == '__main__': arguments = docopt(__doc__, version='Naval Fate 2.0') print(arguments) I have written about docopt a few years ago and that post still holds water so I recommend you go read that for more information. Click The library I use most often these days — because it works so nicely with custom Django commands with django-click — is click. Click uses Python decorators to define commands, subcommands, arguments and options. # Example from click documentation # https://click.palletsprojects.com/en/stable/ import click @click.command() @click.option('--count', default=1, help='Number of greetings.') @click.option('--name', prompt='Your name', help='The person to greet.') def hello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for x in range(count): click.echo(f"Hello {name}!") if __name__ == '__main__': hello() You can add extra validation through callbacks directly at this level which makes the actual code of your application a bit cleaner when the validation and first level input processing is done before the arguments and options ever reach your application logic. # Example from click documentation # https://click.palletsprojects.com/en/stable/advanced/#callbacks-for-validation def validate_rolls(ctx, param, value): if isinstance(value, tuple): return value try: rolls, _, dice = value.partition("d") return int(dice), int(rolls) except ValueError: raise click.BadParameter("format must be 'NdM'") @click.command() @click.option( "--rolls", type=click.UNPROCESSED, callback=validate_rolls, default="1d6", prompt=True, ) def roll(rolls): sides, times = rolls click.echo(f"Rolling a {sides}-sided dice {times} time(s)") With django-click, you can use the same interface to define an interface for your custom commands. import djclick as click @click.command() @click.option("--file", help="Load set from a local JSON file") def command(file): ... If something above resonated with you, let's start a discussion about it! Email me at juhis@hamatti.org and share your thoughts. This year, I want to have more deeper discussions with people from around the world and I'd love if you'd be part of that.
Stored topics

Every stored assignment is listed, including rows below the current cutoff (0.75), so a missing tag can be explained.

Concept Code Score Meter Margin Origin Path
economy, business and finance economy-business-and-finance 0.99 5.10 ↑derived from 20000225 economy, business and finance
products and services products-and-services 0.99 5.10 ↑derived from 20000225 economy, business and finance > products and services
computing and information technology computing-and-information-technology 0.99 5.10 direct economy, business and finance > products and services > computing and information technology
software and applications software-and-applications 0.99 4.46 ↑derived from local:software-development economy, business and finance > products and services > computing and information technology > software and applications
Software development software-development 0.99 4.46 direct economy, business and finance > products and services > computing and information technology > software and applications > Software development
science and technology science-and-technology 0.99 4.19 direct science and technology
technology and engineering technology-and-engineering 0.98 4.01 ↑derived from 20000763 science and technology > technology and engineering
information technology and computer science information-technology-and-computer 0.98 4.01 direct science and technology > technology and engineering > information technology and computer science
Open source open-source 0.97 3.42 direct economy, business and finance > products and services > computing and information technology > software and applications > Open source
arts, culture, entertainment and media arts-culture-entertainment-and-media 0.86 1.84 ↑derived from local:blogging arts, culture, entertainment and media
mass media mass-media 0.86 1.84 ↑derived from local:blogging arts, culture, entertainment and media > mass media
social media social-media 0.86 1.84 ↑derived from local:blogging arts, culture, entertainment and media > mass media > social media
Blogging blogging 0.86 1.84 direct arts, culture, entertainment and media > mass media > social media > Blogging
artificial intelligence artificial-intelligence 0.35 -0.61 direct science and technology > technology and engineering > information technology and computer science > artificial intelligence
Information security information-security 0.20 -1.38 direct science and technology > technology and engineering > information technology and computer science > Information security
scientific research scientific-research 0.18 -1.55 ↑derived from 20000739 science and technology > scientific research
scientific exploration scientific-exploration 0.18 -1.55 ↑derived from 20000739 science and technology > scientific research > scientific exploration
space exploration space-exploration 0.18 -1.55 direct science and technology > scientific research > scientific exploration > space exploration
lifestyle and leisure lifestyle-and-leisure 0.15 -1.72 ↑derived from 20000538 lifestyle and leisure
leisure leisure 0.15 -1.72 direct lifestyle and leisure > leisure
conflict, war and peace conflict-war-and-peace 0.08 -2.41 direct conflict, war and peace
environment environment 0.08 -2.46 direct environment
hobby hobby 0.07 -2.58 direct lifestyle and leisure > leisure > hobby
game game 0.07 -2.63 ↑derived from local:mmorpg lifestyle and leisure > leisure > game
video game video-game 0.07 -2.63 ↑derived from local:mmorpg lifestyle and leisure > leisure > game > video game
MMORPG mmorpg 0.07 -2.63 direct lifestyle and leisure > leisure > game > video game > MMORPG
arts and entertainment arts-and-entertainment 0.05 -3.05 ↑derived from 20000013 arts, culture, entertainment and media > arts and entertainment
literature literature 0.05 -3.05 direct arts, culture, entertainment and media > arts and entertainment > literature
politics and government politics-and-government 0.04 -3.07 direct politics and government
social sciences social-sciences 0.04 -3.16 ↑derived from 20000747 science and technology > social sciences
history history 0.04 -3.16 direct science and technology > social sciences > history
crime, law and justice crime-law-and-justice 0.03 -3.34 ↑derived from 20000086 crime, law and justice
crime crime 0.03 -3.34 ↑derived from 20000086 crime, law and justice > crime
cyber crime cyber-crime 0.03 -3.34 direct crime, law and justice > crime > cyber crime
education education 0.03 -3.34 direct education
media and entertainment industry media-and-entertainment-industry 0.03 -3.59 ↑derived from 20000306 economy, business and finance > products and services > media and entertainment industry
books and publishing books-and-publishing 0.03 -3.59 direct economy, business and finance > products and services > media and entertainment industry > books and publishing
health health 0.03 -3.62 ↑derived from 20000458 health
disease and condition disease-and-condition 0.03 -3.62 ↑derived from 20000458 health > disease and condition
mental health and disorder mental-health-and-disorder 0.03 -3.62 direct health > disease and condition > mental health and disorder
society society 0.03 -3.64 ↑derived from 20000780 society
family family 0.03 -3.64 direct society > family
labour labour 0.03 -3.64 direct labour
sport sport 0.02 -3.68 ↑derived from 20001183 sport
competition discipline competition-discipline 0.02 -3.68 ↑derived from 20001183 sport > competition discipline
eSports esports 0.02 -3.68 direct sport > competition discipline > eSports
visual arts visual-arts 0.02 -3.68 ↑derived from 20000036 arts, culture, entertainment and media > arts and entertainment > visual arts
photography photography 0.02 -3.68 direct arts, culture, entertainment and media > arts and entertainment > visual arts > photography
film industry film-industry 0.02 -3.69 direct economy, business and finance > products and services > media and entertainment industry > film industry
Tabletop gaming tabletop-gaming 0.02 -3.95 direct lifestyle and leisure > leisure > game > Tabletop gaming
human interest human-interest 0.02 -4.07 direct human interest
weather weather 0.02 -4.11 ↑derived from 20001128 weather
weather forecast weather-forecast 0.02 -4.11 direct weather > weather forecast
Self-hosting self-hosting 0.02 -4.14 direct economy, business and finance > products and services > computing and information technology > Self-hosting
award and prize award-and-prize 0.01 -4.21 direct human interest > award and prize
exercise and fitness exercise-and-fitness 0.01 -4.21 direct lifestyle and leisure > wellness > exercise and fitness
wellness wellness 0.01 -4.21 ↑derived from 20001239 lifestyle and leisure > wellness
culture culture 0.01 -4.28 direct arts, culture, entertainment and media > culture
disaster, accident and emergency incident disaster-accident-and-emergency-incident 0.01 -4.35 direct disaster, accident and emergency incident
Cryptography cryptography 0.01 -4.41 direct science and technology > technology and engineering > information technology and computer science > Cryptography
board game board-game 0.01 -4.44 direct lifestyle and leisure > leisure > game > board game
travel and tourism travel-and-tourism 0.01 -4.46 direct lifestyle and leisure > leisure > travel and tourism
natural science natural-science 0.01 -4.46 direct science and technology > natural science
card game card-game 0.01 -4.49 direct lifestyle and leisure > leisure > game > card game
fundamental rights fundamental-rights 0.01 -4.54 ↑derived from 20001300 society > fundamental rights
privacy privacy 0.01 -4.54 direct society > fundamental rights > privacy
biology biology 0.01 -4.56 direct science and technology > natural science > biology
television television 0.01 -4.63 direct arts, culture, entertainment and media > mass media > television
health treatment and procedure health-treatment-and-procedure 0.01 -4.70 direct health > health treatment and procedure
diet diet 0.01 -4.70 direct health > health treatment and procedure > diet
climate change climate-change 0.01 -4.76 direct environment > climate change
animation animation 0.01 -4.91 direct arts, culture, entertainment and media > arts and entertainment > animation
public health public-health 0.01 -4.95 direct health > public health
religion religion 0.01 -5.11 direct religion
music music 0.01 -5.16 direct arts, culture, entertainment and media > arts and entertainment > music
food and drink enthusiasm food-and-drink-enthusiasm 0.01 -5.23 direct lifestyle and leisure > leisure > hobby > food and drink enthusiasm
streaming service streaming-service 0.00 -5.30 direct economy, business and finance > products and services > media and entertainment industry > streaming service
podcast podcast 0.00 -5.52 direct economy, business and finance > products and services > media and entertainment industry > podcast
lifestyle lifestyle 0.00 -5.69 ↑derived from 20001257 lifestyle and leisure > lifestyle
house and home house-and-home 0.00 -5.69 ↑derived from 20001257 lifestyle and leisure > lifestyle > house and home
gardening gardening 0.00 -5.69 direct lifestyle and leisure > lifestyle > house and home > gardening
consumer goods consumer-goods 0.00 -6.01 ↑derived from 20001160 economy, business and finance > products and services > consumer goods
handicrafts handicrafts 0.00 -6.01 direct economy, business and finance > products and services > consumer goods > handicrafts

← Back to posts

feedtagger 0.1.0-dev model: modernbert-base-zeroshot-v2.0/int8@all-s256 660 posts 0 unclassified min score 0.75