12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- # -*- coding: utf-8 -*-
- # name: multicmd
- # key: multicmd
- # --
- #!/usr/bin/env python
- import argparse
- import logging
- import sys
- parser = argparse.ArgumentParser(usage="%(prog)s [options] args...")
- parser.add_argument("-v", action="append_const", const=1, dest="verbosity", default=[],
- help="Be more verbose. Can be specified multiple times to increase verbosity further")
- subparsers = parser.add_subparsers(help="Action to be taken")
- def _action(name):
- def decorator(func):
- parser = subparsers.add_parser(name)
- parser.set_defaults(action=func)
- return parser
- return decorator
- @_action("${1:command}")
- def cmd_$1(args):
- $0
- return 0
- ################################## Boilerplate ################################
- def _configure_logging(args):
- verbosity_level = len(args.verbosity)
- if verbosity_level == 0:
- level = "WARNING"
- elif verbosity_level == 1:
- level = "INFO"
- else:
- level = "DEBUG"
- logging.basicConfig(
- stream=sys.stderr,
- level=level,
- format="%(asctime)s -- %(message)s"
- )
- #### For use with entry_points/console_scripts
- def main_entry_point():
- args = parser.parse_args()
- _configure_logging(args)
- sys.exit(args.action(args))
- if __name__ == "__main__":
- main_entry_point()
|