Skip to content

Instantly share code, notes, and snippets.

@mcaselli
Created January 11, 2021 16:31
Show Gist options
  • Save mcaselli/4b09a1e7476db4c92e90e2eb17813fa7 to your computer and use it in GitHub Desktop.
Save mcaselli/4b09a1e7476db4c92e90e2eb17813fa7 to your computer and use it in GitHub Desktop.
Optionally override an variable boolean default with plac
import distutils.util
import plac
VIEW_DEFAULT = False
@plac.opt(
"view",
help="show something",
type=str,
choices=["True", "False", "true", "false"]
)
def main(view=VIEW_DEFAULT):
"""a simple application
if the user provides the `view` argument, its value will override the default,
which might itself be configured in some kind of config file
"""
print(f"VIEW_DEFAULT: {VIEW_DEFAULT}")
if isinstance(view, str):
view = bool(distutils.util.strtobool(view))
print(f"view: {view}")
if __name__ == "__main__":
plac.call(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment