← Back to team overview

launchpad-dev team mailing list archive

Re: script_commands library

 

Hi,

On Mon, Aug 8, 2011 at 4:15 PM, Aaron Bentley <aaron@xxxxxxxxxxxxx> wrote:
> While hacking on the local-latency script (which now has a configurable
> - --port, by the way), I was unable to resist my compulsion to make it
> simpler the next time I do this kind of thing.

Resistance is futile.

> The result is utilities/script_commands.py, which makes it easy to turn
> a function into a script, or script subcommand.
>
> Here's the definition of the "start" subcommand:
>
> @helps(delay='Length of delay in miliseconds (each way).',
>       port='Port to induce delay on.')
> def start(delay=500, port=443):
>    """Add artificial latency to the lo interface on the specified port."""
>
> As you can see, the only weird thing about the function is the "helps"
> decorator that describes each parameter.  And that's optional.  The stop
> command, which takes no parameters, is even simpler:
>
> def stop():
>
>
> You run such commands with script.commands.run_from_args.  Each
> parameter that has a default is converted into an option, and the type
> of the parameter is determined from the default.  (I haven't needed
> positional parameters yet, so I haven't added support for them yet.)
>
> If your script needs subcommands, you stick them in a dict and call
> script.commands.run_subcommand instead of run_from_args.
>
> So the next time you're writing a utility script, give script_commands a
> try.  It could save you time and drudgery.

This sounds a lot like a library I wrote some time ago called
Commandant.  It's a wrapper around bzrlib that let's you easily write
applications with a Bazaar-like user experience.  It might be
interesting if you want to go further and add topics, run shell
scripts, etc.:

 lp:commandant

Thanks,
J.


Follow ups

References