paul j3 added the comment:
In http://bugs.python.org/issue11588#msg212243 I explore the option of using
decorators to 'register' custom functions with the parser. There I was adding
exclusive/inclusive tests, but the same approach could be used to register
custom types and a
paul j3 added the comment:
This is not getting much attention for several reasons:
- there's quite a backlog of argparse patches and issues
- 'set_defaults' is not commonly used. Setting default in 'add_argument' seems
more common.
- defining the same argumen
Changes by paul j3 :
--
nosy: +paul.j3
___
Python tracker
<http://bugs.python.org/issue12776>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.pyth
paul j3 added the comment:
I think is issue was already raised in
http://bugs.python.org/issue14074
argparse allows nargs>1 for positional arguments but doesn't allow metavar to
be a tuple
--
nosy: +paul.j3
___
Python tracke
paul j3 added the comment:
You can specify the 'formatter_class' when creating each subparser:
sp1=sp.add_parser('cmd1', formatter_class =
argparse.RawDescriptionHelpFormatter)
The 'add_parser' command is the one that passes a variety of **kwargs to
'A
paul j3 added the comment:
Another way to add an existing Action to a group is to modify the
'add_argument' method for the Group subclass. For example we could add this to
the _MutuallyExclusiveGroup class:
def add_argument(self, *args, **kwargs):
# allow adding a
paul j3 added the comment:
First, 'parse_intermixed_args' on stack is not relevant. It's from an
unreleased patch that we worked on.
What matters is the 'print_help', invoked probably with a '-h'.
The error message that normally specifies the problem arg
paul j3 added the comment:
The ''_expand_help' method formats one action (argument) at a time, so it could
issue an error message that includes that action's name.
The disconnect that you noticed arises because your bad 'help' parameter wasn't
tested until i
paul j3 added the comment:
In http://bugs.python.org/file30010/nargswarn.patch adding the
'_expand_help(action)' line should test the help string (during add_argument).
def _check_argument(self, action):
# check action arguments
# focus on the arguments that
paul j3 added the comment:
http://bugs.python.org/issue21666
raises the possibility of testing the 'help' parameter in the same way. By
adding (to _check_argument):
# check the 'help' string
try:
self._get_formatter()._expand_help(action)
paul j3 added the comment:
I have developed a UsageGroup class that can implement nested 'inclusive'
tests. Using this, the original example in this issue could be coded as 3
groups, 2 mutually_exclusive and inclusive one.
parser = ArgumentParser(prog='PROG
paul j3 added the comment:
It's essentially the same issue as http://bugs.python.org/issue21666.
An error in the 'help' parameter is not caught until 'print_help' is called
(via 'parse_args').
A possible improvement is to test the 'help' string
paul j3 added the comment:
It should be easy to write a subclass of Action, or append Action, that does
what you want. It just needs a different `__call__` method. You just need a
way of identifying an default that needs to be overwritten as opposed to
appended to.
def __call__(self
New submission from paul j3:
Argparse
15.4.5.1. Sub-commands
In the example:
>>> parser.parse_args(['--help'])
usage: PROG [-h] [--foo] {a,b} ...
positional arguments:
{a,b} sub-command help
a a help
b b help
optional arguments:
-h, --help show this he
Changes by paul j3 :
--
nosy: +paul.j3
___
Python tracker
<http://bugs.python.org/issue11708>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.pyth
Changes by paul j3 :
--
nosy: +paul.j3
___
Python tracker
<http://bugs.python.org/issue13824>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.pyth
Changes by paul j3 :
--
nosy: +paul.j3
___
Python tracker
<http://bugs.python.org/issue14365>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.pyth
Changes by paul j3 :
--
nosy: +paul.j3
___
Python tracker
<http://bugs.python.org/issue9694>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.pyth
paul j3 added the comment:
Another situation in which this MultiGroupHelpFormatter helps is when one or
more of the groups includes an optional positional.
The regular formatter moves all the positionals to the end, after the
optionals. This move could easily break up a mutually exclusive
paul j3 added the comment:
On a related point, the 'action.required' value is set differently for '?' and
'*' positionals.
if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]:
kwargs['required'] = True
if kwargs.get(
paul j3 added the comment:
This is a HelpFormatter function that takes a list of formatted actions, and
groups contiguous blocks of optional positional actions. It accounts for
optionals (via prefix_chars) and mutually exclusive groups.
Since it assumes 'parts' is a list, rather t
paul j3 added the comment:
In this patch I implement a FileContext class. It differs from FileType
in 2 key areas:
- it returns a 'partial(open, filename, ...)'
- it wraps '-' (stdin/out) in a dummy context protecting the file from closure.
The resulting argument is
paul j3 added the comment:
An alternative way to delay the file opening, is to return an instance that has
a `filename` attribute, and has an `open` method. This can be compactly added
to the `FileContext` that I defined in the previous patch. The `FileContext`
provides the `_ostest
paul j3 added the comment:
Steven's patch (subparse_optionals.diff) run with jakub's test case
(argparse_subparses_ambiguous_bug.py) works. But if the input string is
print(parser.parse_args('--foo baz'.split()))
produces
Namespace(cmd=None, foo='baz
paul j3 added the comment:
I think the correction to the problem that I noted in the previous post is to
return 'None, arg_string, None', rather than 'action, arg_string, None' in the
case where the action is found in a subparser.
This is what '_parse_optional'
paul j3 added the comment:
In the last patch, 'parser.scan = True' is needed to activate this fix. I left
that switch in for testing convenience.
--
___
Python tracker
<http://bugs.python.o
Changes by paul j3 :
Removed file: http://bugs.python.org/file31888/subparser_patch.diff
___
Python tracker
<http://bugs.python.org/issue14365>
___
___
Python-bugs-list m
paul j3 added the comment:
This the argparse patch as described in the previous post, along with
test_argparse tests.
For now the tests handle both subparser required cases and not required ones (
http://bugs.python.org/issue9253 ). While error messages can differ based on
this
paul j3 added the comment:
Looks like this behavior is intentional. This subclass adds a '%(default)s'
string to the help lines if:
if '%(default)' not in action.help:
if action.default is not SUPPRESS:
defaulting_nargs = [OPTIONAL, ZERO_OR_
paul j3 added the comment:
parse_args() would see ['-foo', 'bar1,bar2,bar3', 'pos1', 'pos2']. The
splitting on space is done by the shell. So having your own code split
'bar1,bar2,bar3' is simplest. But that would be messed up if the user e
paul j3 added the comment:
When you add an argument, argparse creates an `Action`, and returns it. It
also places that action in various lists (e.g. parse._actions) and
dictionaries. A `remove_argument` function would have to trace and remove all
of those links. That's a non-trivial
paul j3 added the comment:
Just hang on the Action object that the `add_argument` returned, and change its
`help` attribute.
a = parser.add_argument('--foo', help='initial help')
a.help = 'new help'
If using a custom parser class and s
paul j3 added the comment:
`argparse.SUPPRESS` should do the trick. According to the documentation it can
be used with `default` and `help` parameters.
--
___
Python tracker
<http://bugs.python.org/issue19
paul j3 added the comment:
f.nargs = '?'
f.default = argparse.SUPPRESS
f.help = argparse.SUPPRESS
may be best set of tweaks to a positional Action `f`. In quick tests it
removes `f` from the help, suppresses any complaints about a missing string,
and does not put anyth
paul j3 added the comment:
http://stackoverflow.com/a/20167038/901925
is an example of using `_parser_class` to produce different behavior in the
subparsers.
parser = ArgumentParser()
parser.add_argument('foo')
sp = parser.add_subparsers(dest='cmd')
sp._pars
paul j3 added the comment:
`parse_args` just calls `parse_known_args`, and then raises an error if `rest`
is not empty. So it is `parse_known_args` that is doing the abbreviation
matching.
Abbreviation matching is done relatively early, when `_parse_known_args` first
loops through the
paul j3 added the comment:
For a programmer who needs to turn off this abbreviation matching now, a simple
solution is to subclass ArgumentParser:
class MyParser(ArgumentParser):
def _get_option_tuples(self, option_string):
return []
This could be the place to
paul j3 added the comment:
With a minor tweak to `argparse._is_mnrep()`, `nargs='{3}'` would also work.
This means the same as `nargs=3`, so it isn't needed. But it is consistent
with the idea of accepting Regular Expression syntax where it makes sense.
`nargs='{2,3}?&
paul j3 added the comment:
Here's a function that implements the format string:
def custom_help(template):
def usage(self):
formatter = self._get_formatter()
formatter.add_usage(self.usage, self._actions,
self._mutually_exclusive_g
paul j3 added the comment:
Another approach would be make the 'argparse' argument a 'switch' command, and
act on it after parsing. Roughly what I have in mind is:
parser.add_argument('--verbosity', dest='switch_verbosity,
action='store_tru
paul j3 added the comment:
This patch has a 'custom_help' which, with a default template, is compatible
with 'format_help' (i.e. it passes test_argparse). It also handles the sample
template in this issue.
Due to long line wrapping issues, the 'Usage: ' strin
paul j3 added the comment:
That original template can also be implemented with a customized 'format_help':
def custom_help(self):
formatter = self._get_formatter()
formatter.add_text('My Program, version 3.5')
formatter.add_usage(self.
paul j3 added the comment:
This patch accomplishes this task by adding a _PseudoGroup class to
_SubParsersAction. It's like the _ChoicesPseudoAction except that it maintains
its own _choices_actions list.
It takes advantage of the fact that formatter._format_actions is recursive whe
Changes by paul j3 :
--
nosy: +paul.j3
___
Python tracker
<http://bugs.python.org/issue1859>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.pyth
paul j3 added the comment:
It will take a while to reconstruct the circumstances behind this issue. I
think I was working through some online class examples, working in the Eclipse
pydev environment. Currently I'm mostly working in linux, and not doing much
with multiprocessing. Looks
paul j3 added the comment:
I wonder if this patch is needed.
- there hasn't been discussion in 4 years
- In Steven's use case, a group without any arguments, the group does not show
up. A common example of an empty argument group, is a parser without any user
defined arguments
paul j3 added the comment:
I believe http://bugs.python.org/issue14174 with REMAINDER has its roots in the
same issue - parse_args tries to process as many positionals as it can at a
time, regardless of what's left in the argument strings.
The fix proposed here depends on the 2nd arg
Changes by paul j3 :
--
nosy: +paul.j3
___
Python tracker
<http://bugs.python.org/issue20554>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.pyth
paul j3 added the comment:
Here's a possible solution to the problem (assuming there really is one):
- redefine REMAINDER so it matches a '((?:A[AO]*)?)' pattern (where O is a
string that looks like an optional flag, A an argument string). I've added the
condition that
paul j3 added the comment:
The documentation currently warns
https://docs.python.org/2/library/multiprocessing.html#windows
> Safe importing of main module
> Make sure that the main module can be safely imported by a new Python
> interpreter without causing unintended side effect
paul j3 added the comment:
Apart from sorting, `_get_kwargs` does little more than:
return [k for k in action.__dict__.items() if not k[0].startswith('_')]
That is, it's the `items()` of the 'public' attributes of the action (or
parser). Those attributes are
paul j3 added the comment:
In elsdoerfer's example, the '--' effectively ends the argument list for
'--ignore'. '--' are not allowed in the arguments of an optional (see the end
of '_get_nargs_pattern()').
Rather the problem is at the start of
paul j3 added the comment:
This patch modifies that '--' handling from 13922, removing '--' from PARSER
and REMAINDER if it is the first string:
if action.nargs not in [PARSER, REMAINDER]:
try:
arg_strings.remove('--')
paul j3 added the comment:
http://bugs.python.org/issue9571 proposes a refinement to this patch - drop the
'--' from PARSER and REMAINDER nargs if it is the 1st string.
--
___
Python tracker
<http://bugs.python.o
paul j3 added the comment:
This is a straight forward patch, modifying '_registry_get' to return 'default'
if it gets this 'TypeError'. 'test_argparse.py' has a testcase based on
jnothman's example. Temporarily it includes a skipped test that wo
paul j3 added the comment:
The issue here is how `textwrap` handles the non-breaking space. That's being
address here:
http://bugs.python.org/issue20491 textwrap: Non-breaking space not honored
Temporarily an `argparse` user can get around it with
`formatter_
paul j3 added the comment:
In this patch I added the '_()' localization to the '_VersionAction' default
'help'.
While this is a straight forward change, I haven't tested it. It does run
test_argparse.py, but that doesn't have any tests for localiz
paul j3 added the comment:
To put this issue in perspective:
- There's nothing in the documentation about nesting a mutually exclusive group
in an argument group.
- There are prominent notes in the documentation about MEGs not taking title
and description. The '_add_container_act
paul j3 added the comment:
This patch probably won't work with [parents]. see
http://bugs.python.org/issue16807
--
___
Python tracker
<http://bugs.python.org/i
paul j3 added the comment:
The subcommands grouping mechanism proposed in http://bugs.python.org/issue9341
probably does not work with [parents] either.
This _add_container_actions method is brittle. It has to know too much about
the structure of a parser and its groups. Any change to that
paul j3 added the comment:
Positionals with '*' are discussed in more detail in:
http://bugs.python.org/issue9625 - argparse: Problem with defaults for variable
nargs when using choices
http://bugs.python.org/issue16878 - argparse: positional args with nargs='*'
paul j3 added the comment:
The documentation patch here should note that a positional '*' string default
is not parsed (i.e. does not pass through _get_value).
(see http://bugs.python.org/issue17250)
--
___
Python tracker
<http://bu
paul j3 added the comment:
The latest patch, using _shutil.get_terminal_size(), looks fine.
It lets environ['COLUMNS'] have priority over the end user's terminal width, as
demonstrated by the change to test_argparse. test_argparse doesn't test
changing the actual
paul j3 added the comment:
I suspect fixing this isn't going to be easy.
Extra lines are removed by the top most `formatter` function:
def format_help(self):
help = self._root_section.format_help()
if help:
help = self._long_break_matcher.sub('\
paul j3 added the comment:
A user could preserve blank lines by including a space (or more) in each. That
is, instead of ending with '\n', end with '\n \n'.
epilog = 'Epilog: No wrap text %(prog)s\n\tNext line\n \n'
---
I just checked a simple hel
paul j3 added the comment:
As Steven notes, the patch lacks tests. It also lacks documentation.
The 2 things that this class does different from 'version' are
- write without passing the text through 'textwrap'
- write to a user defined file
There is a difference in opin
paul j3 added the comment:
I think you want to unlink 9ac347a7f375.diff
It's from a different project
--
___
Python tracker
<http://bugs.python.org/is
New submission from paul j3:
A number of the issues seek to customize the wrapping behavior in HelpFormatter
- beyond what the current Formatter subclasses offer.
http://bugs.python.org/issue13923 and http://bugs.python.org/issue12806 - want
a wrapping method that preserves existing \n, while
paul j3 added the comment:
One way of marking a string for special wrap handing is to make it an instance
of a subclass of 'str'.
This patch adds class _WhitespaceStyle(str), and 5 subclasses corresponding to
the possible values of the CSS whitespace:.
Normal
Pre
paul j3 added the comment:
For now the user could add this to his module:
import os, shutil
os.environ['COLUMNS'] = str(shutil.get_terminal_size().columns)
--
___
Python tracker
<http://bugs.python.o
paul j3 added the comment:
Note that
'-t 1 2 3'.split()
becomes
['-t', '1', '2', '3']
Your 'type' function sees those 3 strings individually. Try printing 'string'
the first
paul j3 added the comment:
That's an artifact of how the group usage is formatted (which isn't very
robust). It's not designed to handle nested groups.
Mutually exclusive groups aren't meant to nest inside other mutually exclusive
groups. While it possible,
paul j3 added the comment:
On further thought, I think
group2 = group1.add_mutually_exclusive_group()
should have raised an error, stating that a group (argument or mutually
exclusive) cannot be added to a mutually exclusive group.
Also an argument group should not be added to another
paul j3 added the comment:
Here's a preliminary patch that raises an error if there's an attempt to nest a
mutually exclusive group in another, or there's an attempt to add an argument
group to either kind of group.
It still needs test_argparse.py and argparse.rst changes
paul j3 added the comment:
What you want is a custom Action rather than a custom Type.
from the documentation:
>>> class FooAction(argparse.Action):
... def __call__(self, parser, namespace, values, option_string=None):
... print('%r %r %r' %
paul j3 added the comment:
ArgumentGroups and MutuallyExclusiveGroups, as currently defined, won't give
you that kind of usage. I have appended a script that uses UsageGroups, which
I am developing for http://bugs.python.org/issue11588,
to solve this.
It defines 2 'mxg' grou
paul j3 added the comment:
This patch adds a
class TestMutuallyExclusiveGroupErrors
test_invalid_add_group() test,
closely modeled on
test_invalid_add_argument()
I'm using ValueError in group add methods (maintaining the similarity with
add_argument errors).
I ha
Changes by paul j3 :
Added file: http://bugs.python.org/file36160/issue22029_2.patch
___
Python tracker
<http://bugs.python.org/issue22029>
___
___
Python-bugs-list mailin
paul j3 added the comment:
In http://bugs.python.org/issue22029 argparse
CSS white-space: like control for individual text blocks
I propose a set of `str` subclasses that can be used to define the wrapping
style of individual text blocks. The idea is adapted from the HTML '' tag,
a
Changes by paul j3 :
Added file: http://bugs.python.org/file36187/issue22029_2.patch
___
Python tracker
<http://bugs.python.org/issue22029>
___
___
Python-bugs-list mailin
Changes by paul j3 :
Removed file: http://bugs.python.org/file36160/issue22029_2.patch
___
Python tracker
<http://bugs.python.org/issue22029>
___
___
Python-bugs-list m
paul j3 added the comment:
In my suggestion I used
if 'current_value is default':
without going into detail. The use of an 'is' test for integer values is
tricky, as discussed in http://bugs.python.org/issue18943
int("42") is 42# True
int(&
paul j3 added the comment:
For documentation, ['this','is','a','test'] might be a bit clearer than 'this
is a test'.split(). But generating a list of strings by split is, I think, a
pretty basic idiom.
But for testing purposes the split() ver
paul j3 added the comment:
Another issue dealing with abbreviations is close to being committed.
http://bugs.python.org/issue14910
argparse: disable abbreviation
--
___
Python tracker
<http://bugs.python.org/issue14
paul j3 added the comment:
On Stackoverflow a couple of posters have asked about nesting namespaces as a
way of separating the parser and subparser arguments.
http://stackoverflow.com/questions/15782948
http://stackoverflow.com/questions/18668227
One solution that I rather like http
paul j3 added the comment:
Reading through, but not testing, it looks like a faithful replacement. The
usage like will be a bit different.
Given optparse:
usage='usage: %prog [-d] [-t] [input [output]]
automatic argparse:
usage='usage: %prog [-d] [-t] [input] [output]
paul j3 added the comment:
I added a print line to a 'windows' example from the documentation:
from multiprocessing import Process
print 'importing multiprocessing'
def foo():
print 'hello'
p = Process(target=foo)
p.start()
Run wi
paul j3 added the comment:
I had noticed the `global _log_to_stderr` in `util.log_to_stderr()`, but hadn't
taken time to track down where it is used.
Not only is it passed from the 'util' module to the 'forking' one, but it is
also passed via a piped pickle fro
paul j3 added the comment:
It still needs a documentation patch.
And if the documentation was clearer, would sfllaw's patch still be needed?
--
___
Python tracker
<http://bugs.python.org/is
paul j3 added the comment:
The formatting of the descriptor line for this class is not consistent with the
classes for this module.
It lacks 'class'.
It is all bold
In contrast
class argparse.ArgumentParser(prog=None, ...
uses bold for onl
paul j3 added the comment:
Maybe the problem is with the latest documentation build, not your patch.
--
___
Python tracker
<http://bugs.python.org/issue13
paul j3 added the comment:
You need line continuation characters '\'
.. class:: Action(option_strings, dest, nargs=None, const=None, default=None, \
type=None, choices=None, required=False, help=None, \
me
paul j3 added the comment:
While we are talking the Action class, the documentation isn't clear that the
'add_argument' method returns an Action object, specifically one the
subclasses. More than once, when answering Stackoverflow questions I've
recommended assign
paul j3 added the comment:
The patch tests assume the test is being run in a particular way:
python -m unittest ...
But I often use
python3 -m unittest ...
or
python3 test_argparse.py
both of which fail these tests.
Testing this change might be more difficult than implementing
paul j3 added the comment:
What if the package is run without `-m`?
--
___
Python tracker
<http://bugs.python.org/issue22240>
___
___
Python-bugs-list mailin
paul j3 added the comment:
I ran into a (possibly) related compiling problem (for 'default', 3.5 branch)
in `asdl.py`:
class Module(AST):
def __init__(self, name, dfns):
...
self.types = {type.name: type.value for type in dfns}
The dictionary com
paul j3 added the comment:
Package might be the wrong term. How about a directory with a `__main__.py`
file, and no local imports (relative or not)? In my tests it runs with and
without the -m.
--
___
Python tracker
<http://bugs.python.
paul j3 added the comment:
http://bugs.python.org/issue14191
'argparse doesn't allow optionals within positionals'
implements a 'parse_intermixed_args()' method, which parses all of the
optionals with one pass, followed by a second pass that handles the
posi
paul j3 added the comment:
http://stackoverflow.com/questions/25626109/python-argparse-conditionally-required-arguments
asks about implementing a 'conditionally-required-arguments' case in
`argparse`. The post-parsing test is simple enough:
if args.argument and (args.a is None
paul j3 added the comment:
Attached is a patch for 3.5.0 dev that adds UsageGroups. Now different 'kinds'
are implemented as subclasses, which are accessed via the registry:
_XorUsageGroup - replicate action of MutuallyExclusiveGroups
_AndUsageGroup - an inclu
401 - 500 of 778 matches
Mail list logo