mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 23:58:20 +00:00
Reorganize main to use getopt. Implement -o for output file and -O help.
Also --version and -h/--help.
This commit is contained in:
parent
5b105e5772
commit
9e0ed0521b
2 changed files with 88 additions and 34 deletions
|
@ -34,3 +34,5 @@ class Quaternion(tuple):
|
||||||
Bugs = set([6495])
|
Bugs = set([6495])
|
||||||
|
|
||||||
LSO = False
|
LSO = False
|
||||||
|
|
||||||
|
DataPath = ''
|
||||||
|
|
120
main.py
120
main.py
|
@ -22,14 +22,17 @@
|
||||||
from lslopt.lslparse import parser,EParse
|
from lslopt.lslparse import parser,EParse
|
||||||
from lslopt.lsloutput import outscript
|
from lslopt.lsloutput import outscript
|
||||||
from lslopt.lsloptimizer import optimizer
|
from lslopt.lsloptimizer import optimizer
|
||||||
import sys
|
import sys, os, getopt
|
||||||
import os
|
|
||||||
import lslopt.lslcommon
|
import lslopt.lslcommon
|
||||||
|
|
||||||
def main():
|
|
||||||
if len(sys.argv) < 2:
|
VERSION = '0.1.1'
|
||||||
|
|
||||||
|
|
||||||
|
def Usage(about = None):
|
||||||
|
if about is None:
|
||||||
sys.stderr.write(
|
sys.stderr.write(
|
||||||
r'''LSL optimizer v0.1
|
r'''LSL optimizer v{version}
|
||||||
|
|
||||||
(C) Copyright 2015 Sei Lisa. All rights reserved.
|
(C) Copyright 2015 Sei Lisa. All rights reserved.
|
||||||
|
|
||||||
|
@ -39,12 +42,23 @@ r'''LSL optimizer v0.1
|
||||||
This program is licensed under the GNU General Public License
|
This program is licensed under the GNU General Public License
|
||||||
version 3.
|
version 3.
|
||||||
|
|
||||||
Usage: %s [-O [+|-]option[,[+|-]option[,...]]] filename
|
Usage: {progname}
|
||||||
|
[{{-O|--optimizer-options}} [+|-]option[,[+|-]option[,...]]]
|
||||||
|
[-h|--help]
|
||||||
|
[--version]
|
||||||
|
[{{-o|--output=}} filename]
|
||||||
|
filename
|
||||||
|
|
||||||
That's an upper case o, not the number zero.
|
|
||||||
If filename is a dash (-) then standard input is used.
|
If filename is a dash (-) then standard input is used.
|
||||||
|
Use: {progname} -O help for help on the command line options.
|
||||||
|
|
||||||
Options (+ means active by default, - means inactive by default):
|
'''.format(progname=sys.argv[0], version=VERSION))
|
||||||
|
return
|
||||||
|
|
||||||
|
if about == 'optimizer-options':
|
||||||
|
sys.stderr.write(
|
||||||
|
r'''
|
||||||
|
Optimizer options (+ means active by default, - means inactive by default):
|
||||||
|
|
||||||
Syntax extensions options:
|
Syntax extensions options:
|
||||||
|
|
||||||
|
@ -110,7 +124,7 @@ Options (+ means active by default, - means inactive by default):
|
||||||
Miscellaneous options
|
Miscellaneous options
|
||||||
|
|
||||||
foldtabs - Tabs can't be copy-pasted, so expressions that produce
|
foldtabs - Tabs can't be copy-pasted, so expressions that produce
|
||||||
tabs, e.g. llUnescapeURL("%%09"), aren't optimized by
|
tabs, e.g. llUnescapeURL("%09"), aren't optimized by
|
||||||
default. This option overrides that check, enabling
|
default. This option overrides that check, enabling
|
||||||
expansion of functions that produce strings with tabs.
|
expansion of functions that produce strings with tabs.
|
||||||
The resulting source isn't guaranteed to be
|
The resulting source isn't guaranteed to be
|
||||||
|
@ -123,33 +137,63 @@ Options (+ means active by default, - means inactive by default):
|
||||||
is useless with 'optimize' and 'optsigns', and is of
|
is useless with 'optimize' and 'optsigns', and is of
|
||||||
basically no use in general, other than to see where
|
basically no use in general, other than to see where
|
||||||
automatic casts happen.
|
automatic casts happen.
|
||||||
''' % sys.argv[0])
|
''')
|
||||||
return 1
|
return
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# If it's good to append the basename to it, it's good to append the
|
||||||
|
# auxiliary files' names to it, which should be located where this file is.
|
||||||
|
lslopt.lslcommon.DataPath = __file__[:-len(os.path.basename(__file__))]
|
||||||
|
|
||||||
|
# Default options
|
||||||
options = set(('extendedglobalexpr','extendedtypecast','extendedassignment',
|
options = set(('extendedglobalexpr','extendedtypecast','extendedassignment',
|
||||||
'allowkeyconcat','allowmultistrings','skippreproc','optimize',
|
'allowkeyconcat','allowmultistrings','skippreproc','optimize',
|
||||||
'optsigns','optfloats','constfold','dcr'
|
'optsigns','optfloats','constfold','dcr'
|
||||||
))
|
))
|
||||||
|
|
||||||
# If it's good to append the basename to it, it's good to append the
|
try:
|
||||||
# auxiliary files' names to it.
|
opts, args = getopt.gnu_getopt(sys.argv[1:], 'hO:o:',
|
||||||
lslopt.lslcommon.DataPath = __file__[:-len(os.path.basename(__file__))]
|
("help", "version", "optimizer-options=", "output="))
|
||||||
|
except getopt.GetoptError:
|
||||||
|
Usage()
|
||||||
|
return 1
|
||||||
|
|
||||||
if sys.argv[1] == '-O':
|
outfile = '-'
|
||||||
if len(sys.argv) < 4:
|
|
||||||
sys.stderr.write('Command line: Not enough parameters\n')
|
for opt, arg in opts:
|
||||||
return 1
|
|
||||||
optchanges = sys.argv[2].split(',')
|
if opt in ('-O', '--optimizer-options'):
|
||||||
for chg in optchanges:
|
if arg == 'help':
|
||||||
if chg[0:1] not in ('+', '-'):
|
Usage('optimizer-options')
|
||||||
chg = '+' + chg
|
return 0
|
||||||
if chg[0] == '-':
|
|
||||||
options.discard(chg[1:])
|
optchanges = arg.split(',')
|
||||||
else:
|
for chg in optchanges:
|
||||||
options.add(chg[1:])
|
if chg[0:1] not in ('+', '-'):
|
||||||
fname = sys.argv[3]
|
chg = '+' + chg
|
||||||
else:
|
if chg[0] == '-':
|
||||||
fname = sys.argv[1]
|
options.discard(chg[1:])
|
||||||
|
else:
|
||||||
|
options.add(chg[1:])
|
||||||
|
|
||||||
|
elif opt in ('-h', '--help'):
|
||||||
|
Usage()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
elif opt in ('-v', '--version'):
|
||||||
|
sys.stdout.write('LSL PyOptimizer v%s\n' % VERSION)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
elif opt in ('-o', '--output'):
|
||||||
|
outfile = arg
|
||||||
|
del opts
|
||||||
|
|
||||||
|
fname = args[0] if args else None
|
||||||
|
if fname is None:
|
||||||
|
Usage()
|
||||||
|
return 1
|
||||||
|
|
||||||
|
del args
|
||||||
|
|
||||||
p = parser()
|
p = parser()
|
||||||
try:
|
try:
|
||||||
|
@ -159,7 +203,7 @@ Options (+ means active by default, - means inactive by default):
|
||||||
else:
|
else:
|
||||||
ts = p.parsefile(fname, options)
|
ts = p.parsefile(fname, options)
|
||||||
except EParse as e:
|
except EParse as e:
|
||||||
print e.message
|
sys.stderr.write(e.message + '\n')
|
||||||
return 1
|
return 1
|
||||||
del p
|
del p
|
||||||
|
|
||||||
|
@ -171,9 +215,17 @@ Options (+ means active by default, - means inactive by default):
|
||||||
script = outs.output(ts, options)
|
script = outs.output(ts, options)
|
||||||
del outs
|
del outs
|
||||||
del ts
|
del ts
|
||||||
sys.stdout.write(script)
|
if outfile == '-':
|
||||||
|
sys.stdout.write(script)
|
||||||
|
else:
|
||||||
|
outf = open(outfile, 'w')
|
||||||
|
try:
|
||||||
|
outf.write(script)
|
||||||
|
finally:
|
||||||
|
outf.close()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
ret = main()
|
if __name__ == '__main__':
|
||||||
if ret:
|
ret = main()
|
||||||
sys.exit(ret)
|
if ret:
|
||||||
|
sys.exit(ret)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue