homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse: option for a positional argument
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bethard, eric.araujo, georg.brandl, r.david.murray, wrobell
Priority: normal Keywords:

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:31adminsetgithub: 59267
2012年06月16日 18:25:57georg.brandlsetnosy: + georg.brandl
messages: + msg162983
2012年06月16日 16:43:37eric.araujosetnosy: + eric.araujo
messages: + msg162977
2012年06月14日 01:18:12r.david.murraysetnosy: + bethard
messages: + msg162746
2012年06月14日 01:04:46wrobellsetmessages: + msg162745
2012年06月14日 00:13:09r.david.murraysetnosy: + r.david.murray
messages: + msg162743
2012年06月13日 23:15:45wrobellcreate

AltStyle によって変換されたページ (->オリジナル) /