【发布时间】:2017-08-13 04:34:51
【问题描述】:
我正在尝试将 python 中的 argparse 模块用于我正在编写的脚本,我希望能够在我的命令中使用多个标志并存储这些变量。这是我目前所拥有的。
parser = argparse.ArgumentParser(
description='This program allows for you to create a User in AD and add that User to a specific AD Group',
epilog='Note: In order to run this script you must have login credentials of an AD admin. \
You must also know the username, if you intend to search for users, or the group name if intending to search for group. \
The logging level is set to info by default.')
parser.add_argument('--listuser', nargs=?, default='None', type=str,help='Lists attributes for a given user. Example: "--listuser --username <AD username(s)> "')
parser.add_argument('--listusergroups', nargs=?, default='None', type=str, help='Lists the groups a user is associated with attributes for a given user. Example: "--listusergroups < AD username(s)>"')
parser.add_argument('--listgroupmembers', nargs=?, default='None', type=str,help='Lists the members of a given group. Example: "--listgroupmembers --groups <group(s)>"')
parser.add_argument('--addgroupmembers', nargs=?, default='None', type=str, help='Adds members to a specific AD group. Example: "./AD_Utility --addgroupmembers <group(s)> --username <username(s)>"')
parser.add_argument('--renamegroup', nargs=?, default='None', type=str, help='Renames the AD group"./AD_Utility --rename <group(s)> <new_group_name>"')
parser.add_argument('--disableuser', nargs=?, default='None', type=str, help='Disables user from AD. Example: "./AD_Utility --disableusers --username <username(s)>"')
parser.add_argument('--removeuserfromgroup', nargs=?, default='None', type=str, help='Disables user from AD. Example: "./AD_Utility --removeuserfromgroup <username> --group <groupname>"')
parser.add_argument('--creategroups', nargs=?, default='None', type=str, help='Enter the name of the group you want to search for. Example: "aws')
parser.add_argument('--username', nargs=?, default='None', type=str, help='Username of the account you intend to gain information on. Example: "amazing')
parser.add_argument('--scope', nargs=?, default='None', type=str, help='Scope of the search for which you are executing. Example: "aws')
args = parser.parse_args()
function_mapping = {
"listuser": attributes_of_a_user, # or optionally: {"function": attributes_of_a_user, "nargs": "?", "default":..}
"listusergroups": find_users_groups,
"listgroupmembers": get_users_associated_with_groups,
"addgroupmembers": add_users_to_group,
# "renamegroup":
"disableuser": disable_user,
"creategroups": create_group,
"createuser": create_user_in_ad,
# "username"
# "scope"
}
for function_name, function in function_mapping.iteritems():
if args[function_name]:
function(*args[function_name])
service = args.serviceaccount
group = args.groups
listuser = args.listuser
listusergroups = args.listusergroups
addgroupmembers = args.addgroupmembers
disableuser = args.disableuser
group_name, new_group_name = args.renamegroup
scope = args.scope
username = args.username
new_users = get_users()
当我运行任何命令时,我如何得到无效的语法:
./<script_name>.py --create test123
谁能看到我在添加参数中的哪个地方出错了?
【问题讨论】:
标签: python python-2.7 function argparse