@@ -1934,6 +1934,46 @@ def setUp(self):
19341934 ('-x - -' , NS (x = eq_bstdin , spam = eq_bstdin )),
19351935 ]
19361936
1937+ class TestChoices (ParserTestCase ):
1938+ """Test the original behavior"""
1939+ def to_dow (arg ):
1940+ days = ["mo" , "tu" , "we" , "th" , "fr" , "sa" , "su" ]
1941+ if arg in days :
1942+ return days .index (arg ) + 1
1943+ else :
1944+ return None
1945+
1946+ argument_signatures = [
1947+ Sig ('when' ,
1948+ type = to_dow , choices = [1 , 2 , 3 , 4 , 5 , 6 , 7 ],
1949+ )
1950+ ]
1951+ failures = ['now' , '1' ]
1952+ successes = [
1953+ ('mo' , NS (when = 1 )),
1954+ ('su' , NS (when = 7 )),
1955+ ]
1956+
1957+ class TestTypedChoices (TestChoices ):
1958+ """Test a set of string choices that convert to weekdays"""
1959+
1960+ parser_signature = Sig (convert_choices = True )
1961+ argument_signatures = [
1962+ Sig ('when' ,
1963+ type = TestChoices .to_dow , choices = ["mo" , "tu" , "we" , "th" , "fr" , "sa" , "su" ],
1964+ )
1965+ ]
1966+
1967+ class TestTypedChoicesNoFlag (TestChoices ):
1968+ """Without the feature flag we fail"""
1969+ argument_signatures = [
1970+ Sig ('when' ,
1971+ type = TestChoices .to_dow , choices = ["mo" , "tu" , "we" , "th" , "fr" , "sa" , "su" ],
1972+ )
1973+ ]
1974+ failures = ['mo' ]
1975+ successes = []
1976+
19371977
19381978class WFile (object ):
19391979 seen = set ()
@@ -5468,6 +5508,39 @@ def custom_type(string):
54685508 version = ''
54695509
54705510
5511+ class TestHelpTypedChoices (HelpTestCase ):
5512+ from datetime import date , timedelta
5513+ def to_date (arg ):
5514+ if arg == "today" :
5515+ return date .today ()
5516+ elif arg == "tomorrow" :
5517+ return date .today () + timedelta (days = 1 ).date ()
5518+ else :
5519+ return None
5520+
5521+ parser_signature = Sig (prog = 'PROG' , convert_choices = True )
5522+ argument_signatures = [
5523+ Sig ('when' ,
5524+ type = to_date ,
5525+ choices = ["today" , "tomorrow" ]
5526+ ),
5527+ ]
5528+
5529+ usage = '''\
5530+ usage: PROG [-h] {today,tomorrow}
5531+ '''
5532+ help = usage + '''\
5533+
5534+ positional arguments:
5535+ {today,tomorrow}
5536+
5537+ options:
5538+ -h, --help show this help message and exit
5539+ '''
5540+ version = ''
5541+
5542+
5543+
54715544class TestHelpUsageLongSubparserCommand (TestCase ):
54725545 """Test that subparser commands are formatted correctly in help"""
54735546 maxDiff = None
0 commit comments