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

Use argparse to parse !command arguments? #30

Open
Lucidiot opened this issue Oct 7, 2018 · 4 comments
Open

Use argparse to parse !command arguments? #30

Lucidiot opened this issue Oct 7, 2018 · 4 comments
Labels
core issues related to core pinhook functionality such as plugin reloading, internal commands enhancement additional features

Comments

@Lucidiot
Copy link
Contributor

Lucidiot commented Oct 7, 2018

When creating command-line scripts, the cool way to parse command-line arguments is to use the argparse module. That could automatically generate help text (like for #19) with details about arguments. This should be easy to do with class-based plugins (#27) by using a way similar to how Django commands are designed:

import pinhook.plugin

class HelloPlugin(pinhook.plugin.BasePlugin):
    help = 'Say hello to someone!'

    def add_arguments(self, parser):
        parser.add_argument(
            'nick',
            nargs='?',
            default=None,
            help='Nickname of the user to say hello to',
        )

    def handle(self, msg, **options):
        message = 'Hello {}!'.format(options.get('nick', msg.nick))
        return pinhook.plugin.message(message)
@archangelic
Copy link
Owner

I love the functionality this will bring, but I worry it takes away the ability to make plugins really simple. I am going to think about ways to abstract this.

@Lucidiot
Copy link
Contributor Author

Lucidiot commented Oct 7, 2018

You could set add_arguments in BasePlugin to have the following default:

def add_arguments(self, parser):
    parser.add_argument('args', nargs='*')

Then it becomes possible to have the following plugin:

import pinhook.plugin

class HelloPlugin(pinhook.plugin.BasePlugin):
    help = 'Say hello to the world!'

    def handle(self, msg, args=['world']):
        return pinhook.plugin.message('Hello {} !'.format(', '.join(args)))

@archangelic archangelic added enhancement additional features future Things on the roadmap labels Oct 10, 2018
@archangelic archangelic added Pinhook v2 and removed future Things on the roadmap labels Oct 10, 2018
@archangelic archangelic added the core issues related to core pinhook functionality such as plugin reloading, internal commands label Jan 25, 2019
@archangelic
Copy link
Owner

archangelic commented Apr 30, 2019

I literally just came up with a way to do this. Bad pseudocode to follow.

@pinhook.plugin.register('thing')
@pinhook.plugin.add_argument(args*, kwargs**)
def thing(msg):
    return "pants"

basically this would run through and create an argument parser for the plugin object, and Message.args would be an extended str class that has the parsed string in it.

@archangelic
Copy link
Owner

Of course, thinking about this more, I am not sure how those decorators stack 🤔

@archangelic archangelic moved this to Future in pinhook Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core issues related to core pinhook functionality such as plugin reloading, internal commands enhancement additional features
Projects
Status: Future
Development

No branches or pull requests

2 participants