[issue41534] argparse : allow_abbrev behavior between 3.7 and 3.8

paul j3 report at bugs.python.org
Mon Aug 24 23:18:27 EDT 2020


paul j3 <ajipanca at gmail.com> added the comment:

In your first example:

In [29]: parser = argparse39.ArgumentParser(allow_abbrev=True)                  
In [30]: parser.add_argument('-o');                                             

In [32]: parser.parse_known_args(['-o','test'])                                 
Out[32]: (Namespace(o='test'), [])

In [33]: parser.parse_known_args(['-object','test'])                            
Out[33]: (Namespace(o='bject'), ['test'])

Here the '-object' is interpreted as '-o' flag, with value the rest of the string.  That's normal behavior for a short option.

In [34]: parser.parse_known_args(['-o','test1','-object','test2'])              
Out[34]: (Namespace(o='bject'), ['test2'])

Same thing only '-object' has overwritten the 'test1' value.

In your second example:

In [39]: parser = argparse39.ArgumentParser(allow_abbrev=False)                 
In [40]: parser.add_argument('-verbose');                                       

In [42]: parser.parse_known_args(['-v', '-verbose=2'])                          
usage: ipython3 [-h] [-verbose VERBOSE]
ipython3: error: argument -verbose: expected one argument

Expected uses:

In [46]: parser.parse_known_args(['-verbose','two'])                            
Out[46]: (Namespace(verbose='two'), [])
In [47]: parser.parse_known_args(['-verbose=2'])                                
Out[47]: (Namespace(verbose='2'), [])

The '-ver' is not accepted as abbreviation:

In [48]: parser.parse_known_args(['-ver=2'])                                    
Out[48]: (Namespace(verbose=None), ['-ver=2'])

'allow_abbrev' doesn't have effect because of '-verbose'

If instead I define '--verbose', I can turn the abbrev on/off:

In [49]: parser = argparse39.ArgumentParser(allow_abbrev=False)                 
In [50]: parser.add_argument('--verbose');                                      
In [51]: parser.parse_known_args(['-ver=2'])                                    
Out[51]: (Namespace(verbose=None), ['-ver=2'])
In [52]: parser.parse_known_args(['--ver=2'])                                   
Out[52]: (Namespace(verbose=None), ['--ver=2'])
In [53]: parser.allow_abbrev=True                                               
In [54]: parser.parse_known_args(['--ver=2'])                                   
Out[54]: (Namespace(verbose='2'), [])

There are a lot of variations to examine here.  The original, the 3.7, and 3.8 versions.  abbrev False or True.  Proper long option (--), improper etc.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41534>
_______________________________________


More information about the Python-bugs-list mailing list