Baker比上面那兩個更簡單。只需要:
- 在function的前面加一行@baker.command,該function就會成為command
- 在if __name__ == "__main__"區塊中加入baker.run()
Example:
import baker
# Add command @baker.command def foo(): "foo is test command" # Add parameter help @baker.command(params={"force": "Delete even if the file exists"}) def delete(filename, force=False): "Delete a file." @baker.command def delete2(filename, force=False): """Deletes a file. :param force: Delete even if the file exists. """ pass # Short options @baker.command(shortopts={"verbose": "v"}, params={"verbose": "Spew lots"}) def test(verbose=False): "test command" #Run script if __name__ == "__main__": baker.run()
Output:
python test.py
Usage: X:\test.py COMMAND <options>
Available commands:
delete Delete a file.
delete2 Deletes a file.
foo foo is test command
test
Use "X:\test.py <command> --help" for individual command help.
python test2.py delete2 --help
Usage: X:\test.py delete2 <filename>
Deletes a file.
Options:
--force Delete even if the file exists.
(specifying a single hyphen (-) in the argument list means all
subsequent arguments are treated as bare arguments, not options)
沒有留言:
張貼留言