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

Implement API for "subcommand" style interfaces #43

Closed
khorn opened this issue Oct 30, 2012 · 9 comments
Closed

Implement API for "subcommand" style interfaces #43

khorn opened this issue Oct 30, 2012 · 9 comments
Labels

Comments

@khorn
Copy link
Contributor

khorn commented Oct 30, 2012

Plumbum should provide an API for "subcommand" style interfaces, in which the cli app has a number of subcommands.

e.g. svn, hg, bzr, git, etc.

One way to do this might be to provide a cli.subcommand decorator which you would use to decorate functions in a cli.Application. Then the parsing logic would look for any of these and use whichever one was specified on the command line instead of the current main method.

@tomerfiliba
Copy link
Owner

nice idea!

@khorn
Copy link
Contributor Author

khorn commented Oct 30, 2012

After thinking about it, I'm not sure my suggested method for how to implement this is the best one (though it might work fine, it depends on plumbum internals, which I'm not that familiar with), but regardless, I'd like to see some kind of subcommand support in plumbum. Thanks for giving the world such a nice tool!

@tomerfiliba
Copy link
Owner

here's a sketch:

class Git(cli.Application):

     @cli.subcommand("commit")
     class Commit(cli.Subcommand):
          @cli.switch("-a")
          def all(self):
              pass
          @cli.switch("-m", str)
          def message(self, msg):
              pass

which allows you to write run git commit -a -m foo

@khorn
Copy link
Contributor Author

khorn commented Oct 30, 2012

Ah, nice! And you could maybe still use @cli.switch to create "global" flags for the base command. I like it.

@khorn
Copy link
Contributor Author

khorn commented Oct 30, 2012

Though it would also be nice to have a way to define subcommands outside of the cli.Application class. It's not clear from your sketch whether the subcommand would be required to be a nested class, and I can't think why it would be required, but I thought I'd mention it.

Also an API for adding subcommands to a cli.Application might be handy, e.g.

app.addSubcommand(subcommand)

or something like that. This would allow you to store your subcommands someplace else and import them. Useful for anyone who wants to build a plugin architecture around this.

@tomerfiliba
Copy link
Owner

a better approach:

class GitCommit(cli.SubApplication):
    @switch("-a")
    def all(self):
        pass

class Git(cli.Application):
    commit = cli.Subcommand(GitCommit)
    verbose = cli.Flag("--verbose")

@khorn
Copy link
Contributor Author

khorn commented Oct 30, 2012

Yes, that's even nicer. As long as whatever processes/detects the subcommands happens when cli.Application.run is called rather than at class instantiation (__init__) time, so you can add subcommands to the application object at run time. I assume this is already the case.

questions:

  • do you need cli.SubApplication or could you use cli.Application as the base class for the subcommand? (depends on plumbum internal I presume)
  • do you need the cli.Subcommand call? What would it do exactly?

It seems to me that you might only need either a special type for subcommands (cli.SubApplication') **or** a callable to register the suncommand (cli.Subcommand`), but not both. This may be my lack of understanding of how plumbum works internally showing, though.

Hmmm...maybe you need both in order to give the subcommand a reference to the parent application? (You might need this in order to e.g. honor a global --verbose flag on all subcommands)

tomerfiliba added a commit that referenced this issue Oct 31, 2012
@tomerfiliba
Copy link
Owner

hey @khorn - i've pushed subcommand support. see the unittest for how to use it:

class GeetAdd(cli.Application):

@khorn
Copy link
Contributor Author

khorn commented Nov 1, 2012

Totally awesome! Way to go!

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

No branches or pull requests

2 participants