@@ -1761,6 +1761,14 @@ def _get_parser(self, subparser_help=False, prefix_chars=None,
17611761 parser2 .add_argument ('-y' , choices = '123' , help = 'y help' )
17621762 parser2 .add_argument ('z' , type = complex , nargs = '*' , help = 'z help' )
17631763
1764+ # add third sub-parser
1765+ parser3_kwargs = dict (description = '3 description' )
1766+ if subparser_help :
1767+ parser3_kwargs ['help' ] = '3 help'
1768+ parser3 = subparsers .add_parser ('3' , ** parser3_kwargs )
1769+ parser3 .add_argument ('t' , type = int , help = 't help' )
1770+ parser3 .add_argument ('u' , nargs = '...' , help = 'u help' )
1771+
17641772 # return the main parser
17651773 return parser
17661774
@@ -1790,6 +1798,10 @@ def test_parse_args(self):
17901798 self .parser .parse_args ('--foo 0.125 1 c' .split ()),
17911799 NS (foo = True , bar = 0.125 , w = None , x = 'c' ),
17921800 )
1801+ self .assertEqual (
1802+ self .parser .parse_args ('-1.5 3 11 -- a --foo 7 -- b' .split ()),
1803+ NS (foo = False , bar = - 1.5 , t = 11 , u = ['a' , '--foo' , '7' , '--' , 'b' ]),
1804+ )
17931805
17941806 def test_parse_known_args (self ):
17951807 self .assertEqual (
@@ -1824,15 +1836,15 @@ def test_dest(self):
18241836
18251837 def test_help (self ):
18261838 self .assertEqual (self .parser .format_usage (),
1827- 'usage: PROG [-h] [--foo] bar {1,2} ...\n ' )
1839+ 'usage: PROG [-h] [--foo] bar {1,2,3 } ...\n ' )
18281840 self .assertEqual (self .parser .format_help (), textwrap .dedent ('''\
1829- usage: PROG [-h] [--foo] bar {1,2} ...
1841+ usage: PROG [-h] [--foo] bar {1,2,3 } ...
18301842
18311843 main description
18321844
18331845 positional arguments:
18341846 bar bar help
1835- {1,2} command help
1847+ {1,2,3} command help
18361848
18371849 optional arguments:
18381850 -h, --help show this help message and exit
@@ -1843,15 +1855,15 @@ def test_help_extra_prefix_chars(self):
18431855 # Make sure - is still used for help if it is a non-first prefix char
18441856 parser = self ._get_parser (prefix_chars = '+:-' )
18451857 self .assertEqual (parser .format_usage (),
1846- 'usage: PROG [-h] [++foo] bar {1,2} ...\n ' )
1858+ 'usage: PROG [-h] [++foo] bar {1,2,3 } ...\n ' )
18471859 self .assertEqual (parser .format_help (), textwrap .dedent ('''\
1848- usage: PROG [-h] [++foo] bar {1,2} ...
1860+ usage: PROG [-h] [++foo] bar {1,2,3 } ...
18491861
18501862 main description
18511863
18521864 positional arguments:
18531865 bar bar help
1854- {1,2} command help
1866+ {1,2,3} command help
18551867
18561868 optional arguments:
18571869 -h, --help show this help message and exit
@@ -1862,15 +1874,15 @@ def test_help_extra_prefix_chars(self):
18621874 def test_help_alternate_prefix_chars (self ):
18631875 parser = self ._get_parser (prefix_chars = '+:/' )
18641876 self .assertEqual (parser .format_usage (),
1865- 'usage: PROG [+h] [++foo] bar {1,2} ...\n ' )
1877+ 'usage: PROG [+h] [++foo] bar {1,2,3 } ...\n ' )
18661878 self .assertEqual (parser .format_help (), textwrap .dedent ('''\
1867- usage: PROG [+h] [++foo] bar {1,2} ...
1879+ usage: PROG [+h] [++foo] bar {1,2,3 } ...
18681880
18691881 main description
18701882
18711883 positional arguments:
18721884 bar bar help
1873- {1,2} command help
1885+ {1,2,3} command help
18741886
18751887 optional arguments:
18761888 +h, ++help show this help message and exit
@@ -1879,18 +1891,19 @@ def test_help_alternate_prefix_chars(self):
18791891
18801892 def test_parser_command_help (self ):
18811893 self .assertEqual (self .command_help_parser .format_usage (),
1882- 'usage: PROG [-h] [--foo] bar {1,2} ...\n ' )
1894+ 'usage: PROG [-h] [--foo] bar {1,2,3 } ...\n ' )
18831895 self .assertEqual (self .command_help_parser .format_help (),
18841896 textwrap .dedent ('''\
1885- usage: PROG [-h] [--foo] bar {1,2} ...
1897+ usage: PROG [-h] [--foo] bar {1,2,3 } ...
18861898
18871899 main description
18881900
18891901 positional arguments:
18901902 bar bar help
1891- {1,2} command help
1903+ {1,2,3} command help
18921904 1 1 help
18931905 2 2 help
1906+ 3 3 help
18941907
18951908 optional arguments:
18961909 -h, --help show this help message and exit
@@ -2001,6 +2014,7 @@ def test_alias_help(self):
20012014 1 (1alias1, 1alias2)
20022015 1 help
20032016 2 2 help
2017+ 3 3 help
20042018 """ ))
20052019
20062020# ============
0 commit comments