Python program that obfuscates an email address I decided to resume my (limited) Python skills after not coding for a long time. 

Here's a very simple self-contained program to convert the characters of an email address into their HTML entities (the purpose is to make the email invisible to spamrobots). 


Any comment for improvement? (e.g. args parsing, code style, etc.)
Apart from the -h option in argparse, what is the standard way to add some documentation for it, such as a manpage or some embedded help? 




#!/usr/bin/python

#
# mung - Convert a string into its HTML entities
#


import argparse

parser = argparse.ArgumentParser(description = 'Convert a string into its HTML entities.')
parser.add_argument('string_to_mung', help = 'String to convert')
parser.add_argument('-l', '--link', action = 'store_true', help = 'Embed the munged string into a mailto: link')
args = parser.parse_args()


def mung(plain):
    munged = ''
    for c in plain:
        munged = munged + '&amp;#' + str(ord(c)) + ';'
    return munged


string_munged = mung(args.string_to_mung)

if (args.link):
    print('&lt;a href="&amp;#109;&amp;#97;&amp;#105;&amp;#108;&amp;#116;&amp;#111;&amp;#58;')
    print(string_munged + '"&gt;')
    print(string_munged + '&lt;/a&gt;')
else:
    print(string_munged)
The code is straightforward and reads well, but:


string concatenation is not very efficient, especially in a loop. You'd be better off using str.join on an iterable;
encoding the mailto: part yourself impairs readability and maintenance, if only you had a function to do it for you. Oh wait...
The comment at the beginning would be better as a module docstrings, you would then be able to use it as the argparse description using __doc__;
You should avoid interleaving code and function definition, and protect top level code using an if __name__ == '__main__': guard.


Proposed improvements:

#!/usr/bin/python

"""Convert a string into its HTML entities"""


import argparse


def command_line_parser():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('string_to_mung', help='String to convert')
    parser.add_argument('-l', '--link', action='store_true',
                        help='Embed the munged string into a mailto: link')
    return parser


def mung(plain):
    return ''.join('&amp;#{};'.format(ord(c)) for c in plain)


if __name__ == '__main__':
    args = command_line_parser().parse_args()
    string_munged = mung(args.string_to_mung)
    if (args.link):
        string_munged = '&lt;a href="{0}{1}"&gt;{1}&lt;/a&gt;'.format(mung('mailto:'), string_munged)
    print(string_munged)