HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //proc/1233/cwd/home/arjun/projects/buyercall/cli/cli.py
import logging
import os
import sys

import click

cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                          'commands'))


class CLI(click.MultiCommand):
    def list_commands(self, ctx):
        """
        Obtain a list of all available commands.

        :param ctx: Click context
        :return: List of commands
        """
        commands = []

        for filename in os.listdir(cmd_folder):
            if filename.endswith('.py') and \
                    filename.startswith('cmd_'):
                commands.append(filename[4:-3])
        commands.sort()

        return commands

    def get_command(self, ctx, name):
        """
        Get a specific command by looking up the module.

        :param ctx: Click context
        :param name: Command name
        :return: Module's cli function
        """
        try:
            if sys.version_info[0] == 2:
                name = name.encode('ascii', 'replace')
            mod = __import__('cli.commands.cmd_' + name,
                             None, None, ['cli'])
        except ImportError as e:
            logging.error('Error importing module {0}:\n{0}'.format(name, e))
            exit(1)

        return mod.cli


@click.command(cls=CLI)
def cli():
    """ Commands to help manage your project. """
    pass