[issue42677] Support comments in argparse fromfile_prefix_chars files

Thomas Nabelek report at bugs.python.org
Sat Dec 19 15:34:04 EST 2020


Thomas Nabelek <nabelekt at gmail.com> added the comment:

For anyone else, here is my implementation. I decided not to do anything to support comments on the same lines as arguments.


import argparse
import re

# Override argparse's convert_arg_line_to_args method to allow for comments and empty lines

class ArgumentParserCustom(argparse.ArgumentParser):
    
    def convert_arg_line_to_args(self, arg_line):
    
        if (re.match(r'^[\s]*#', arg_line) or   # Look for any number of whitespace characters up to a `#` character
            re.match(r'^[\s]*$', arg_line)):  # Look for lines containing nothing or just whitespace
            return []
        else:
            return [arg_line]

----------

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


More information about the Python-bugs-list mailing list