[issue22049] argparse: type= doesn't honor nargs > 1
New submission from Chris Bruner: >From the documentation, I think that argparse should pass the entire >nargs-long string to the "type=" callable. Instead, it only passes the first >argument (of nargs), making it impossible within argparse's framework to check >for a tuple of mixed types, e.g., 2 ints and a float (my case), or a string >and three numbers, etc. See attached reproducer. -- files: argparse_typedef_bug.py messages: 223748 nosy: Chris.Bruner priority: normal severity: normal status: open title: argparse: type= doesn't honor nargs > 1 type: behavior versions: Python 2.7 Added file: http://bugs.python.org/file36048/argparse_typedef_bug.py ___ Python tracker <http://bugs.python.org/issue22049> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22050] argparse: read nargs > 1 options from file doesn't work
New submission from Chris Bruner: When reading options from a file, argparse should read all values from each line. Instead, it complains of there not being enough options. python file: #!/usr/bin/env python import argparse p = argparse.ArgumentParser(description='Reproduce argparse nargs file bug', fromfile_prefix_chars='@') p.add_argument('-t', '--triple', metavar='N', nargs=3) args = p.parse_args(['@input.opts']) print args input.opts: --triple 1 2 3 -t 2 2 32 Output: bernoulli:myclu cwbrune$ argparse_nargs_file_bug.py usage: argparse_nargs_file_bug.py [-h] [-t N N N] argparse_nargs_file_bug.py: error: argument -t/--triple: expected 3 argument(s) -- files: argparse_nargs_file_bug.py messages: 223753 nosy: Chris.Bruner priority: normal severity: normal status: open title: argparse: read nargs > 1 options from file doesn't work type: behavior versions: Python 2.7 Added file: http://bugs.python.org/file36049/argparse_nargs_file_bug.py ___ Python tracker <http://bugs.python.org/issue22050> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22050] argparse: read nargs > 1 options from file doesn't work
Chris Bruner added the comment: Tried the format you gave. Still doesn't work. Same error message: bernoulli:myclu cwbrune$ argparse_nargs_file_bug.py usage: argparse_nargs_file_bug.py [-h] [-t N N N] argparse_nargs_file_bug.py: error: argument -t/--triple: expected 3 argument(s) bernoulli:myclu cwbrune$ cat argparse_nargs_file_bug.py #!/usr/bin/env python import argparse p = argparse.ArgumentParser(description='Reproduce argparse nargs file bug', fromfile_prefix_chars='@') p.add_argument('-t', '--triple', metavar='N', nargs=3) args = p.parse_args(['@input.opts']) print args bernoulli:myclu cwbrune$ cat input.opts --triple 1 2 3 -t 2 2 32 bernoulli:myclu cwbrune$ -- ___ Python tracker <http://bugs.python.org/issue22050> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22050] argparse: read nargs > 1 options from file doesn't work
Chris Bruner added the comment: Oops, my mistake: had multiple '-t' in file, but not an "action=append". Your version works just fine. Sorry for the confusion, and thank you for the correction! -- ___ Python tracker <http://bugs.python.org/issue22050> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22049] argparse: type= doesn't honor nargs > 1
Chris Bruner added the comment: Yes, I know. My function just sees '1', but I think it should see '1 2 3' so that it can figure out what to do. That's impossible (well, impossible without saving state between calls) when it sees the arguments piecemeal. Sent from my iPhone > On Jul 24, 2014, at 9:42 PM, paul j3 wrote: > > > 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 thing in your function to see what we mean. > > -- > nosy: +paul.j3 > > ___ > Python tracker > <http://bugs.python.org/issue22049> > ___ -- ___ Python tracker <http://bugs.python.org/issue22049> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22049] argparse: type= doesn't honor nargs > 1
Chris Bruner added the comment: Just had a chance to try this, and this does exactly what I wanted from "type=". Thank you! On Fri, Jul 25, 2014 at 4:17 PM, paul j3 wrote: > > 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' % (namespace, values, option_string)) > ... setattr(namespace, self.dest, values) > > 'values' will be the list ['1','2','3'], which you test and manipulate, > before finally saving it to the 'namespace'. > > ret = (int(values[0]), int(values[1]), float(values[2])) > setattr(namespace, self.dest, ret) > > Setting 'nargs=3' ensures that this action will always get a 3 item list. > If the parser can't give it 3 items, it will raise an error rather than > call your Action. > > 'optparse' passed the remaining argument strings to Option's callback, > which could consume as many as it wanted. 'argparse' does not give the > Actions that power. There is a fundamental difference in the parsing > algorithm. > > -- > > ___ > Python tracker > <http://bugs.python.org/issue22049> > ___ > -- ___ Python tracker <http://bugs.python.org/issue22049> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com