git @ Cat's Eye Technologies Eqthy / master src / eqthy / main.py
master

Tree @master (Download .tar.gz)

main.py @masterraw · history · blame

# Copyright (c) 2022-2024 Chris Pressey, Cat's Eye Technologies
# This file is distributed under a 2-clause BSD license.  See LICENSES directory:
# SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Eqthy

from argparse import ArgumentParser
import sys
from typing import List

from eqthy.loader import Loader
from eqthy.objects import Document
from eqthy.verifier import Verifier


def main(args: List[str]) -> None:
    argparser = ArgumentParser()

    argparser.add_argument(
        'input_files', nargs='+', metavar='FILENAME', type=str,
        help='Path to source file containing an Eqthy document'
    )

    argparser.add_argument(
        "--input-format",
        type=str,
        choices=("markdown", "bare-eqthy"),
        default="markdown",
        help="Select the format of the input files.  Default is Eqthy embedded in Markdown."
    )
    argparser.add_argument(
        "--dump-ast",
        action="store_true",
        help="Just show the AST and stop"
    )
    argparser.add_argument(
        "--traceback",
        action="store_true",
        help="When an error occurs, display a full Python traceback."
    )
    argparser.add_argument(
        "--verbose",
        action="store_true",
        help="Tell the user about every little thing"
    )
    argparser.add_argument(
        "--version", action="version", version="%(prog)s 0.4"
    )

    options = argparser.parse_args(args)

    documents: List[Document] = []
    input_format = "eqthy" if options.input_format == "bare-eqthy" else options.input_format
    try:
        for filename in options.input_files:
            loader = Loader(input_format)
            documents.extend(loader.load(filename))
        if options.dump_ast:
            for document in documents:
                print(document)
            sys.exit(0)
        verifier = Verifier(verbose=options.verbose)
        for document in documents:
            verifier.verify(document)
    except Exception as e:
        print('*** {}: {}'.format(e.__class__.__name__, e))
        if options.traceback:
            raise
        sys.exit(1)