Add command line arguments

master
rubenwardy 2014-10-12 19:47:56 +01:00
parent bd9f5049e9
commit 6e66e8ab00
4 changed files with 44 additions and 9 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*~*
.directory
*.kate-swap
output/*
*.pyc

View File

@ -8,3 +8,11 @@ Created by rubenwardy.
License: GPL 3.0 or later License: GPL 3.0 or later
The copyright of mesecode scripts, and any resulting Lua files, remains with their author. The copyright of mesecode scripts, and any resulting Lua files, remains with their author.
Usage
-----
```
$ python mesecode.py path/to/file.mese output/directory
```

View File

@ -1,4 +1,4 @@
import re, sys import re, sys, os
# How many spaces in a tab. # How many spaces in a tab.
# -1 disables support for spaces as tabs (recommended) # -1 disables support for spaces as tabs (recommended)
@ -66,6 +66,10 @@ def throwParseError(msg):
print("\033[91mParse Error: " + msg + "\033[0m") print("\033[91mParse Error: " + msg + "\033[0m")
sys.exit(-1) sys.exit(-1)
def checkMkDir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def parse_list(the_list, line): def parse_list(the_list, line):
if line == "": if line == "":
return return
@ -164,12 +168,26 @@ class Project:
if curprop is None: if curprop is None:
throwParseError("Too many levels of indentation at " + str(lineno)) throwParseError("Too many levels of indentation at " + str(lineno))
curprop.append(line) curprop.append(line)
def get(self):
retval = "-- Mod namespace: " + self.modname + "\n"
retval += "-- Generated using MeseCode\n\n"
for item in self.objects:
retval += item.build(self) + "\n"
print(retval)
return retval
def write(self, directory): def write(self, directory):
print("-- Mod namespace: " + self.modname) if directory.endswith("/"):
print("-- Generated using the Minetest Readable Modding Format\n") directory = directory[:-1]
for item in self.objects: checkMkDir(directory)
print(item.build(self)) out = open(directory + "/init.lua", "w")
out.write(self.get())
out.close()
if __name__ == "__main__":
Project("test.mese").write("test") if len(sys.argv) == 2:
Project(sys.argv[1]).write("output")
elif len(sys.argv) == 3:
Project(sys.argv[1]).write(sys.argv[2])
else:
print("Usage: mesecode.py path/to/file.mese output/directory")

4
test.py Normal file
View File

@ -0,0 +1,4 @@
import mesecode
mesecode.Project("test.mese").write("output")