OVH email manager script using docopt This is what I coded to realize a command line program to make it easier to manage emails on ovh shared hostings. It's working ok but it seems ugly.

I'm a beginner with Python and docopt.  What would you suggest?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
''' OVH Email Manager (ovhEmailMan)
A small script that helps to add and remove one or more email addresses on the OVH shared domains

    Usage:
        ovh_mails.py list [--ugly]
        ovh_mails.py add (&lt;address&gt; [--pswd=&lt;password&gt;][--description=&lt;description&gt;] | --file &lt;filename&gt; [--notify])
        ovh_mails.py remove (&lt;address&gt; | --file &lt;filename&gt;)
        ovh_mails.py (-h | --help)

    Arguments:
        &lt;password&gt;                        Password to access the mailbox (if not provided it's random generated)
        &lt;filename&gt;                        Name of the files to process (csv). Check README to see how to format it

    Options: 
        -h, --help                        Show this help message
        -u, --ugly                        Print without nice tables
        -p, --pswd=&lt;password&gt;             Set the password to the one provided
        -n, --notify                      If set, notification mail is sent using smtp credentials in ovh.conf

    Commands:
        list                              list all the email addresses currently configured
        add                               add one or more (configured in &lt;filename&gt;) email addresses
        remove                            remove one ore more (configured in &lt;filename&gt;) email addresses
    '''



import ovh
from docopt import docopt
from ovhem import EmailManager
from ovhem import fileprocesser as fp


if __name__ == '__main__':
    args = docopt(__doc__)
    #Validate args ---- TODO
    eman = EmailManager()

    # 'List' command parsing
    if args['list']:
        if args['--ugly']:
            eman.niceoutput = False

        eman.list_emails()
    # 'Add' command parsing
    elif args['add']:
        if args['&lt;address&gt;']:

            emails = (
                      {
                       'address': args['&lt;address&gt;'],
                       'password': None,
                       'description': None,
                       },
                      )
            if args['--description']:
                emails[0]['description'] = args['&lt;description&gt;']
            if args['--pswd']:
                emails[0]['password'] = args['&lt;password&gt;']
        if args['--file']:
            emails = fp.process_file(args['&lt;filename&gt;'])    

        # Getting back the emails dict
        emails=eman.add_emails(emails)
        if args['--notify']:
            fp.send_notifications(emails)

    # 'remove' command parsing       
    elif args['remove']:
        if args['&lt;address&gt;']:

            emails = (
                      {
                       'address': args['&lt;address&gt;'],
                       },
                      )
        if args['--file']:
            emails = fp.process_file(args['&lt;filename&gt;'])
        eman.remove_emails(emails)
Overall, it looks quite fine, with some smaller and bigger issues.

Parsing the add command

It's not so great to use the '&lt;address&gt;' string twice here:


    if args['&lt;address&gt;']:

        emails = (
                  {
                   'address': args['&lt;address&gt;'],
        # ...



You might mistype one of them. Or you might change something later and forget to update it everywhere. It would be better to put the value of args['&lt;address&gt;'] into a variable, and use that variable in evaluations and assignments.

This part is also awkward:


        emails = ( ... )
        if args['--description']:
            emails[0]['description'] = args['&lt;description&gt;']
        if args['--pswd']:
            emails[0]['password'] = args['&lt;password&gt;']



It would be much easier to use description and password variables to store defaults, then update these variables if --description or --pswd were given, and finally create the emails tuple.

Like this:

    address = args['&lt;address&gt;']
    if address:
        password = None
        description = None
        if args['--description']:
            description = args['&lt;description&gt;']
        if args['--pswd']:
            password = args['&lt;password&gt;']
        emails = (
            {
                'address': address,
                'password': password,
                'description': description,
            },
        )


Parsing the remove command

Here, if the second if statement is true,
it will overwrite the effect of the first:


    if args['&lt;address&gt;']:
        emails = (
            {
                'address': args['&lt;address&gt;'],
            },
        )
    if args['--file']:
        emails = fp.process_file(args['&lt;filename&gt;'])



That suggest that you should use an elif, and switch the order of the statements:

    if args['--file']:
        emails = fp.process_file(args['&lt;filename&gt;'])
    elif args['&lt;address&gt;']:
        emails = (
            {
                'address': args['&lt;address&gt;'],
            },
        )


emails might be undefined

Both the parsing of add and remove have a problem:
As their last step they do something with emails,
for example eman.add_emails(emails) and eman.remove_emails(emails),
but at that point the emails variable might be undefined.

This can be fixed by adding validation for these commands.
It looks that either of these must be true, checked in this order:


--file was specified and &lt;filename&gt; parameter was given
&lt;address&gt; parameter was given


For example:

if args['--file']:
    filename = args['&lt;filename&gt;']
    if filename:
        emails = fp.process_file(filename)
    else:
        raise Exception("Filename parameter missing")
else:
    address = args['&lt;address&gt;']
    if address:
        emails = ( ... )
    else:
        raise Exception("Address parameter missing")

# main action