Прочитать параметр командной строки с помощью getopt python - PullRequest
0 голосов
/ 11 июля 2020

Я новичок в python и программировании сокетов. когда я пытаюсь прочитать параметры командной строки с помощью модуля getopt, я могу прочитать только первые два параметра и не могу прочитать другие параметры. Вот часть моего кода для чтения командной строки:

import getopt
import sys

def main():
    global listen
    global port
    global execute
    global command
    global upload_destination
    global target

    if not len(sys.argv[1:]):
        usage()
# read the commandline options
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hle:t:p:cu:", ["help","listen","execute","target","port","command","upload"])
    except getopt.GetoptError as err:
        print (str(err))
        usage()


    for o,a in opts:
        if o in ("-h","--help"):
            usage()
            print("test1")
        elif o in ("-l","--listen"):
            listen = True
            print("-l is", listen)
        elif o in ("-e", "--execute"):
            execute = a
            print("test2")
        elif o in ("-c", "--commandshell"):
            command = True
            print("test3")
        elif o in ("-u", "--upload"):
            upload_destination = a
            print("-u is: ", a)
        elif o in ("-t", "--target"):
            target = a
            print("-t is:", a)
        elif o in ("-p", "--port"):
            port = int(a)
            print("-p is:", port)
        else:
            assert False,"Unhandled Option" 

, например, если я запускаю

my_program.py -t 127.0.0.1 -p 9999 -l -u d:\\test.txt

, результат будет:

-t is: 127.0.0.1
-p is: 9999

и upload_destination пусто, а listen по-прежнему ложно

что мне делать, чтобы исправить это?

...