Skip to content

Use subparsers in the notifications command #1675

@MarceloRobert

Description

@MarceloRobert

We currently use the --actions parameter to define what to do, but this has increased over time and since --actions is a required parameter, we turned it into subcommands but without much organization. We should split it into subparsers and delegate each action to a defined function.

Example code for subparsers:

from django.core.management.base import BaseCommand
from kernelCI_app.helpers.logger import out


class Command(BaseCommand):
    help = "Test command for subparsers"

    def add_arguments(self, parser):
        subparsers = parser.add_subparsers(
            title="action",
            help="Available actions",
        )
        parser_function = subparsers.add_parser(
            'function',
            help='Execute _function'
        )
        parser_function.set_defaults(func=self._function)

        second_function = subparsers.add_parser(
            'second_action',
            help='Execute _second_action'
        )
        second_function.set_defaults(func=self._second_action)

        second_function.add_argument(
            '--option',
            type=str,
            help='An example option for the second action',
        )

    def handle(
        self,
        *args,
        **options,
    ):
        out("Handle executed")
        func = options["func"]
        func(*args, **options)

    def _function(self, *args, **options):
        print(" ~ 🔍 _function (39) - options: %s" % options)
        print("Function executed")

    def _second_action(self, *args, option, **options):
        print(" ~ 🔍 _second_action (44) - option: %s" % option)
        print(" ~ 🔍 _second_action (42) - options: %s" % options)
        print("Second action executed")

Metadata

Metadata

Assignees

No one assigned

    Labels

    notificationstechnical debtNon-function issues like outdated libs or lack of proper tooling.

    Type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions