Aginu Aginu - 5 months ago 9
Python Question

Python get passed arguments

I'm working on a project which allows the user to set a path to upload files to by adding the necessary argument but for whatever reason, the upload_destination variable is always empty!
Here's my code

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

if not len(sys.argv[1:]):
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)

for o,a in opts:
if o in ("-h", "--help"):
elif o in ("-l", "--listen"):
listen = True
elif o in ("-e", "--execute"):
execute = True
elif o in ("-c", "--commandshell"):
command = True
elif o in ("-u", "--upload"):
#Here's the problem, a is empty even though I include a path
upload_destination = a
elif o in ("-t", "--target"):
target = a
elif o in ("-p", "--port"):
port = int(a)
assert False, "Unhandled Option"

if not listen and len(target) and port > 0:
buffer =

if listen:

I call the programm by entering

C:\Users\Asus\Desktop\PythonTest>python -l -c -p 3500 -u C:\Users\Asus\Desktop\Test


It's a simple missing colon :.

options is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (':'; i.e., the same format that Unix getopt() uses).

Change the "hle:t:p:cu" to "hle:t:p:cu:" and it should work (at least it did work for me with Win7/Python3.5).

When you execute print(opts, args) with your code, you get:

([('-l', ''), ('-c', ''), ('-p', '3500'), ('-u', '')], ['C:UsersAsusDesktopTest'])

with the added colon it becomes:

([('-l', ''), ('-c', ''), ('-p', '3500'), ('-u', 'C:UsersAsusDesktopTest')], [])

Without the colon C:\Users\... becomes a new argument.