Skip to content

Commit 5a2a8b2

Browse files
committed
Address comment
1 parent b313ddb commit 5a2a8b2

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
v2.6.0 is a feature release with the following features, fixes and enhancements:
66

7-
- [KIP-848 EA](https://cwiki.apache.org/confluence/display/KAFKA/KIP-848%3A+The+Next+Generation+of+the+Consumer+Rebalance+Protocol): Admin API for listing consumer groups now has an optional filter to return only groups of given types (#).
7+
- [KIP-848 EA](https://cwiki.apache.org/confluence/display/KAFKA/KIP-848%3A+The+Next+Generation+of+the+Consumer+Rebalance+Protocol): Admin API for listing consumer groups now has an optional filter to return only groups of given types (#1830).
88

99
confluent-kafka-python is based on librdkafka v2.6.0, see the
1010
[librdkafka release notes](https://github.com/confluentinc/librdkafka/releases/tag/v2.6.0)

examples/adminapi.py

+24-26
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import sys
3232
import threading
3333
import logging
34+
import argparse
3435

3536
logging.basicConfig()
3637

@@ -473,32 +474,28 @@ def example_list(a, args):
473474

474475

475476
def parse_list_consumer_groups_args(args, states, types):
477+
parser = argparse.ArgumentParser(prog='list_consumer_groups')
478+
parser.add_argument('-states')
479+
parser.add_argument('-types')
480+
parsed_args = parser.parse_args(args)
481+
476482
def usage(message):
477-
raise Exception(f"{message}\nUsage: list_consumer_groups [-states <state1> <state2> ..] "
478-
"[-types <type1> <type2> ..]")
479-
480-
if len(args) > 0:
481-
typeArray = False
482-
stateArray = False
483-
lastArray = 0
484-
for i in range(0, len(args)):
485-
if (args[i] == "-states"):
486-
if (stateArray):
487-
usage("Cannot pass the states flag (-states) more than once")
488-
lastArray = 1
489-
stateArray = True
490-
elif (args[i] == "-types"):
491-
if (typeArray):
492-
usage("Cannot pass the types flag (-types) more than once")
493-
lastArray = 2
494-
typeArray = True
495-
else:
496-
if (lastArray == 1):
497-
states.add(ConsumerGroupState[args[i]])
498-
elif (lastArray == 2):
499-
types.add(ConsumerGroupType[args[i]])
500-
else:
501-
usage(f"Unknown argument: {args[i]}")
483+
print(message)
484+
parser.print_usage()
485+
sys.exit(1)
486+
487+
if parsed_args.states:
488+
for arg in parsed_args.states.split(","):
489+
try:
490+
states.add(ConsumerGroupState[arg])
491+
except KeyError:
492+
usage(f"Invalid state: {arg}")
493+
if parsed_args.types:
494+
for arg in parsed_args.types.split(","):
495+
try:
496+
types.add(ConsumerGroupType[arg])
497+
except KeyError:
498+
usage(f"Invalid type: {arg}")
502499

503500

504501
def example_list_consumer_groups(a, args):
@@ -971,7 +968,8 @@ def example_elect_leaders(a, args):
971968
sys.stderr.write(' delete_acls <resource_type1> <resource_name1> <resource_patter_type1> ' +
972969
'<principal1> <host1> <operation1> <permission_type1> ..\n')
973970
sys.stderr.write(' list [<all|topics|brokers|groups>]\n')
974-
sys.stderr.write(' list_consumer_groups [<state1> <state2> ..]\n')
971+
sys.stderr.write(' list_consumer_groups [-states <state1>,<state2>,..] ' +
972+
'[-types <type1>,<type2>,..]\n')
975973
sys.stderr.write(' describe_consumer_groups <include_authorized_operations> <group1> <group2> ..\n')
976974
sys.stderr.write(' describe_topics <include_authorized_operations> <topic1> <topic2> ..\n')
977975
sys.stderr.write(' describe_cluster <include_authorized_operations>\n')

0 commit comments

Comments
 (0)