Issue 15062: argparse: option for a positional argument (original) (raw)

Created on 2012-06-13 23:15 by wrobell, last changed 2022-04-11 14:57 by admin.

Messages (6)
msg162741 - (view) Author: wrobell (wrobell) Date: 2012-06-13 23:15
it would be great if argparse allowed to specify options for multiple positional arguments, i.e. usage: ascript [-h] [-k value] input [[-k value] input ...] output then $ ascript pos1 pos2 -k 1 pos3 -k 2 pos4 pos5 pos6 out would give Namespace(input=[(None, 'pos1'), (None, 'pos2'), ('1', 'pos3'), ('2', 'pos4'), (None, 'pos5'), ('None', 'pos6')], output=['out'])
msg162743 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-06-14 00:13
It took me a while to make any sense out of your example, but having done so I don't think it makes any sense in the argparse context. It is certainly not the way argparse handles options or arguments. In argparse, if you have an option 'k', you get 'k' mapped to a value in the Namespace. I don't believe I've ever seen a command line command with syntax like that either. Do you know of one?
msg162745 - (view) Author: wrobell (wrobell) Date: 2012-06-14 01:04
an example would be a pdf merger accepting multiple files and allowing to specify list of pages for each input. at the moment, you can do the following with argparse pdfmerge [pages] input [[pages] input ...] output of course, above is not perfect as you need a bit of heuristic to determine what is "pages" and what is the "input" itself (easy to do, but still...). on other side, i find the following more straightforward pdfmerge [-p pages] input [[-p pages] input ...] output or to complicate things pdfmerge [-p pages] [-s scale] input [[-p pages] [-s scale] input ...] output btw. i understand the argparse limitations, therefore putting this as feature enhancement... if possible :)
msg162746 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-06-14 01:18
Well, if you can figure out how to add it, please do suggest a patch :) It occurs to me that 'find' is a sort-of similar example, where options can be repeated but the order in which they appear relative to other options matters. So I can see that there could be utility to such a thing if someone can figure out how to do it. On the other hand, it is quite possible that these kind of commands really need a custom parser.
msg162977 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-06-16 16:43
I wouldn’t use the syntax used by find, dd or other very old and peculiar commands as an example for anything new :)
msg162983 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-06-16 18:25
What alternate syntax would you suggest for a "find" type command then (other than replacing "-long" by "--long")?
History
Date User Action Args
2022-04-11 14:57:31 admin set github: 59267
2012-06-16 18:25:57 georg.brandl set nosy: + georg.brandlmessages: +
2012-06-16 16:43:37 eric.araujo set nosy: + eric.araujomessages: +
2012-06-14 01🔞12 r.david.murray set nosy: + bethardmessages: +
2012-06-14 01:04:46 wrobell set messages: +
2012-06-14 00:13:09 r.david.murray set nosy: + r.david.murraymessages: +
2012-06-13 23:15:45 wrobell create