コード例 #1
0
ファイル: __main__.py プロジェクト: ggm/vm-for-transfer
def main():
    vm = VM()

    try:
        opts, args = getopt.getopt(
            sys.argv[1:], "c:i:o:gh", ["codefile=", "inputfile=", "outputfile=", "debug", "help"]
        )
    except getopt.GetoptError as error:
        print(error)
        exit()
    # Go through the options, initializing and configuring the vm.
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            exit()
        elif opt in ("-c", "--codefile"):
            try:
                codeFile = open(arg, mode="rt", encoding="utf-8")
                header = codeFile.readline()
                if not vm.setLoader(header, codeFile):
                    print("The header of the file {} is not recognized:".format(arg))
                    print(header)
                    sys.exit(2)

                transferHeader = codeFile.readline()
                vm.setTransferStage(transferHeader)
            except IOError as ioe:
                print(ioe)
                sys.exit(2)
        elif opt in ("-i", "--inputfile"):
            try:
                vm.input = open(arg, mode="rt", encoding="utf-8")
            except IOError as ioe:
                print(ioe)
                sys.exit(2)
        elif opt in ("-o", "--outputfile"):
            try:
                vm.output = open(arg, mode="wb")
            except IOError as ioe:
                print(ioe)
                sys.exit(2)
        elif opt in ("-g", "--debug"):
            vm.setDebugMode()

    # If a code file isn't supplied, there won't be any loader so we end.
    if vm.loader is None:
        exit()

    vm.run()

    sys.exit(0)