#!python
# Mode: -*- python -*-
#
# Copyright (c) 2015-2017 by Rocky Bernstein <rb@dustyfeet.com>
#
from __future__ import print_function
import sys, os
import click

from xdis.version import VERSION
from xdis import PYTHON_VERSION
from xdis.main import disassemble_file

program, ext = os.path.splitext(os.path.basename(__file__))

PATTERNS = ('*.pyc', '*.pyo')

@click.command()
@click.option("--asm/--noasm", default=False,
              help='Produce output suitable for the xasm assembler')
@click.option("--version", '-V', default=False,
              help='Show version and exit')
@click.argument('files', nargs=-1, type=click.Path(readable=True), required=True)
def main(asm, version, files):
    """Disassembles a Python bytecode file.

    We handle bytecode for virtually every release of Python and some releases of PyPy.
    The version of Python in the bytecode doesn't have to be the same version as
    the Python interpreter used to run this program. For example, you can disassemble Python 3.6.1
    bytecode from Python 2.7.13 and vice versa.
    """
    Usage_short = """usage:
   %s [--asm] -i FILE...
   %s --version
Type -h for for full help.""" % (program, program)


    if not (2.5 <= PYTHON_VERSION <= 3.6):
        sys.stderr(print("This works on Python version 2.5..3.6; have %s" % PYTHON_VERSION))

    if not len(files):
        sys.stderr.write("No file(s) given..\n")
        print(Usage_short, file=sys.stderr)
        sys.exit(1)

    if version:
            print("%s %s" % (program, VERSION))
            sys.exit(0)

    for path in files:
        disassemble_file(path, sys.stdout, asm)
    return

if __name__ == '__main__':
    main(sys.argv[1:])
