That was my decision. It analyzes the most normal input in the same way as if it were passed directly to the command line.
import re def simpleParse(input_): def reduce_(quotes): return '' if quotes.group(0) == '"' else '"' rex = r'("[^"]*"(?:\s|$)|[^\s]+)' return [re.sub(r'"{1,2}',reduce_,z.strip()) for z in re.findall(rex,input_)]
Use case: collecting a bunch of single-shot scripts into a launch utility without having to re-enter the command.
Edit: Got an OCD about the stupid way the command line handles crappy quotes and wrote below:
import re tokens = list() reading = False qc = 0 lq = 0 begin = 0 for z in range(len(trial)): char = trial[z] if re.match(r'[^\s]', char): if not reading: reading = True begin = z if re.match(r'"', char): begin = z qc = 1 else: begin = z - 1 qc = 0 lc = begin else: if re.match(r'"', char): qc = qc + 1 lq = z elif reading and qc % 2 == 0: reading = False if lq == z - 1: tokens.append(trial[begin + 1: z - 1]) else: tokens.append(trial[begin + 1: z]) if reading: tokens.append(trial[begin + 1: len(trial) ]) tokens = [re.sub(r'"{1,2}',lambda y:'' if y.group(0) == '"' else '"', z) for z in tokens]
source share