class Command(BaseCommand): 'Placeholer manager.' arg_list = BaseCommand.arg_list + ( make_option( '-l', '--list', action='store_true', help='List all placeholers.'), make_option( '-a', '--add', type=str, help='Add a placeholer.', default=''), make_option('-d', '--delete', type=int, help='Delete the oldest N placeholders.', default=0), ) def handle(self, *v, **u): args = self.arguments if args.list: for i in PlaceholderModel.objects.order_by('time').all(): print i.content if args.add: m = PlaceholderModel(content=args.add, time=datetime.now()) m.save() if args.delete: for i in PlaceholderModel.objects.order_by('time')[:args.delete]: i.delete()
class BaseCommand(BaseDjangoCommand): arg_list = ( make_option('-v', '--verbosity', action='store', dest='verbosity', default='1', choices=['0', '1', '2', '3'], help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'), make_option('--settings', help='The Python path to a settings module, e.g. "myproject.settings.main". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.'), make_option('--pythonpath', help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".'), make_option('--traceback', action='store_true', help='Print traceback on exception'), ) description = None epilog = None add_help = True prog = None usage = None def run_from_argv(self, argv): """ Set up any environment changes requested (e.g., Python path and Django settings), then run this command. """ parser = self.create_parser(argv[0], argv[1]) self.arguments = parser.parse_args(argv[2:]) handle_default_options(self.arguments) options = vars(self.arguments) self.execute(**options) def get_usage(self, subcommand): """ Return a brief description of how to use this command, by default from the attribute ``self.help``. """ return self.usage or '%(prog)s {subcommand} [options]'.format(subcommand=subcommand) def create_parser(self, prog_name, subcommand): """ Create and return the ``ArgumentParser`` which will be used to parse the arguments to this command. """ parser = ArgumentParser( description=self.description, epilog=self.epilog, add_help=self.add_help, prog=self.prog, usage=self.get_usage(subcommand), ) parser.add_argument('--version', action='version', version=self.get_version()) self.add_arguments(parser) return parser def add_arguments(self, parser): for args, kwargs in self.arg_list: parser.add_argument(*args, **kwargs)
class Command(BaseCommand): arg_list = BaseCommand.arg_list + ( make_option('ip', type=str), make_option('-r', '--remove', action='store_true', help='Delete this entry')) def handle(self, *v, **u): args = self.arguments assert(args.ip) if args.remove: BlockIpModel.objects.filter(ip=args.ip).delete() else: new_entry = BlockIpModel(ip=args.ip) new_entry.save()
class Command(BaseCommand): arg_list = BaseCommand.arg_list + (make_option('id', type=int), ) def handle(self, *v, **u): args = self.arguments assert (args.id) print ContentModel.objects.get(id=args.id).ip
class Command(BaseCommand): arg_list = BaseCommand.arg_list + ( make_option('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'), make_option('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)'), ) def handle(self, *args, **options): args = self.arguments print(args.accumulate(args.integers))
class Command(BaseCommand): arg_list = BaseCommand.arg_list + ( make_option('-r', '--raw', action='store_true', help='Post without number, nor saving to database.'), make_option('content', metavar='CONTENT', type=str, help='Post statu.'), ) def handle(self, *v, **u): args = self.arguments assert (args.content) if args.raw: postRawStatu(args.content) else: postStatu(args.content)
class Command(BaseCommand): arg_list = BaseCommand.arg_list + (make_option( '--foo', action='store_true', help='foo help'), ) def create_parser(self, *args, **kwargs): parser = super(Command, self).create_parser(*args, **kwargs) subparsers = parser.add_subparsers(help='sub-command help') # create the parser for the "a" command parser_a = subparsers.add_parser('a', help='a help') parser_a.add_argument('bar', type=int, help='bar help') # create the parser for the "b" command parser_b = subparsers.add_parser('b', help='b help') parser_b.add_argument('--baz', choices='XYZ', help='baz help') return parser def handle(self, *args, **options): print args, options