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.

Author riccardomurri
Recipients riccardomurri
Date 2012年08月31日.16:08:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1346429290.76.0.112195597635.issue15832@psf.upfronthosting.co.za>
In-reply-to
Content
The `argparse` module (tested with 2.7, but other versions might be
affected) checks the `default` value of an option too early: if the
default value raises an exception, then command-line parsing stops.
Consider for example the following code:
############### begin sample #####################
import os
import argparse
def existing_filename(path):
 if not os.access(path, os.F_OK | os.R_OK):
 raise argparse.ArgumentTypeError("File '%s' does not exist or cannot be read." % path)
 return path
parser = argparse.ArgumentParser(description='Process a file.')
parser.add_argument('file', type=existing_filename, default='/some/weird/default',
 help='A file to process.')
args = parser.parse_args()
print ("Will process file '%s' ..." % args.file)
############### end sample #####################
The intention here is that the default value should be used *if and
only if* the script users do not provide their own file to process.
It may happen that the default is invalid, but that should be reported
as an error only if the default is used, i.e., users did not provide
any file name to the script.
What happens instead is that the argparse errors out in any case, even
when `--help` is requested or when a valid file name is passed, as the
following two examples show:
 rmurri@xenia:/tmp$ ./argparse-processes-defaults-too-early.py --help
 Traceback (most recent call last):
 File "./argparse-processes-defaults-too-early.py", line 18, in <module>
 args = parser.parse_args()
 File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
 args, argv = self.parse_known_args(args, namespace)
 File "/usr/lib/python2.7/argparse.py", line 1710, in parse_known_args
 default = self._get_value(action, default)
 File "/usr/lib/python2.7/argparse.py", line 2239, in _get_value
 raise ArgumentError(action, msg)
 argparse.ArgumentError: argument file: File '/some/weird/default' does not exist or cannot be read.
 rmurri@xenia:/tmp$ ./argparse-processes-defaults-too-early.py /tmp/xxx.py 
 Traceback (most recent call last):
 File "./argparse-processes-defaults-too-early.py", line 18, in <module>
 args = parser.parse_args()
 File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
 args, argv = self.parse_known_args(args, namespace)
 File "/usr/lib/python2.7/argparse.py", line 1710, in parse_known_args
 default = self._get_value(action, default)
 File "/usr/lib/python2.7/argparse.py", line 2239, in _get_value
 raise ArgumentError(action, msg)
 argparse.ArgumentError: argument file: File '/some/weird/default' does not exist or cannot be read.
Thanks!
History
Date User Action Args
2012年08月31日 16:08:10riccardomurrisetrecipients: + riccardomurri
2012年08月31日 16:08:10riccardomurrisetmessageid: <1346429290.76.0.112195597635.issue15832@psf.upfronthosting.co.za>
2012年08月31日 16:08:10riccardomurrilinkissue15832 messages
2012年08月31日 16:08:09riccardomurricreate

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