Lazy Python Argument Parsing

November 2nd, 2022
python, tech
When I'm writing real Python programs I use argparse. When I'm writing quick scripts, I destructure sys.argv[1:]. For example:

width, depth, height = sys.argv[1:]
...

Or, if I need to import the file:

def start(width, depth, height):
  ...

if __name__ == '__main__':
  start(*sys.argv[1:])

This is a very simple way of handling positional arguments. It ignores sys.argv[0] which is likely the name of the script, and then assigns the remaining arguments to the variables. If I provide the wrong number of arguments it's a bit shouty, but it's clear enough for a quick script and does remind me of the intended arguments:

Traceback (most recent call last):
  File "example.py", line 2, in 
    width, depth, height = sys.argv[1:]
ValueError: too many values to unpack (expected 3)

Comment via: facebook, lesswrong, substack

Recent posts on blogs I like:

Mindful posting

It is important, for the preservation of American democracy and maybe the world, that the Democrats take the House and Senate in November.

via Thing of Things September 18, 2026

There's no point at which turning your brain off will work

In early 2025, I started seeing people turn off their brain as they use LLMs1. They would have an LLM take an action (summarize text, write some code, etc.), and just assume that it worked2. This generally didn't work in early 2025 and the result was …

via Posts on September 18, 2026

My Weirdy Creatures

Over the last few days, I've been working on some little creatres. But they're very weird. I call them "weirdy creatures". I've made quite a lot of them, I think maybe 5-6, not sure. Weirdy Predator This is a weirdy predator. They'…

via Anna Wise's Blog Posts September 13, 2026

more     (via openring)