#!/usr/bin/env python
import json
import os
import optparse
import random

from crypto import *
from file_io import *
from settings import *

options = None
arguments = None

def main():
    if arguments[0] == 'generate':
        generate()
    elif arguments[0] == 'save':
        save()
    elif arguments[0] == 'edit':
        edit()

def save():
    try:
        bytes = multiple_of(read_file(DECRYPTED_FILE), BLOCK_LENGTH)
    except IOError:
        print("There's no plaintext file to save!")
        print("Tried %s" % os.path.abspath(DECRYPTED_FILE))
        return
    iv, encrypted = encrypt(bytes)
    write_file(ENCRYPTED_FILE, iv + encrypted)
    os.unlink(DECRYPTED_FILE)
    print("Removed plaintext and saved encrypted file.")

def edit():
    bytes = decrypt()
    write_file(DECRYPTED_FILE, bytes)
    print("Plaintext written to: %s" % os.path.abspath(DECRYPTED_FILE))

def generate():
    length = options.n if options.n else DEFAULT_PASSWORD_LENGTH
    for i in range(length):
        print(random.choice(KEY_CHARS), end='')
    print()

if __name__ == '__main__':
    p = optparse.OptionParser()
    p.add_option("-n", type='int', help="With 'generate', the length of the generated password")
    options, arguments = p.parse_args()
    if len(arguments) == 0:
        arguments.append(DEFAULT_ARGUMENT)
    main()

@@ -1110,16 +1110,67 @@
 nParser(
+usage=%22usage: %25prog %5Boptions%5D %5Bgenerate%7Csave%7Cedit%5D%22
 )%0A    p.

Document available arguments