summaryrefslogtreecommitdiffstats
path: root/deluge/ui/console/cmdline/commands/help.py
blob: 2711eea995ae4ef1d4cef586d3ff2b4f73bc0ca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008-2009 Ido Abramovich <ido.deluge@gmail.com>
# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

from __future__ import unicode_literals

import logging

from twisted.internet import defer

import deluge.component as component

from . import BaseCommand

log = logging.getLogger(__name__)


class Command(BaseCommand):
    """Displays help on other commands"""

    def add_arguments(self, parser):
        parser.add_argument(
            'commands', metavar='<command>', nargs='*', help=_('One or more commands')
        )

    def handle(self, options):
        self.console = component.get('ConsoleUI')
        self._commands = self.console._commands
        deferred = defer.succeed(True)
        if options.commands:
            for arg in options.commands:
                try:
                    cmd = self._commands[arg]
                except KeyError:
                    self.console.write('{!error!}Unknown command %s' % arg)
                    continue
                try:
                    parser = cmd.create_parser()
                    self.console.write(parser.format_help())
                except AttributeError:
                    self.console.write(cmd.__doc__ or 'No help for this command')
                self.console.write(' ')
        else:
            self.console.set_batch_write(True)
            cmds_doc = ''
            for cmd in sorted(self._commands):
                if cmd in self._commands[cmd].aliases:
                    continue
                parser = self._commands[cmd].create_parser()
                cmd_doc = (
                    '{!info!}'
                    + '%-9s' % self._commands[cmd].name_with_alias
                    + '{!input!} - '
                    + self._commands[cmd].__doc__
                    + '\n     '
                    + parser.format_usage()
                    or ''
                )
                cmds_doc += parser.formatter.format_colors(cmd_doc)
            self.console.write(cmds_doc)
            self.console.write(' ')
            self.console.write('For help on a specific command, use `<command> --help`')
            self.console.set_batch_write(False)

        return deferred

    def complete(self, line):
        return [x for x in component.get('ConsoleUI')._commands if x.startswith(line)]