Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save FilBot3/1d200c80ca4fabaa525f47803d0b8742 to your computer and use it in GitHub Desktop.

Select an option

Save FilBot3/1d200c80ca4fabaa525f47803d0b8742 to your computer and use it in GitHub Desktop.
Convert a Tuple of Strings from the Click library to Python Dict.
"""Use click to take in a tuple of strings formatted as key=value separated by
a space character. Then convert those into a Python Dictionary.
"""
import click
@click.command()
@click.argument('arg', nargs=-1)
def says(arg):
"""Phil Says stuff.
This is the Click command that performs the logic to convert a tuple of
strings like key=value into dict(key, value)
"""
phil_dict: dict = dict((a.strip(), str(b.strip()))
for a, b in (element.split('=')
for element in arg))
print(phil_dict)
kyle_dict: dict = {item.split('=')[0]: item.split('=')[1] for item in arg}
print(kyle_dict)
if __name__ == '__main__':
says()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment