Paver is a library for easily creating command line tools in Python.
You can easily create command line tools to some extent using Python's standard library such as `ʻargparse``, but Paver makes it much easier to create scripts with subcommands in particular. ..
The Paver original documentation focuses on automating Python packaging, so I'd like to explain it as a task definition and execution tool.
$ easy_install paver
If you create a file called pavement.py
and then create a function with a decorator called @ task
, it becomes a task.
pavement.py
from paver.easy import *
@task
def hello():
"""My first task."""
print 'hello'
To run the task, run paver task name
.
$ paver hello
---> pavement.hello
hello
paver -h
also shows the tasks you have defined. docstrin is the description for that command.
$ paver -h
---> paver.tasks.help
Usage: paver [global options] taskname [task options] [taskname [taskoptions]]
Options:
--version show program's version number and exit
-n, --dry-run don't actually do anything
…
Tasks from pavement:
hello - My first task.
To take an argument, add the decorator @consume_args
to the function that defines the task and write the argument ```args``.
pavement.py
from paver.easy import *
@task
@consume_args
def hello(args):
"""My first task."""
print 'hello', args
$ paver hello foo bar
---> pavement.hello
hello ['foo', 'bar']
Use the @cmdopts
decorator to define options, and the argument ```optionsto receive them. Pass the
@cmdoptsdecorator as an argument a list of options defined in tuples in the form
(long name, one letter, help) . Adding
to the end of a long name gives you the option to take an argument.
pavement.py
from paver.easy import *
@task
@cmdopts([
('foo', 'f', "The foo"), # -f or--foo
('bar=', 'b', "Bar bar bar"), # -b xxx or--bar=xxx
])
def hello(options):
"""My first task."""
print 'hello', options.foo, options.bar
$ paver hello --foo -b 3
---> pavement.hello
hello True 3
Actually, ```optionsis defined as a global variable of
paver.easyeven if it is not taken as an argument, so if you do
from paver.easy import * ``, you do not have to take an argument. Can also be used.
pavement.py
from paver.easy import *
@task
@cmdopts([
('foo', 'f', "The foo"),
('bar=', 'b', "Bar bar bar"),
])
def hello():
"""My first task."""
print 'hello', options.foo, options.bar