Oct-15-2022, 02:53 PM
(This post was last modified: Oct-15-2022, 02:53 PM by Gribouillis.)
You can also use textwrap to wrap the comments
#!/usr/bin/env python3 __doc__ = '''program wrapcomment.py ''' __version__ = '0.0.1' import argparse import token from tokenize import tokenize import textwrap def spit_comments(filename, width): with open(filename, 'rb') as ifh: for tok in tokenize(ifh.readline): if tok.type is token.COMMENT: s = textwrap.fill(tok.string, width=width) print(s) if __name__ == '__main__': parser = argparse.ArgumentParser( description = """\ Applies textwrap to all the comments in the argument file and print these comments to stdout. """) parser.add_argument('filename', metavar='PYFILE', help='python program to scan.') parser.add_argument('-w', '--width', metavar='WIDTH', type=int, default=70, help='textwrap width') args = parser.parse_args() spit_comments(args.filename, width=args.width)