From 2f6ecd9171d67a55621ae97dd2d13f551884123a Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Sat, 16 Nov 2013 02:26:47 +0100 Subject: [PATCH] commands: Move to using a help attribute instead of __doc__ --- mopidy/backends/local/command.py | 2 +- mopidy/commands.py | 4 ++-- mopidy/utils/command.py | 12 ++++++++---- tests/utils/command_test.py | 12 ++++++------ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/mopidy/backends/local/command.py b/mopidy/backends/local/command.py index 58746cf0..6b41dda9 100644 --- a/mopidy/backends/local/command.py +++ b/mopidy/backends/local/command.py @@ -20,7 +20,7 @@ class LocalCommand(command.Command): class ScanCommand(command.Command): - """Scan local media files and populate the local library.""" + help = "Scan local media files and populate the local library." def __init__(self): super(ScanCommand, self).__init__() diff --git a/mopidy/commands.py b/mopidy/commands.py index e807cb65..503733ea 100644 --- a/mopidy/commands.py +++ b/mopidy/commands.py @@ -136,7 +136,7 @@ class RootCommand(command.Command): class ConfigCommand(command.Command): - """Show currently active configuration.""" + help = "Show currently active configuration." def __init__(self): super(ConfigCommand, self).__init__() @@ -148,7 +148,7 @@ class ConfigCommand(command.Command): class DepsCommand(command.Command): - """Show dependencies and debug information.""" + help = "Show dependencies and debug information." def __init__(self): super(DepsCommand, self).__init__() diff --git a/mopidy/utils/command.py b/mopidy/utils/command.py index dd9280c5..8994c6fd 100644 --- a/mopidy/utils/command.py +++ b/mopidy/utils/command.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import argparse import collections import os @@ -31,6 +33,8 @@ class _HelpAction(argparse.Action): class Command(object): + help = None + def __init__(self): self._children = collections.OrderedDict() self._arguments = [] @@ -78,8 +82,8 @@ class Command(object): formatter = argparse.HelpFormatter(prog) formatter.add_usage(None, actions, []) - if self.__doc__: - formatter.add_text(self.__doc__) + if self.help: + formatter.add_text(self.help) if actions: formatter.add_text('OPTIONS:') @@ -100,11 +104,11 @@ class Command(object): def _subhelp(self, name, result): actions = self._build()[1] - if self.__doc__ or actions: + if self.help or actions: formatter = argparse.HelpFormatter(name) formatter.add_usage(None, actions, [], '') formatter.start_section(None) - formatter.add_text(self.__doc__) + formatter.add_text(self.help) formatter.start_section(None) formatter.add_arguments(actions) formatter.end_section() diff --git a/tests/utils/command_test.py b/tests/utils/command_test.py index aa69f012..634f5df2 100644 --- a/tests/utils/command_test.py +++ b/tests/utils/command_test.py @@ -293,7 +293,7 @@ class HelpTest(unittest.TestCase): def test_command_with_documentation(self): cmd = command.Command() - cmd.__doc__ = 'some text about everything this command does.' + cmd.help = 'some text about everything this command does.' expected = ('usage: foo\n\n' 'some text about everything this command does.') @@ -301,7 +301,7 @@ class HelpTest(unittest.TestCase): def test_command_with_documentation_and_option(self): cmd = command.Command() - cmd.__doc__ = 'some text about everything this command does.' + cmd.help = 'some text about everything this command does.' cmd.add_argument('-h', '--help', action='store_true', help='show this message') @@ -320,7 +320,7 @@ class HelpTest(unittest.TestCase): def test_subcommand_with_documentation_shown(self): child = command.Command() - child.__doc__ = 'some text about everything this command does.' + child.help = 'some text about everything this command does.' cmd = command.Command() cmd.add_child('bar', child) @@ -359,7 +359,7 @@ class HelpTest(unittest.TestCase): def test_subcommand_with_options_and_documentation(self): child = command.Command() - child.__doc__ = ' some text about everything this command does.' + child.help = ' some text about everything this command does.' child.add_argument('-h', '--help', action='store_true', help='show this message') @@ -428,11 +428,11 @@ class HelpTest(unittest.TestCase): def test_command_with_options_doc_and_subcommand_with_option_and_doc(self): child = command.Command() - child.__doc__ = 'some text about this sub-command.' + child.help = 'some text about this sub-command.' child.add_argument('--test', help='the great and wonderful') cmd = command.Command() - cmd.__doc__ = 'some text about everything this command does.' + cmd.help = 'some text about everything this command does.' cmd.add_argument('-h', '--help', action='store_true', help='show this message') cmd.add_child('bar', child)