Commit c928e21f authored by Christos Stavrakakis's avatar Christos Stavrakakis

snf_django: Use option groups in SynnefoCommand

Extend SynnefoCommand to use option groups to distinguish between common
and command-specific options, by overriding the 'create_parser' method.
The command specific options must be declared in the
'command_option_list' attribute of the SynnefoCommand. Also, we use a
custom formatter to pretty-print options.
parent 9985db89
......@@ -48,7 +48,7 @@ class Command(SynnefoCommand):
help = "Get available statistics of Cyclades service"
can_import_settings = True
option_list = SynnefoCommand.option_list + (
command_option_list = (
make_option("--backend",
dest="backend",
help="Include statistics only for this backend."),
......
......@@ -52,7 +52,8 @@ DEFAULT_RESOURCES = ["cyclades.cpu",
class Command(SynnefoCommand):
help = """Check and fix quota violations for Cyclades resources.
"""
option_list = SynnefoCommand.option_list + (
command_option_list = (
make_option("--max-operations",
help="Limit operations per backend."),
make_option("--users", dest="users",
......
# Copyright 2012-2013 GRNET S.A. All rights reserved.
# Copyright 2012-2014 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -31,7 +31,8 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
from optparse import (make_option, OptionParser, OptionGroup,
TitledHelpFormatter)
from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import FieldError
......@@ -44,6 +45,13 @@ import distutils
USER_EMAIL_FIELD = "user.email"
class SynnefoCommandFormatter(TitledHelpFormatter):
def format_heading(self, heading):
if heading == "Options":
return ""
return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading))
class SynnefoCommand(BaseCommand):
option_list = BaseCommand.option_list + (
make_option(
......@@ -56,6 +64,34 @@ class SynnefoCommand(BaseCommand):
"csv [comma-separated output]"),
)
def create_parser(self, prog_name, subcommand):
parser = OptionParser(prog=prog_name, add_help_option=False,
formatter=SynnefoCommandFormatter())
parser.set_usage(self.usage(subcommand))
parser.version = self.get_version()
# Handle Django's and common options
common_options = OptionGroup(parser, "Common Options")
common_options.add_option("-h", "--help", action="help",
help="show this help message and exit")
common_options.add_option("--version", action="version",
help="show program's version number and"
" exit")
[common_options.add_option(o) for o in self.option_list]
if common_options.option_list:
parser.add_option_group(common_options)
# Handle command specific options
command_options = OptionGroup(parser, "Command Specific Options")
[command_options.add_option(o)
for o in getattr(self, "command_option_list", ())]
if command_options.option_list:
parser.add_option_group(command_options)
return parser
class ListCommand(SynnefoCommand):
"""Generic *-list management command.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment