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

Commands that are a substring of the functions they call? #17

Closed
neithere opened this issue Aug 20, 2013 · 4 comments
Closed

Commands that are a substring of the functions they call? #17

neithere opened this issue Aug 20, 2013 · 4 comments
Assignees
Milestone

Comments

@neithere
Copy link
Owner

Is it possible to register a command (e.g. spam) that calls a function with a slightly different name (e.g. do_spam)?

Note: This issue has been automatically migrated from Bitbucket
Created by Zearin on 2012-04-17 11:57:04+00:00, last updated: 2012-11-04 05:19:27+00:00

@ghost ghost assigned neithere Aug 20, 2013
@neithere
Copy link
Owner Author

I think you mean this:

from argh import alias

@alias('spam')
def do_spam(args):
    print 'Albatross!!!'

If not, please reopen the issue and clarify the use case.

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-04-17 14:05:40+00:00, last updated: 2012-04-17 14:06:12+00:00

@neithere
Copy link
Owner Author

Couple things:

  • I think your example would add both do_spam and spam. I need the function name to be do_spam but the parser to recognize only spam from input, but call the correct function.
  • I have a class with lots of do_* methods. I want to have the parser automatically “know” that command should call do_command without having to specify this each time.

Note: This comment has been automatically migrated from Bitbucket
Created by Zearin on 2012-04-17 14:13:46+00:00

@neithere
Copy link
Owner Author

  • Actually at this point {{{@alias}}} renames the command, see the [[http://packages.python.org/argh/reference.html#argh.decorators.alias|documentation]]. It is true that the decorator's name is currently a bit misleading.
  • I think this is a marginal case that requires your class to do some extra work, e.g. have a method that returns a list of commands like this:
from argh import ArghParser, alias, arg

class App:

    @arg(...)
    def do_x(self): pass

    @arg(...)
    def do_y(self): pass

    def get_commands(self):
        names = [name for name in self.__dict__ if name.startswith('do_')]
        for name in names:
            meth = getattr(self, name)
            yield alias(name[3:])(meth)

    def dispatch(self, argv):
        parser = ArghParser()
        parser.add_commands(self.get_commands())
        parser.dispatch(argv)

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-04-17 15:07:32+00:00, last updated: 2012-04-17 15:12:28+00:00

@neithere
Copy link
Owner Author

I don't see any reason why this issue should stay open. Please correct me if I'm wrong.

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-11-04 05:19:27+00:00

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

No branches or pull requests

1 participant