【问题标题】:Using the subprocess library: error: unrecognized arguments使用子进程库:错误:无法识别的参数
【发布时间】:2019-11-06 12:26:21
【问题描述】:

所以基本上我有 15 个左右的脚本可以使用 SSH 库连接到各种网络设备。我想创建一个可以运行其他 python 脚本的顶级 python 文件,以便用户可以决定他们想要运行哪些脚本。有人建议我使用 subprocess 库,这似乎对我想做的事情最有意义。需要注意的是,我的 python 脚本包含用于运行的命令行 argparse 参数,例如:

python San_cable_test.py -deviceIP 172.1.1.1 -deviceUsername myUsername -devicePassword myPassword

到目前为止,我已经创建了一个顶级 python 文件,该文件设置为调用两个 python 脚本以开始用户可以输入的操作。但是,当我运行程序并选择其中一个选项并获取用户参数时,我得到了一个

error: unrecognized arguments:

我尝试了两种不同的方法,我将显示回溯:

usage: San_cable_test.py [-h] [-deviceIP DEVICEIP]
The name of this script is: San_cable_test.py
                         [-deviceUsername DEVICEUSERNAME]
                         [-devicePassword DEVICEPASSWORD]
San_cable_test.py: error: unrecognized arguments: 172.1.1.1 myUsername myPassword


usage: San_cable_test.py [-h] [-deviceIP DEVICEIP]
                         [-deviceUsername DEVICEUSERNAME]
The name of this script is: San_cable_test.py
                         [-devicePassword DEVICEPASSWORD]
San_cable_test.py: error: unrecognized arguments: -deviceIP 172.1.1.1 -deviceUsername myUsername -devicePassword myPassword


这是我第一次使用 subprocces 库,我不知道我是否正确地调用了这些脚本。问题是这些脚本是在命令行中使用 argparse 运行的,所以这就是问题所在。不幸的是,我正在使用 2.7.16,因为这件奇怪的公司事情,我一直试图让我的经理知道 2.7 很快就会不受支持,但目前这不相关。这是我的代码的重要部分。非常感谢您的帮助!


def runMain():

    scriptName = os.path.basename(__file__)

    print("The name of this script: " + scriptName + "\n")

    scriptPurpose = 'This script is the top-level module that can invoke any script the user desires !\n'

    while True:
        optionPrinter()

        user_input = input("Please select an option for which your heart desires...\n")

        switch_result = mySwitch(user_input)

        if switch_result == "our_Switch":
            deviceIP = raw_input("Enter the IP address for the device")
            deviceUsername = raw_input("Enter the username for the device")
            devicePassword = raw_input("Enter the password for the device")

            subprocess.call(['python', 'our_Switch.py', deviceIP, deviceUsername, devicePassword])

        elif switch_result == "San_cable_test":
            deviceIP = raw_input("Enter the IP address for the device")
            deviceUsername = raw_input("Enter the username for the device")
            devicePassword = raw_input("Enter the password for the device")
            subprocess.call(['python', 'San_cable_test.py', deviceIP, deviceUsername, devicePassword])

        else:
            print("Exiting the program now, have a great day !\n")
            sys.exit(-1)

if __name__ == '__main__':

这是在其中一个脚本中使用 argparse 的示例

def runMain():

    scriptName = os.path.basename(__file__)

    print("The name of this script is: " + scriptName)

    scriptPurpose = 'This script enables and disables the SAN switches'

    parser = argparse.ArgumentParser(description=scriptPurpose, formatter_class=RawTextHelpFormatter)
    parser.add_argument("-deviceIP", help="Target device IP address", type=str)
    parser.add_argument("-deviceUsername", help="Target device username", type=str)
    parser.add_argument("-devicePassword", help="Target device password", type=str)
    args = parser.parse_args()

    if args.deviceIP is None:
        print("The device IP parameter is blank\n")
    else:
        deviceIP = args.deviceIP

    if args.deviceUsername is None:
        print("The device userName parameter is blank\n")
    else:
        deviceUsername = args.deviceUsername

    if args.devicePassword is None:
        print("The device password parameter is blank\n")
    else:
        devicePassword = args.devicePassword

    print("**********************\n")
    print (deviceIP + " " + deviceUsername + " " + devicePassword)
    print("**********************\n")

    print("This script allows the user to enable and disable ports on a SAN switch to see how it behaves\n")

    print("Checking to see if the SAN switch is pingable\n")

    test_ping = canPing(deviceIP)

    if test_ping:
        print("The switch is pingable, let's proceed !\n")
    else:
        print("This device is not pingable unfortunately, sorry... : (\n")
        sys.exit(-1)

    sshConnection = connectToSSH(deviceIP, deviceUsername, devicePassword)

    while True:
        optionPrinter()

        user_input = input("Select an option from the menu\n")

        switch_result = mySwitch_function(user_input)

        if switch_result == 'ShowPort':
            portShow(sshConnection)
        elif switch_result == 'SwitchShow':
            switchShow(sshConnection)
        elif switch_result == 'EnablePort':
            enablePort(sshConnection)
        elif switch_result == 'DisablePort':
            disablePort(sshConnection)
        elif switch_result == 'disableEnable':
            disableEnableIteration(sshConnection)
        else:
            print("Program is exiting now, have a great day/night ! \n")
            sys.exit(-1)

【问题讨论】:

    标签: python command-line arguments subprocess argparse


    【解决方案1】:

    您在参数之前缺少选项名称。

    subprocess.call(['python', 'our_Switch.py', '-deviceIP', deviceIP, '-deviceUsername', deviceUsername, '-devicePassword', devicePassword])
    

    但是,如果您将这些其他脚本更改为 Python 模块,您可以直接将其作为函数导入和调用,而不是将它们作为子进程运行,这可能会更简洁。

    【讨论】:

    • @Bamar 我爱你,你成就了我的一天!我会检查你的答案(它说等待 7 分钟)。你是英雄!
    • @Bamar 你能解释一下为什么最好将这些脚本作为函数导入吗?这些文件中有很多函数,我没有为它们创建类,因为写出没有类的函数更容易(老实说,我对 python 还很陌生,请放轻松)
    • 因为您正在创建不必要的过程,并经历额外的参数格式化和解析。命令是给人类的,函数是给程序的。
    • @Bamar 它会通过使用子进程减慢我的程序?
    • 一点,但这不是重点。你只是让事情变得更复杂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2015-12-24
    • 2021-01-14
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多