[issue34458] No way to alternate options

2018-09-22 Thread paul j3


Change by paul j3 :


--
resolution:  -> not a bug

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34458] No way to alternate options

2018-09-20 Thread paul j3


Change by paul j3 :


--
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34458] No way to alternate options

2018-08-22 Thread paul j3


paul j3  added the comment:

I agree that a custom Action subclass like this is the way to go.

The regular 'append' is implemented in _AppendAction class, which is different 
from the default _StoreAction.  So even if there was enough demand for this new 
idea to be put into development, it would be implemented as subclass (along 
with a registry entry).

--
nosy: +paul.j3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34458] No way to alternate options

2018-08-22 Thread Josh Rosenberg


Josh Rosenberg  added the comment:

That's a *really* niche use case; you want to store everything to a common 
destination list, in order, but distinguish which switch added each one? I 
don't know of any programs that use such a design outside of Python (and 
therefore, it seems unlikely there would be enough demand from argparse users 
to justify the development, maintenance, and complexity cost of adding it).

argparse does support defining custom Actions, so it's wholly possible to add 
this sort of support for yourself if there isn't enough demand to add it to 
argparse itself. For example, a simple implementation would be:

class AppendWithSwitchAction(argparse.Action):
def __init__(self, option_strings, dest, *args, **kwargs):
super().__init__(option_strings, dest, *args, **kwargs)
# Map all possible switches to the final switch provided
# so we store a consistent switch name
self.option_map = dict.fromkeys(option_strings, option_strings[-1])

def __call__(self, parser, namespace, values, option_string=None):
option = self.option_map.get(option_string)
try:
getattr(namespace, self.dest).append((option, value))
except AttributeError:
setattr(namespace, self.dest, [(option, value)])

then use it with:

parser.add_argument('-p', '--preload', help='preload asset', 
action=AppendWithSwitchAction, metavar='NAMESPACE')

parser.add_argument('-f', '--file', help='preload file', 
action=AppendWithSwitchAction, metavar='FILE', dest='preload')

All that does is append ('--preload', argument) or ('--file', argument) instead 
of just appending the argument, so you can distinguish one from the other (for 
switch, arg in args.preload:, then test if switch=='--preload' or '--file'). 
It's bare bones (the actual class underlying the 'append' action ensures nargs 
isn't 0, and that if const is provided, nargs is '?'), but it would serve.

--
nosy: +josh.r

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34458] No way to alternate options

2018-08-21 Thread Victor Porton


New submission from Victor Porton :

I have:

parser.add_argument('-p', '--preload', help='preload asset', action='append', 
metavar='NAMESPACE')

I want also add:

parser.add_argument('-f', '--file', help='preload file', action='append', 
metavar='FILE', dest='preload')

This way I could specify -p and/or -f options in any order (like: first -p then 
-f then -p again) and store then in 'preload' field in the order which the user 
specified the options.

But I have no way to know if an option is -p or -f :-(

Please add something to argparse to solve this problem.

--
components: Library (Lib)
messages: 323857
nosy: porton
priority: normal
severity: normal
status: open
title: No way to alternate options
versions: Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com