Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
f97c59aa
Commit
f97c59aa
authored
Jun 09, 2011
by
R David Murray
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#10424: argument names are now included in the missing argument message
Fix and initial test patch by Michele Orrù.
parent
8dd8d582
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
10 deletions
+69
-10
Lib/argparse.py
Lib/argparse.py
+5
-10
Lib/test/test_argparse.py
Lib/test/test_argparse.py
+61
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/argparse.py
View file @
f97c59aa
...
...
@@ -1969,17 +1969,12 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
# if we didn't consume all the argument strings, there were extras
extras
.
extend
(
arg_strings
[
stop_index
:])
# if we didn't use all the Positional objects, there were too few
# arg strings supplied.
if
positionals
:
self
.
error
(
_
(
'too few arguments'
))
# make sure all required actions were present
for
action
in
self
.
_actions
:
if
action
.
required
:
if
action
not
in
seen
_actions
:
name
=
_get_action_name
(
action
)
self
.
error
(
_
(
'argument %s is required'
)
%
name
)
required_actions
=
[
_get_action_name
(
action
)
for
action
in
self
.
_actions
if
action
.
required
and
action
not
in
seen_actions
]
if
required
_actions
:
self
.
error
(
_
(
'the following arguments are required: %s'
)
%
', '
.
join
(
required_actions
)
)
# make sure all required groups had one option present
for
group
in
self
.
_mutually_exclusive_groups
:
...
...
Lib/test/test_argparse.py
View file @
f97c59aa
...
...
@@ -4480,6 +4480,67 @@ class TestArgumentTypeError(TestCase):
else
:
self
.
fail
()
# =========================
# MessageContentError tests
# =========================
class
TestMessageContentError
(
TestCase
):
def
test_missing_argument_name_in_message
(
self
):
parser
=
ErrorRaisingArgumentParser
(
prog
=
'PROG'
,
usage
=
''
)
parser
.
add_argument
(
'req_pos'
,
type
=
str
)
parser
.
add_argument
(
'-req_opt'
,
type
=
int
,
required
=
True
)
parser
.
add_argument
(
'need_one'
,
type
=
str
,
nargs
=
'+'
)
with
self
.
assertRaises
(
ArgumentParserError
)
as
cm
:
parser
.
parse_args
([])
msg
=
str
(
cm
.
exception
)
self
.
assertRegex
(
msg
,
'req_pos'
)
self
.
assertRegex
(
msg
,
'req_opt'
)
self
.
assertRegex
(
msg
,
'need_one'
)
with
self
.
assertRaises
(
ArgumentParserError
)
as
cm
:
parser
.
parse_args
([
'myXargument'
])
msg
=
str
(
cm
.
exception
)
self
.
assertNotIn
(
msg
,
'req_pos'
)
self
.
assertRegex
(
msg
,
'req_opt'
)
self
.
assertRegex
(
msg
,
'need_one'
)
with
self
.
assertRaises
(
ArgumentParserError
)
as
cm
:
parser
.
parse_args
([
'myXargument'
,
'-req_opt=1'
])
msg
=
str
(
cm
.
exception
)
self
.
assertNotIn
(
msg
,
'req_pos'
)
self
.
assertNotIn
(
msg
,
'req_opt'
)
self
.
assertRegex
(
msg
,
'need_one'
)
def
test_optional_optional_not_in_message
(
self
):
parser
=
ErrorRaisingArgumentParser
(
prog
=
'PROG'
,
usage
=
''
)
parser
.
add_argument
(
'req_pos'
,
type
=
str
)
parser
.
add_argument
(
'--req_opt'
,
type
=
int
,
required
=
True
)
parser
.
add_argument
(
'--opt_opt'
,
type
=
bool
,
nargs
=
'?'
,
default
=
True
)
with
self
.
assertRaises
(
ArgumentParserError
)
as
cm
:
parser
.
parse_args
([])
msg
=
str
(
cm
.
exception
)
self
.
assertRegex
(
msg
,
'req_pos'
)
self
.
assertRegex
(
msg
,
'req_opt'
)
self
.
assertNotIn
(
msg
,
'opt_opt'
)
with
self
.
assertRaises
(
ArgumentParserError
)
as
cm
:
parser
.
parse_args
([
'--req_opt=1'
])
msg
=
str
(
cm
.
exception
)
self
.
assertRegex
(
msg
,
'req_pos'
)
self
.
assertNotIn
(
msg
,
'req_opt'
)
self
.
assertNotIn
(
msg
,
'opt_opt'
)
def
test_optional_positional_not_in_message
(
self
):
parser
=
ErrorRaisingArgumentParser
(
prog
=
'PROG'
,
usage
=
''
)
parser
.
add_argument
(
'req_pos'
)
parser
.
add_argument
(
'optional_positional'
,
nargs
=
'?'
,
default
=
'eggs'
)
with
self
.
assertRaises
(
ArgumentParserError
)
as
cm
:
parser
.
parse_args
([])
msg
=
str
(
cm
.
exception
)
self
.
assertRegex
(
msg
,
'req_pos'
)
self
.
assertNotIn
(
msg
,
'optional_positional'
)
# ======================
# parse_known_args tests
# ======================
...
...
Misc/NEWS
View file @
f97c59aa
...
...
@@ -187,6 +187,9 @@ Core and Builtins
Library
-------
- Issue #10424: Argparse now includes the names of the missing required
arguments in the missing arguments error message.
- Issue #12168: SysLogHandler now allows NUL termination to be controlled using
a new '
append_nul
' attribute on the handler.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment