main.py takes script file or script on stdin

This commit is contained in:
adam j hartz 2015-03-18 19:00:01 -04:00
parent 0ff1b8df5f
commit b1423b78ee

View file

@ -13,6 +13,11 @@ parser.add_argument('-c',
dest='command', dest='command',
required=False, required=False,
default=None) default=None)
parser.add_argument('file',
metavar='script-file',
help='If present, execute the script contained in script-file and exit',
nargs='?',
default=None)
def main(argv=None): def main(argv=None):
"""Main entry point for xonsh cli.""" """Main entry point for xonsh cli."""
@ -21,10 +26,23 @@ def main(argv=None):
shell = Shell() shell = Shell()
if args.command is None: if args.command is not None:
shell.cmdloop() # run a single command and exit
else:
shell.default(args.command) shell.default(args.command)
elif args.file is not None:
if os.path.isfile(args.file):
with open(args.file) as f:
for line in f:
shell.default(line)
else:
print('xonsh: {0}: No such file or directory.'.format(args.file))
elif not sys.stdin.isatty():
# run a script given on stdin
for line in sys.stdin:
shell.default(line)
else:
# otherwise, enter the shell
shell.cmdloop()
if __name__ == '__main__': if __name__ == '__main__':
main() main()