aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/tests/extension_model_test.py
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-06 16:59:11 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-06 16:59:11 -0800
commite78d0432b8db215cf76cb410de354287fc2da8ba (patch)
tree1c8e160868d7749b44f66e317848170947b28249 /nitrocli/src/tests/extension_model_test.py
parent6bb629b4d1035c3fd851244060f99da78a7bd929 (diff)
downloadnitrocli-e78d0432b8db215cf76cb410de354287fc2da8ba.tar.gz
nitrocli-e78d0432b8db215cf76cb410de354287fc2da8ba.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 find executables in the PATH environment variable whose file name starts with "nitrocli-" and allow for them to be invoked as a nitrocli command.
Diffstat (limited to 'nitrocli/src/tests/extension_model_test.py')
-rw-r--r--nitrocli/src/tests/extension_model_test.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/nitrocli/src/tests/extension_model_test.py b/nitrocli/src/tests/extension_model_test.py
new file mode 100644
index 0000000..651c8e7
--- /dev/null
+++ b/nitrocli/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))