Python argparse vis shell glob

Users (or developers!) may not realize that the shell expands glob asterisk * unless enclosed in quotes. This can surprise users unfamiliar with this shell behavior, say when using Python argparse with position-based arguments. Say a user has a single file to process in a directory, and doesn’t want to type the long filename, so they type:

python ~/data/*.h5 32

Here we assume expects two positional arguments, the first being a filename, and the second being an integer. If more than one “*.h5” file subsequently exists and is run, the actual input to Python would be like:

python ~/data/file1.h5 ~/data/file2.h5 32

Which causes a Python argparse exception.

To see what the shell is going to expand to, with default keybindings and Bash or Zsh at least, press after typing the command these keys:

Ctrlx g