Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider refactoring command-line interface code around plugin code #2521

Open
ctb opened this issue Mar 12, 2023 · 3 comments
Open

consider refactoring command-line interface code around plugin code #2521

ctb opened this issue Mar 12, 2023 · 3 comments
Labels
plugin_todo Write a plugin for this!

Comments

@ctb
Copy link
Contributor

ctb commented Mar 12, 2023

The CLI plugin system introduced in #2438 is (to my mind ;) pretty nice - maybe we could refactor the sourmash CLI code to look like this more generally?

class Command_XYZ(CommandLinePlugin):
    command = 'xyz'
    description = "does a thing"

    def __init__(self, subparser):
        super().__init__(subparser)
        # add argparse arguments here.
        debug_literal('RUNNING cmd_xyz.__init__')

    def main(self, args):
        # code that we actually run.
        super().main(args)
        print('RUNNING cmd', self, args)
@luizirber
Copy link
Member

Can we expand the args in def main(self, args): to the actual args (sorta like https://github.com/sourmash-bio/sourmash/pull/245/files#diff-33c477dfa4d3640fd07b0474999840da51c478a0813345cef0518197f72d7525R21 ?)

It is still pretty hard to use the Python API interactively, it was painful to use #2433 in the browser... I think this can fit what I was trying in #245 in a more general way (where we can share CLI/API code more easily, and expose to plugins too)

@ctb
Copy link
Contributor Author

ctb commented Mar 13, 2023

I really like this idea!

But I'm not sure how to do it technically. main(self, args) takes the results of argparse.ArgumentParser.parse_args. Is the idea that we would do a click-style expansion of the args?

click example from genome-grist:

@click.command(context_settings={"ignore_unknown_options": True})
@click.argument("configfile")
@click.option("--no-use-conda", is_flag=True, default=False)
@click.option("--verbose", is_flag=True)
@click.option("--outdir", nargs=1)
@click.option("-h", "--help", nargs=0)
@click.argument("snakemake_args", nargs=-1)
def run(configfile, snakemake_args, no_use_conda, verbose, outdir, help):
    "execute genome-grist workflow (using snakemake underneath)"
    targets = [ arg for arg in snakemake_args if not arg.startswith('-') ]
...

I'm not sure how clean that would be, but I can try it out.

An alternative might be to add another method (execute? do? run?) to the class that would take the expanded arguments. But that seems like it would be uglier and require more manual work, be redundant, and cause errors. Hmm.

@ctb
Copy link
Contributor Author

ctb commented Sep 23, 2023

ref #1112

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin_todo Write a plugin for this!
Projects
None yet
Development

No branches or pull requests

2 participants