1
0
src/it/parse_command.py
2021-05-30 17:16:34 -04:00

66 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
def parse_command(command):
casesensitive = True
escapes = {"\\"}
quotes = {"'", '"'}
spaces = {" ", "\t"}
command = command.strip()
in_quotes = 0
word = ""
parsed_command = []
for i in range(len(command)):
# if this char is a quote char
# , we're in quotes
if ((command[i] in quotes)
and in_quotes == 0
and (i == 0
or (i > 0 and not(command[i-1] in escapes)))):
in_quotes = command[i]
# if this char matches the char by which we're in quotes
# , we're not in quotes
elif (command[i] == in_quotes
and (i > 0 and not(command[i-1] in escapes))):
in_quotes = 0
# if this char is an arg delimiter
# and we're not in quotes
# and the last char isn't an escape
# , this word is an argument
elif (command[i] in spaces and in_quotes == 0
and (i > 0 and not(command[i-1] in escapes))):
parsed_command += [word]
word = ""
elif (not(command[i] in escapes) or (i == len(command) - 1)
or not(command[i+1] in spaces + quotes)):
word += command[i]
parsed_command += [word]
return [] if parsed_command == [''] else parsed_command
def main(*args):
while True:
try:
command = input()
except:
break
if command == ".":
break
command = parse_command(command)
for i in range(len(command)):
print("\t%d:\t%s" % (i, command[i]))
return 0 if len(args) != 2 else args[0]
if __name__ == "__main__":
sys.exit(main())