summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Gallo <aamsgallo@gmail.com>2018-12-06 02:32:15 +0100
committerAlejandro Gallo <aamsgallo@gmail.com>2018-12-06 02:32:15 +0100
commit8a09de3730328608123945e199136995583fbbda (patch)
tree66d6e272294f89e54a02cdcba911ad19c29671f7
parentdd5a2663f39d571a29ef55e0942f1d747140df35 (diff)
Add @papis.cli.bypass decorator for scripts
-rw-r--r--papis/cli.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/papis/cli.py b/papis/cli.py
index 50dfd3a9..59731c3c 100644
--- a/papis/cli.py
+++ b/papis/cli.py
@@ -106,3 +106,38 @@ def git_option(**attrs):
attrs.setdefault('help', 'Add git interoperability')
return click.decorators.option('--git/--no-git', **attrs)(f)
return decorator
+
+
+def bypass(group, command, command_name):
+ """
+ This function is specially important for people developing scripts in papis.
+
+ Suppose you're writing a plugin that uses the ``add`` command as seen
+ in the command line in papis. However you don't want exactly the ``add``
+ command and you want to add some behavior before calling it, and you
+ don't want to write your own ``add`` function from scratch.
+
+ You can then use the following snippet
+
+ .. code::python
+
+ import click
+ import papis.cli
+ import papis.commands.add
+
+ @click.group()
+ def main():
+ \"\"\"Your main app\"\"\"
+ pass
+
+ @papis.cli.bypass(main, papis.commands.add.cli, "add")
+ def add(**kwargs):
+ # do some logic here...
+ # and call the original add command line function by
+ papis.commands.add.cli.bypassed(**kwargs)
+ """
+ group.add_command(command, command_name)
+ def decorator(new_callback):
+ setattr(command, "bypassed", command.callback)
+ command.callback = new_callback
+ return decorator