From tdwalton at gmail.com Wed Jan 7 18:36:51 2026 From: tdwalton at gmail.com (Todd Walton) Date: Wed, 7 Jan 2026 12:36:51 -0600 Subject: [Cialug] Python argparse question Message-ID: >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 From kristau at protonmail.com Wed Jan 7 23:54:32 2026 From: kristau at protonmail.com (kristau) Date: Wed, 07 Jan 2026 23:54:32 +0000 Subject: [Cialug] Python argparse question In-Reply-To: References: Message-ID: Spit-balling an idea here: Make -c/--command optional instead of required Call parse_args() with made up positionals If -c/--command is defined, then call parse_args() again, replacing the first positional with the contents of -c/--command Else, call parse_args() expecting the 1st positional to be the command and 2nd positional to be the path Either way, use the positional values in the main part of the script, ignoring -c/--command, because you will have copied it into the positional I don't think this will work with the append action on -c/--command. Also, the first call to parse_args() *might* clobber to original values with the bogus ones? I'll leave testing this idea up to you, but I'd love to hear whether or not it works! Thanks! kristau On Wednesday, January 7th, 2026 at 12:37 PM, Todd Walton wrote: > 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 > _______________________________________________ > Cialug mailing list > Cialug at cialug.org > https://www.cialug.org/cgi-bin/mailman/listinfo/cialug From barry at vonahsen.com Thu Jan 8 00:05:30 2026 From: barry at vonahsen.com (Barry Von Ahsen) Date: Thu, 08 Jan 2026 00:05:30 +0000 Subject: [Cialug] Python argparse question In-Reply-To: References: Message-ID: my first thought, consider -c/--command as a flag, then parse the first positional argument as the command as you were. you can action='store_true' to do whatever logic you may need for the explicit case in your command parser method ``` def validate_command(str_command: str) if command: #the confusingly named arg bool, not the arg to this method # do something slightly different because explicitly -c? else: sys.exec(str_command) ``` second thought, have both args, and send them through the same command parser method: ``` add_argument("-c","--command", ...) add_argument("positional_command", ...) args = parser.parse_args if args.command: validate_command(command) elif args.positional_command: validate_command(positional_command) ``` third thought: I haven't used dest= can you use the same dest= on multiple arguments? -barry On Wednesday, January 7th, 2026 at 12:37, Todd Walton wrote: > > > 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 > _______________________________________________ > Cialug mailing list > Cialug at cialug.org > https://www.cialug.org/cgi-bin/mailman/listinfo/cialug