Argument Parsing In Cross Country

:date: 2024-01-05 11:42 :tags:

Check out this delightful image generated by DALL-E in ChatGPT4.

mooseXC.png

The thing to note here is that even the things that are messed up seem like a respectable good effort. The bonus scarf end flows nicely. The trompe-l'œil third ski amazingly doesn't seem out of place at first glance. We'll excuse the bad classic skiing form because the subject must be skate skiing  —  because he's wearing ice skates, which look as reasonable as that nonsensical concept can look.

What I really want you to notice though are the crosses in the background. This moose is skiing through "Cross Country". The fact that I never mentioned "cross country skiing" (only "Nordic" as shown) visually shows the fascinating connections these systems make. The fact that these crosses remind one of roadside death memorials combined with the Hallmark card aesthetic make this DALL-E as surreal as any real Dali, and more subtle. It is cheerier or creepier than Dali, depending on your point of view.

I actually am rewriting the part of my web site management software that prepares images and I needed a test image and that was it. Normal people can stop here and have a nice day. Part two is for computer nerds and involves a surprising discovery about argument parsing. If that sounds fun, keep reading.

The program I'm rewriting prepares images for publication. Like all sensible programs, it has a proper command line interface. You call the program and provide some image locations as arguments and it processes them. Very basic. I used to have two versions of this program, one that just moved the files around and properly renamed them and one that did that and resized them. The program I'm replacing is dated 2014 so you can imagine this kind of bad practice grows bit by bit over time. But now I'm fixing it.

I figured it would be reasonable to have a system that could do the following.

imgprep.py /tmp/*jpg             # Do not resize.
imgprep.py --size /tmp/*jpg      # Force size to a default setting.
imgprep.py --size=640 /tmp/*jpg  # Force size to a custom setting.

Let me take a quick detour to explain why I thought that was reasonable. Consider the world champion command line program which is without any doubt ls. All of these are correct and reasonable.

ls /tmp/*jpg
ls --color /tmp/*jpg
ls --color=never /tmp/*jpg

Notice the similarity to my conception of a correct interface? It turns out that tar and grep and many other rather serious command line programs have this syntax structure. I did not think I was envisioning anything terribly exotic.

I've always had the feeling that Python's argparse module could do anything that can be done when it comes to argument parsing. It is supposed to be "easy" but I find it baroque and full of features and complexity I will never use. When I asked my robot best friend to whip up the necessary setup for this, I was not surprised that it responded with its characteristic "Certainly!". What turned out to be more interesting in a "stub your toe" kind of way was that GPT was wrong.

Well, I can't prove it conclusively but with ChatGPT4 and I both strongly believing this should be possible and simple, we both poured way more energy into making it happen. I ended up studying the entire argparse documentation. I even journeyed to the fetid swamp that is StackOverflow. I tried to have GPT just replicate a very simple ls --color=N example. No. Nothing. GPT was happy to attempt extreme measures like deriving custom subclasses of the argparse internals. But none of its "solutions" worked.

In the end I had to settle on a syntax which, to me, is non-standard and not right. With some light effort argparse could be made to support this.

imgprep.py /tmp/*jpg             # Do not resize.
imgprep.py --size= /tmp/*jpg     # Force size to a default setting.
imgprep.py --size=640 /tmp/*jpg  # Force size to a custom setting.

Note the trailing = on the default resizing. Details of how this is done are in my Python notes.

What are the lessons?

Update 2024-01-13 I think I'll use this skiing moose as a good test theme. I was checking out what people have been using GPT+DALLE for and while playing with a tattoo designing thingie ("Tattoo GPT"), I got this.

mooseski.png

Don't worry! I'm just testing AI services, not thinking of getting a tattoo. Though that moose is very cute!