[issue20430] Make argparse.SUPPRESS work as an argument "dest"

2016-04-24 Thread paul j3

paul j3 added the comment:

I just found a case where `dest` is `SUPPRESS` - the default subparsers setup.

  _SubParsersAction(option_strings=[], dest='==SUPPRESS==', nargs='A...', 
...

If I make this Action 'required' (in the current distribution 'subparsers' are 
not required - there's a bug/issue for that)

 parser._actions[1].required=True

and call the parser without the required subparser argument I should get an 
error message.  In earlier versions this would have been a non specific,  
`error: too few arguments', but the new one tries to name the missing argument, 
e.g.

program: error: the following arguments are required: choice

But with SUPPRESS I get a further error as _parse_known_args tries to format 
this error message:

C:\Users\paul\Miniconda3\lib\argparse.py in _parse_known_args(self, 
arg_strings, namespace)
   1991 if required_actions:
   1992 self.error(_('the following arguments are required: %s') %
-> 1993', '.join(required_actions))

TypeError: sequence item 0: expected str instance, NoneType found

(This error may have been reported elsewhere.  This issue is the first one I 
found that deals with `dest=argparse.SUPPRESS`.)

--

___
Python tracker 

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



[issue20430] Make argparse.SUPPRESS work as an argument dest

2014-05-28 Thread jzwinck

jzwinck added the comment:

Yes, I have a practical need for dest=SUPPRESS.  One of the reasons is 
basically what you said: to implement things analogous to --help.  In those 
cases you want to be able to invoke a function (e.g. via type=myfunc) but not 
store anything.  Or you may need to ignore an argument completely for 
compatibility, but you have logic which expects all the arguments returned by 
parse_args() to be known and meaningful.  In any case, I wrote this ticket 
because I ran into a concrete need that argparse didn't readily support (which 
almost never happens!).

As for what the usage statement should show: it should behave as if 
dest=SUPPRESS were not present.  If metavar is specified, use that, otherwise 
use the option name as if dest were not passed in.  And we already have the 
behavior that help=SUPPRESS will hide it from the usage entirely, so that 
should still work (i.e. dest=SUPPRESS, help=SUPPRESS should hide the option 
both from --help and the result of parse_args()).

--

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



[issue20430] Make argparse.SUPPRESS work as an argument dest

2014-05-28 Thread paul j3

paul j3 added the comment:

In the attached file I tried another approach - a custom Action class.  It 
gives you the custom behavior now, instead of 2-3 releases in the future.

class Action2(Action):
# custom action
def __init__(self, *args, **kwargs):
super(Action2, self).__init__(*args, **kwargs)
self.default = SUPPRESS 
def __call__(self, parser, namespace, values, option_string=None):
   print('Action2', argparse._get_action_name(self), values)

'self.default=SUPPRESS' keeps the dest out of the namespace without affecting 
help display.  And as with '_HelpAction', the __call__ can do its own thing 
without modifying the namespace.

One thing that this custom Action class does not do well is let you modify the 
handling of the argument after it is created.  For example if an argument is 
created by an imported parent parser, attributes like 'dest' and 'default' can 
be changed after the fact, but the argument class can't.

In   http://bugs.python.org/issue14191  I wrestled with the issue of 
temporarily disabling subsets of the arguments, first the positionals, and then 
the optionals, so I could run 'parse_known_args' separately on the two groups.

For optionals it was enough to ensure that 'required=False'.  For positionals I 
first used 'nargs=0', and latter enabled a 'nargs=SUPPRESS' option to suppress 
them.

--
Added file: http://bugs.python.org/file35389/issue20430.py

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



[issue20430] Make argparse.SUPPRESS work as an argument dest

2014-05-27 Thread paul j3

paul j3 added the comment:

If we make this change to '_StoreAction', we need to do it to 4 other 
subclasses which take the same 'setattr(namespace, self.dest, values)'.

An alternative would be to define a 'Namespace' function that does this 
conditional 'setattr'.

How should this 'SUPPRESS' affect the usage and help?  I'm seeing --foo 
==SUPPRESS==.

Do you have practical need for such a 'SUPPRESS', or are you just trying to 
make behavior consistent?  'SUPPRESS' is used for '--help' and subparsers 
because those Actions have important side effects, and we don't want default 
values to appear in the Namespace.  With '_StoreAction', the only effect is to 
store a value in the Namespace.

--
nosy: +paul.j3

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



[issue20430] Make argparse.SUPPRESS work as an argument dest

2014-01-28 Thread jzwinck

Changes by jzwinck jzwi...@gmail.com:


--
title: argparse.SUPPRESS - Make argparse.SUPPRESS work as an argument dest

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