New submission from Paolo Benvenuto :
I'm using argparse with this code:
parser = argparse.ArgumentParser(
description='Scan a media tree in order to generate cache files suitable
for showing a beautiful web gallery',
)
parser.add_argument(
"config_file_or_album_path",
help="the config file, if it's the only positional argument, or the album
path, if two positional arguments are supplied"
)
parser.add_argument(
"cache_path",
nargs="?",
default="",
help="the cache path, if two positional arguments are supplied; otherwise,
nothing"
)
parser.add_argument(
"-s",
"--periodic-save",
nargs="?",
type=int,
const=5,
default=0,
dest="periodic_save",
metavar="MINUTES",
help="runs the scanner in a more robust way, saving json files every X
minutes, where X is the value given, or 5 if no value is given; 0 means non
autosaving"
)
parser.add_argument(
"-j",
"--recreate-json-files",
action='store_true',
dest="recreate_json_files",
help="completely recreate the json files cache"
)
parser.add_argument(
"-r",
"--recreate-reduced-photos",
action='store_true',
dest="recreate_reduced_photos",
help="completely recreate reduced photo cache files"
)
parser.add_argument(
"-t",
"--recreate-thumbnails",
action='store_true',
dest="recreate_thumbnails",
help="completely recreate thumbnail cache files"
)
parser.add_argument(
"-v",
"--recreate-transcoded-videos",
action='store_true',
dest="recreate_transcoded_videos",
help="completely recreate transcoded video cache files"
)
parser.add_argument(
"-a",
"--recreate-all",
action='store_true',
dest="recreate_all",
help="completely recreate the cache: json files, reduced photos, thumbnails
and transcoded videos; same as -jrtv"
)
parser.add_argument("--version", action="version", version='%(prog)s v5.3.10')
args = parser.parse_args()
Running the app without parameter gives me:
$ myapp
usage: scanner [-h] [-s [MINUTES]] [-j] [-r] [-t] [-v] [-a] [--version]
config_file_or_album_path [cache_path]
myapp: error: the following arguments are required: config_file_or_album_path
(the last line doesn't matter here, a required parameter is missing).
The -s option is optional and has an optional value, but:
$ myapp -s myfile
usage: scanner [-h] [-s [MINUTES]] [-j] [-r] [-t] [-v] [-a] [--version]
config_file_or_album_path [cache_path]
scanner: error: argument -s/--periodic-save: invalid int value: '/my/file'
I'm expecting that, since there is a mandatory positional argument, myfile is
interpreted as such, and only after that, -s is recognized as the optional
argument without value.
--
components: Library (Lib)
messages: 405322
nosy: paolobenve
priority: normal
severity: normal
status: open
title: argparse error with option with optional value
type: behavior
versions: Python 3.8
___
Python tracker
<https://bugs.python.org/issue45673>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com