-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MRG: add plugin support for new command-line subcommands (#2438)
Add command-line plug-in interface, per #1353. This PR builds on #2428 to provide support for adding new sourmash subcommands, under `sourmash scripts <cmd>`. The CLI plugin interface looks like this on the client side - ``` class cmd_somewhat: command = 'somewhat' description = "does a thing" def __init__(self, subparser): super().__init__(p) # add arguments etc here debug_literal('RUNNING cmd_somewhat.__init__') subparser.add_argument('foo', type=int) def main(self, args): super().main(args) # what we actually run. print('RUNNING cmd', self, args) print('XYZ somewhat', args.foo) print(f'2*{args.foo} is {2*args.foo}') ``` Adding this plugin enables: ``` % sourmash scripts -h == This is sourmash version 4.6.1. == == Please cite Brown and Irber (2016), doi:10.21105/joss.00027. == usage: scripts [-h] optional arguments: -h, --help show this help message and exit extension commands: sourmash scripts somewhat --help - do a thing ``` and ``` % sourmash scripts somewhat 5 == This is sourmash version 4.6.1. == == Please cite Brown and Irber (2016), doi:10.21105/joss.00027. == ... 2*5 is 10 ``` which is prety cool 🎉 ## Alternatives to `script` could be `sourmash ext` or `sourmash plugin`?
- Loading branch information
Showing
8 changed files
with
519 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Provide a mechanism to add CLI plugins to sourmash. | ||
See https://sourmash.readthedocs.io/en/latest/dev_plugins.html for docs, | ||
src/sourmash/plugins.py for core sourmash implementation code, and | ||
https://github.com/sourmash-bio/sourmash_plugin_template for a template repo | ||
for making new plugins. | ||
""" | ||
|
||
# CTB TODO: | ||
# * provide suggestions for documentation & metadata for authors: | ||
# * provide guidance on how to test your CLI plugin at the CLI | ||
# (minimal testing regime: sourmash scripts, look for description etc.) | ||
|
||
import argparse | ||
import sourmash | ||
|
||
# Here, we decorate this module with the various extension objects | ||
# e.g. 'sourmash scripts foo' will look up attribute 'scripts.foo' | ||
# and we will return the extension class object, which will then | ||
# be run by sourmash.__main__. This dictionary is loaded below | ||
# by sourmash.plugins.add_cli_scripts. | ||
_extension_dict = {} | ||
|
||
def __getattr__(name): | ||
if name in _extension_dict: | ||
return _extension_dict[name] | ||
raise AttributeError(name) | ||
|
||
def subparser(subparsers): | ||
subparser = subparsers.add_parser('scripts', | ||
usage=argparse.SUPPRESS, | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
aliases=['ext']) | ||
|
||
# get individual help strings: | ||
descrs = list(sourmash.plugins.get_cli_scripts_descriptions()) | ||
if descrs: | ||
description = "\n".join(descrs) | ||
else: | ||
description = "(No script plugins detected!)" | ||
|
||
s = subparser.add_subparsers(title="available plugin/extension commands", | ||
dest='subcmd', | ||
metavar='subcmd', | ||
help=argparse.SUPPRESS, | ||
description=description) | ||
|
||
_extension_dict.update(sourmash.plugins.add_cli_scripts(s)) |
This file contains 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
Oops, something went wrong.