aboutsummaryrefslogtreecommitdiff
path: root/src/tests/extension_model_test.py
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2020-08-25 19:04:50 -0700
committerDaniel Mueller <deso@posteo.net>2020-08-25 19:04:50 -0700
commit223c8484f727451d15ad23ffb0133a1858b56b5c (patch)
tree383713050d9da0fa3f6b7ef9802bfbfd836ebb74 /src/tests/extension_model_test.py
parentc2a9d31ee70ddae22ea410a383a094b7842e50fd (diff)
downloadnitrocli-223c8484f727451d15ad23ffb0133a1858b56b5c.tar.gz
nitrocli-223c8484f727451d15ad23ffb0133a1858b56b5c.tar.bz2
Introduce support for user-provided extensions
This change introduces support for discovering and executing user-provided extensions to the program. Extensions are useful for allowing users to provide additional functionality on top of the nitrocli proper. Implementation wise we stick to an approach similar to git or cargo subcommands in nature: we search the directories listed in the PATH environment variable for a file that starts with "nitrocli-", followed by the extension name. This file is then executed. It is assumed that the extension recognizes (or at least not prohibits) the following arguments: --nitrocli (providing the path to the nitrocli binary), --model (with the model passed to the main program), and --verbosity (the verbosity level).
Diffstat (limited to 'src/tests/extension_model_test.py')
-rw-r--r--src/tests/extension_model_test.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/tests/extension_model_test.py b/src/tests/extension_model_test.py
new file mode 100644
index 0000000..651c8e7
--- /dev/null
+++ b/src/tests/extension_model_test.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+
+from argparse import (
+ ArgumentParser,
+)
+from enum import (
+ Enum,
+)
+from sys import (
+ argv,
+ exit,
+)
+
+
+class Action(Enum):
+ """An action to perform."""
+ NITROCLI = "nitrocli"
+ MODEL = "model"
+ VERBOSITY = "verbosity"
+
+ @classmethod
+ def all(cls):
+ """Return the list of all the enum members' values."""
+ return [x.value for x in cls.__members__.values()]
+
+
+def main(args):
+ """The extension's main function."""
+ parser = ArgumentParser()
+ parser.add_argument(choices=Action.all(), dest="what")
+ parser.add_argument("--nitrocli", action="store", default=None)
+ parser.add_argument("--model", action="store", default=None)
+ # We deliberately store the argument to this option as a string
+ # because we can differentiate between None and a valid value, in
+ # order to verify that it really is supplied.
+ parser.add_argument("--verbosity", action="store", default=None)
+
+ namespace = parser.parse_args(args[1:])
+ if namespace.what == Action.NITROCLI.value:
+ print(namespace.nitrocli)
+ elif namespace.what == Action.MODEL.value:
+ print(namespace.model)
+ elif namespace.what == Action.VERBOSITY.value:
+ print(namespace.verbosity)
+ else:
+ return 1
+
+ return 0
+
+
+if __name__ == "__main__":
+ exit(main(argv))