Skip to content

Commit d2bc0d0

Browse files
committed
Address comment
1 parent d7d17cf commit d2bc0d0

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

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):
@@ -934,7 +931,8 @@ def example_delete_records(a, args):
934931
sys.stderr.write(' delete_acls <resource_type1> <resource_name1> <resource_patter_type1> ' +
935932
'<principal1> <host1> <operation1> <permission_type1> ..\n')
936933
sys.stderr.write(' list [<all|topics|brokers|groups>]\n')
937-
sys.stderr.write(' list_consumer_groups [<state1> <state2> ..]\n')
934+
sys.stderr.write(' list_consumer_groups [-states <state1>,<state2>,..] ' +
935+
'[-types <type1>,<type2>,..]\n')
938936
sys.stderr.write(' describe_consumer_groups <include_authorized_operations> <group1> <group2> ..\n')
939937
sys.stderr.write(' describe_topics <include_authorized_operations> <topic1> <topic2> ..\n')
940938
sys.stderr.write(' describe_cluster <include_authorized_operations>\n')

0 commit comments

Comments
 (0)