#!/usr/bin/env python
# -*- mode: python -*-
# PYTHON_ARGCOMPLETE_OK

import sys
import argparse

import argcomplete

from textract import process
from textract.colors import red
from textract.exceptions import CommandLineError


# command line options here
parser = argparse.ArgumentParser(
    description='Command line tool for extracting text from any document'
)
parser.add_argument(
    'filename', help='Filename to extract text.',
)
parser.add_argument(
    '-o', '--output', type=argparse.FileType('w'), default='-',
    help='Put raw text in here.',
)

# enable autocompletion with argcomplete
argcomplete.autocomplete(parser)

# extract text
args = parser.parse_args()
try:
    output = process(args.filename)
except CommandLineError, e:
    sys.stderr.write(red(e) + '\n')
else:
    args.output.write(output)
    args.output.write('\n')
