import sys
import os.path
from srtmod import srtmod

def main(): 
    if len(sys.argv) < 5:
        print 'Usage: ' + sys.argv[0] +' <file name> <time amount> <time part>' \
            + ' <operation>'
        print 'Time part: (S)econds or (M)inutes'
        print 'Operation: (/A)dd or (/D)iscount'
    else:
        try:
            file_name = sys.argv[1]
            time_amount = int(sys.argv[2])
            time_part = sys.argv[3]
            operation = sys.argv[4]
            if time_part not in ('S', 'M'):
                raise ValueError('Time part should be S or M')
            if operation not in ('/A', '/D'):
                raise ValueError('Operation should be /A or /D')
            srt = srtmod.SrtMod(file_name, time_amount, time_part, operation)
            if srt.process():
                print '\nSubtitle file was created successfully'
                print 'File saved at :' + os.path.splitext(file_name)[0] + '(modified).srt'
            else:
                print '\nSubtitle can not be processed'
        except OverflowError:
            print 'Exception: Invalid time amount for this operation'
        except ValueError as ex:
            print 'Exception: ' + str(ex)

if __name__ == '__main__':
    main()
import os.path
from srtmod import srtmod
import argparse


def main():
    parser = argparse.ArgumentParser(prog='SrtModPy',
            description='Just adjusts the timing of subtitle files')
    parser.add_argument('file_name', help='the subtitle to be modified')
    parser.add_argument('time_amount', metavar='time_amount', type=int,
           help='the amount of time to be added or subtracted to subtitle')
    parser.add_argument('time_part',
           help='it\'s the part of time to be modified', choices=['S', 'M'])
    parser.add_argument('operation',
           help='add or dicount time to subtitle', choices=['/A', '/D'])
    r = parser.parse_args()
    time_amount = r.time_amount
    file_name = r.file_name
    time_part = r.time_part
    operation = r.operation
    try:
        srt = srtmod.SrtMod(file_name, time_amount, time_part, operation)
        if srt.process():
            print '\nsubtitle file was created successfully'
            print 'file saved at :' + os.path.splitext(file_name)[0] \
                  + '(modified).srt'
        else:
            print '\nsubtitle can not be processed'
    except OverflowError:
        print 'Exception: Invalid time amount for this operation'


if __name__ == '__main__':
    main()
Modify command-line parser