python的命令行参数处理

时间:2023-02-08 14:31:55
 
import argparse
 
# A position argument
func_choice = {'client' : 'client function', "server" : "server function"}

# parser
parser = argparse.ArgumentParser(description = "Select your favorite function")
# add argument
parser.add_argument('role', choices = func_choice, help = "select your role")
parser.add_argument("-p", type = int, help = "select your port")

# parse
args = parser.parse_args()
print("You select role {0} and port {1}".format(func_choice[args.role], args.p))