15
15
import argparse
16
16
import warnings
17
17
18
+ from enum import StrEnum
18
19
from test .support import os_helper , captured_stderr
19
20
from unittest import mock
20
21
@@ -952,6 +953,35 @@ class TestDisallowLongAbbreviationAllowsShortGroupingPrefix(ParserTestCase):
952
953
]
953
954
954
955
956
+ class TestStrEnumChoices (TestCase ):
957
+ class Color (StrEnum ):
958
+ RED = "red"
959
+ GREEN = "green"
960
+ BLUE = "blue"
961
+
962
+ def test_metavar_formatter_with_strenum (self ):
963
+ parser = argparse .ArgumentParser ()
964
+ parser .add_argument ('--color' , choices = self .Color )
965
+ args = parser .parse_args (['--color' , 'red' ])
966
+ self .assertEqual (args .color , self .Color .RED )
967
+
968
+ def test_expand_help_with_strenum (self ):
969
+ parser = argparse .ArgumentParser ()
970
+ parser .add_argument ('--color' , choices = self .Color , help = 'Choose a color' )
971
+ help_output = parser .format_help ()
972
+ self .assertIn ('[--color {red,green,blue}]' , help_output )
973
+ self .assertIn (' --color {red,green,blue}' , help_output )
974
+
975
+ def test_check_value_with_strenum (self ):
976
+ parser = argparse .ArgumentParser (exit_on_error = False )
977
+ parser .add_argument ('--color' , choices = self .Color )
978
+ self .assertRaisesRegex (
979
+ argparse .ArgumentError ,
980
+ r"invalid choice: yellow \(choose from red, green, blue\)" ,
981
+ parser .parse_args ,
982
+ ['--color' , 'yellow' ],
983
+ )
984
+
955
985
# ================
956
986
# Positional tests
957
987
# ================
@@ -2399,7 +2429,7 @@ def test_wrong_argument_subparsers_no_destination_error(self):
2399
2429
parser .parse_args (('baz' ,))
2400
2430
self .assertRegex (
2401
2431
excinfo .exception .stderr ,
2402
- r"error: argument {foo,bar}: invalid choice: ' baz' \(choose from ' foo', ' bar' \)\n$"
2432
+ r"error: argument {foo,bar}: invalid choice: baz \(choose from foo, bar\)\n$"
2403
2433
)
2404
2434
2405
2435
def test_optional_subparsers (self ):
@@ -6061,7 +6091,7 @@ def test_subparser(self):
6061
6091
args = parser .parse_args (['x' , '--' , 'run' , '--' , 'a' , '--' , 'b' ])
6062
6092
self .assertEqual (NS (foo = 'x' , f = None , bar = ['a' , '--' , 'b' ]), args )
6063
6093
self .assertRaisesRegex (argparse .ArgumentError ,
6064
- "invalid choice: '--' " ,
6094
+ "invalid choice: -- " ,
6065
6095
parser .parse_args , ['--' , 'x' , '--' , 'run' , 'a' , 'b' ])
6066
6096
6067
6097
def test_subparser_after_multiple_argument_option (self ):
@@ -6075,7 +6105,7 @@ def test_subparser_after_multiple_argument_option(self):
6075
6105
args = parser .parse_args (['--foo' , 'x' , 'y' , '--' , 'run' , 'a' , 'b' , '-f' , 'c' ])
6076
6106
self .assertEqual (NS (foo = ['x' , 'y' ], f = 'c' , bar = ['a' , 'b' ]), args )
6077
6107
self .assertRaisesRegex (argparse .ArgumentError ,
6078
- "invalid choice: '--' " ,
6108
+ "invalid choice: -- " ,
6079
6109
parser .parse_args , ['--foo' , 'x' , '--' , '--' , 'run' , 'a' , 'b' ])
6080
6110
6081
6111
0 commit comments