Created
December 15, 2022 00:55
-
-
Save tsibley/9b36f990f2e59b38a068d10c555ac232 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import chain, starmap | |
from shlex import quote as shquote | |
from typing import Dict, Union | |
flatten = chain.from_iterable | |
def augur_options(opts: Dict[str, Union[str, list, None]]) -> str: | |
""" | |
Converts a dictionary of command-line config options to a string suitable | |
for use in an ``augur`` invocation. | |
Values are appropriately quoted/escaped: | |
>>> augur_options({"--min-length": 9000, "--include-where": "state = WA"}) | |
"--min-length 9000 --include-where 'state = WA'" | |
Flags are keys where the value is ``True``: | |
>>> augur_options({"--exclude-all": True}) | |
'--exclude-all' | |
Keys where the value is ``False`` or ``None`` are ignored: | |
>>> augur_options({"--exclude-all": False}) | |
'' | |
>>> augur_options({"--exclude-all": None}) | |
'' | |
Multiple values for an option use a list: | |
>>> augur_options({"--metadata-id-columns": ["strain", "seq name", "seq id", "$id"]}) | |
"--metadata-id-columns strain 'seq name' 'seq id' '$id'" | |
""" | |
def optval(opt, val): | |
if isinstance(val, list): | |
return shquote(opt) + " " + " ".join(map(shquote, map(str, val))) | |
elif val is True: | |
return shquote(opt) | |
elif val in {False, None}: | |
return None | |
else: | |
return shquote(opt) + " " + shquote(str(val)) | |
return " ".join(filter(None, starmap(optval, opts.items()))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment