from __future__ import absolute_import
from __future__ import print_function

from typing import Any

from django.core.management.base import CommandParser

from zerver.lib.actions import bulk_remove_subscriptions
from zerver.lib.management import ZulipBaseCommand
from zerver.models import UserProfile, get_stream

class Command(ZulipBaseCommand):
    help = """Remove some or all users in a realm from a stream."""

    def add_arguments(self, parser):
        # type: (CommandParser) -> None
        parser.add_argument('-s', '--stream',
                            dest='stream',
                            type=str,
                            help='A stream name.')

        parser.add_argument('-u', '--users',
                            dest='users',
                            type=str,
                            help='A comma-separated list of email addresses.')

        parser.add_argument('-a', '--all-users',
                            dest='all_users',
                            action="store_true",
                            default=False,
                            help='Remove all users in this realm from this stream.')
        self.add_realm_args(parser, True)

    def handle(self, **options):
        # type: (**Any) -> None
        realm = self.get_realm(options)

        if realm is None or options["stream"] is None or \
                (options["users"] is None and not options["all_users"]):
            self.print_help("./manage.py", "remove_users_from_stream")
            exit(1)

        stream_name = options["stream"].strip()
        stream = get_stream(stream_name, realm)

        if options["all_users"]:
            user_profiles = UserProfile.objects.filter(realm=realm)
        else:
            emails = set([email.strip() for email in options["users"].split(",")])
            user_profiles = []
            for email in emails:
                user_profiles.append(self.get_user(email, realm))

        result = bulk_remove_subscriptions(user_profiles, [stream])
        not_subscribed = result[1]
        not_subscribed_users = {tup[0] for tup in not_subscribed}

        for user_profile in user_profiles:
            if user_profile in not_subscribed_users:
                print("%s was not subscribed" % (user_profile.email,))
            else:
                print("Removed %s from %s" % (user_profile.email, stream_name))

@@ -569,32 +569,75 @@
  dest='stream',%0A
+                            required=True,%0A
                 
@@ -714,213 +714,8 @@
 ')%0A%0A
-        parser.add_argument('-u', '--users',%0A                            dest='users',%0A                            type=str,%0A                            help='A comma-separated list of email addresses.')%0A%0A
     
@@ -982,16 +982,17 @@
 ream.')%0A
+%0A
         
@@ -1024,16 +1024,56 @@
 r, True)
+%0A        self.add_user_list_args(parser)
 %0A%0A    de
@@ -1171,25 +1171,24 @@
 ptions)%0A
-%0A
         
 if realm
@@ -1183,108 +1183,89 @@
     
-if realm is None or options%5B%22stream%22%5D is None or %5C%0A                (options%5B%22users%22%5D is None and not
+user_profiles = self.get_users(options, realm)%0A%0A        if bool(user_profiles) ==
  opt
@@ -1281,17 +1281,16 @@
 _users%22%5D
-)
 :%0A      
@@ -1576,235 +1576,8 @@
 alm)
-%0A        else:%0A            emails = set(%5Bemail.strip() for email in options%5B%22users%22%5D.split(%22,%22)%5D)%0A            user_profiles = %5B%5D%0A            for email in emails:%0A                user_profiles.append(self.get_user(email, realm))
 %0A%0A  

Use add_user_list_args in remove_users_from_stream.