mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	[3.13] gh-53780: Ignore the first "--" (double dash) between an option and command in argparse (GH-124275) (GH-125073)
(cherry picked from commit c578271366)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
			
			
This commit is contained in:
		
							parent
							
								
									a380dc6836
								
							
						
					
					
						commit
						db3ccd8b62
					
				
					 3 changed files with 23 additions and 4 deletions
				
			
		|  | @ -2097,11 +2097,15 @@ def consume_positionals(start_index): | ||||||
|             # and add the Positional and its args to the list |             # and add the Positional and its args to the list | ||||||
|             for action, arg_count in zip(positionals, arg_counts): |             for action, arg_count in zip(positionals, arg_counts): | ||||||
|                 args = arg_strings[start_index: start_index + arg_count] |                 args = arg_strings[start_index: start_index + arg_count] | ||||||
|                 # Strip out the first '--' if it is not in PARSER or REMAINDER arg. |                 # Strip out the first '--' if it is not in REMAINDER arg. | ||||||
|                 if (action.nargs not in [PARSER, REMAINDER] |                 if action.nargs == PARSER: | ||||||
|                     and arg_strings_pattern.find('-', start_index, |                     if arg_strings_pattern[start_index] == '-': | ||||||
|  |                         assert args[0] == '--' | ||||||
|  |                         args.remove('--') | ||||||
|  |                 elif action.nargs != REMAINDER: | ||||||
|  |                     if (arg_strings_pattern.find('-', start_index, | ||||||
|                                                  start_index + arg_count) >= 0): |                                                  start_index + arg_count) >= 0): | ||||||
|                     args.remove('--') |                         args.remove('--') | ||||||
|                 start_index += arg_count |                 start_index += arg_count | ||||||
|                 if args and action.deprecated and action.dest not in warned: |                 if args and action.deprecated and action.dest not in warned: | ||||||
|                     self._warning(_("argument '%(argument_name)s' is deprecated") % |                     self._warning(_("argument '%(argument_name)s' is deprecated") % | ||||||
|  |  | ||||||
|  | @ -6106,6 +6106,20 @@ def test_subparser(self): | ||||||
|             "invalid choice: '--'", |             "invalid choice: '--'", | ||||||
|             parser.parse_args, ['--', 'x', '--', 'run', 'a', 'b']) |             parser.parse_args, ['--', 'x', '--', 'run', 'a', 'b']) | ||||||
| 
 | 
 | ||||||
|  |     def test_subparser_after_multiple_argument_option(self): | ||||||
|  |         parser = argparse.ArgumentParser(exit_on_error=False) | ||||||
|  |         parser.add_argument('--foo', nargs='*') | ||||||
|  |         subparsers = parser.add_subparsers() | ||||||
|  |         parser1 = subparsers.add_parser('run') | ||||||
|  |         parser1.add_argument('-f') | ||||||
|  |         parser1.add_argument('bar', nargs='*') | ||||||
|  | 
 | ||||||
|  |         args = parser.parse_args(['--foo', 'x', 'y', '--', 'run', 'a', 'b', '-f', 'c']) | ||||||
|  |         self.assertEqual(NS(foo=['x', 'y'], f='c', bar=['a', 'b']), args) | ||||||
|  |         self.assertRaisesRegex(argparse.ArgumentError, | ||||||
|  |             "invalid choice: '--'", | ||||||
|  |             parser.parse_args, ['--foo', 'x', '--', '--', 'run', 'a', 'b']) | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| # =========================== | # =========================== | ||||||
| # parse_intermixed_args tests | # parse_intermixed_args tests | ||||||
|  |  | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | :mod:`argparse` now ignores the first ``"--"`` (double dash) between an option and command. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Miss Islington (bot)
						Miss Islington (bot)