def __new__(cls, executable): try: parse(executable) except SyntaxError: raise ValueError("«%s» is not a valid shell command" % executable) # Value is OK, now we maintain original. return str.__new__(cls, executable)
single_line_cmd = "ls -la /tmp 2>&1 && echo foo'bar'\ " print("DEMO: shnake.lex()") print("The shnake lexer handles single line commands:") print() print("Raw command: %r" % single_line_cmd) result = shnake.lex(single_line_cmd) print("Lexed command: %r" % result) print() print() # parser behavior multi_line_cmd = "cmd1-part1\\\ncmd1-part2\ncmd2" print() print("DEMO: shnake.parse()") print("The shnake parser is a wrapper for the lexer.") print("Its handles multi-line strings, so it can parse file buffers") print() print("Raw command: %r" % multi_line_cmd) result = shnake.parse(multi_line_cmd) print("Parsed command: %r" % result) print() print() # command interpreter demo print("DEMO: shnake.Shell()") print("This shnake shell is a command line interpreter.") print("Type: `help` in the shell interface") interpreter = shnake.Shell() interpreter.prompt = "shnake > " interpreter.cmdloop()