Revision fa532396 snf-django-lib/snf_django/management/commands/__init__.py

b/snf-django-lib/snf_django/management/commands/__init__.py
1
# Copyright 2012-2013 GRNET S.A. All rights reserved.
1
# Copyright 2012-2014 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
......
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from optparse import make_option
34
from optparse import (make_option, OptionParser, OptionGroup,
35
                      TitledHelpFormatter)
35 36

  
36 37
from django.core.management.base import BaseCommand, CommandError
37 38
from django.core.exceptions import FieldError
......
44 45
USER_EMAIL_FIELD = "user.email"
45 46

  
46 47

  
48
class SynnefoCommandFormatter(TitledHelpFormatter):
49
    def format_heading(self, heading):
50
        if heading == "Options":
51
            return ""
52
        return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading))
53

  
54

  
47 55
class SynnefoCommand(BaseCommand):
48 56
    option_list = BaseCommand.option_list + (
49 57
        make_option(
......
56 64
                 "csv [comma-separated output]"),
57 65
    )
58 66

  
67
    def create_parser(self, prog_name, subcommand):
68
        parser = OptionParser(prog=prog_name, add_help_option=False,
69
                              formatter=SynnefoCommandFormatter())
70

  
71
        parser.set_usage(self.usage(subcommand))
72
        parser.version = self.get_version()
73

  
74
        # Handle Django's and common options
75
        common_options = OptionGroup(parser, "Common Options")
76
        common_options.add_option("-h", "--help", action="help",
77
                                  help="show this help message and exit")
78

  
79
        common_options.add_option("--version", action="version",
80
                                  help="show program's version number and"
81
                                       "  exit")
82
        [common_options.add_option(o) for o in self.option_list]
83
        if common_options.option_list:
84
            parser.add_option_group(common_options)
85

  
86
        # Handle command specific options
87
        command_options = OptionGroup(parser, "Command Specific Options")
88
        [command_options.add_option(o)
89
         for o in getattr(self, "command_option_list", ())]
90
        if command_options.option_list:
91
            parser.add_option_group(command_options)
92

  
93
        return parser
94

  
59 95

  
60 96
class ListCommand(SynnefoCommand):
61 97
    """Generic *-list management command.

Also available in: Unified diff