[Cialug] Python argparse question
Todd Walton
tdwalton at gmail.com
Wed Jan 7 18:36:51 UTC 2026
>From the ripgrep man page:
rg [OPTIONS] PATTERN [PATH...]
rg [OPTIONS] -e PATTERN... [PATH...]
So pattern always comes first, path second, and pattern is either the first
positional argument, or it's made explicit with "-e". How do I do that with
argparse in Python? My script takes a command to run in each subdirectory
of a given directory. So like:
myscript.py 'git status' ~/gitrepos
myscript.py 'git status'
myscript.py -c 'git status' .
I currently have this:
parser.add_argument(
"directory",
nargs='?',
default=os.getcwd(),
help="Root directory to search for git repositories",
)
parser.add_argument(
"-c",
"--command",
action="append",
dest="commands",
required=True,
help="Command to run in each repo (can be specified multiple
times)",
)
--
Todd
More information about the Cialug
mailing list