Skip to content

Instantly share code, notes, and snippets.

@FFY00
Last active September 16, 2021 21:12
Show Gist options
  • Save FFY00/0029d3001b384ff974360cd6c69b39f8 to your computer and use it in GitHub Desktop.
Save FFY00/0029d3001b384ff974360cd6c69b39f8 to your computer and use it in GitHub Desktop.
def expand(value):
args = []
escape = False
end_section = None
section = ''
for c in value:
# previous character was \, just append this one
if escape:
section += c
escape = False
continue
# character is \, signal to escape the next character
if c == '\\':
escape = True
continue
# we are inside quotes
if end_section:
if c == end_section:
# found closing quote, signal that we are no longer inside quotes
end_section = None
continue
# we are outside quotes
else:
# found space, append the section to the arguments
if c == ' ' and section: # only append if the current action has something
args.append(section)
section = ''
continue
# found starting quote, signal that we are now inside quotes
elif c in ('"', "'"):
end_section = c
continue
# just add the current character to the section
section += c
# the text has ended, if we have any data in the section append it to the arguments
if section:
args.append(section)
return args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment