2017-12-17 1 views
1

Ich versuche, mit opts zu arbeiten, aber kann es nicht in anderen PC arbeiten, weil Argumente immer leer ist. Unten ist mein Code.Python opts bekommen empy Werte

import getopt 
import sys 

try: 
    print getopt.getopt(sys.argv[1:], "f::c::") 
    opts, args = getopt.getopt(sys.argv[1:], "f::c::") 
except getopt.GetoptError, err: 
    # print help information and exit: 
    print str(err) # will print something like "option -a not recognized" 
    sys.exit(2) 

print opts 
print args 

funcion = None 
tipo = None 

for opt, arg in opts: 
    if opt in ('-f'): 
     funcion = arg 
    if opt in ('-c'): 
     tipo = arg 

print funcion 
print tipo 

Nutzung Test:

python test.py –f import_dbs –c 1 

PC Ein Ergebnis:

([('-f', 'imports'), ('-c', '1')], []) 
[('-f', 'imports'), ('-c', '1')] 
[] 
imports 
1 

PC B Ergebnis:

([], ['\x96f', 'import_dbs', '\x96c', '1']) 
[] 
['\x96f', 'import_dbs', '\x96c', '1'] 
None 
None 
+0

Was sind "PC A" und "PC B"? –

+0

PC B. zeigt die Unicode Bindestriche anstelle der einfachen Bindestriche von PC A – ShpielMeister

Antwort

1

Das Problem liegt in Ihrem CLI-Befehl, nicht in Ihrem Code. statt Sie haben Striche (oder irgendeine Art von Unicode) Bindestriche

$ python test.py –f import_dbs –c 1 
None 
None 
$ python test.py -f import_dbs –c 1 
import_dbs 
None 
$ python test.py -f import_dbs -c 1 
import_dbs 
1 
$ echo "python test.py –f import_dbs –c 1" | od -c 
0000000 p y t h o n  t e s t . p y  – 
0000020 ** ** f  i m p o r t _ d b s  – 
0000040 ** ** c  1 \n           
0000046 
$ echo "python test.py -f import_dbs -c 1" | od -c 
0000000 p y t h o n  t e s t . p y  - 
0000020 f  i m p o r t _ d b s  - c  
0000040 1 \n               
0000042 

wahrscheinlich durch Ausschneiden und Einfügen oder seltsame Tastaturbelegung verursacht.

+0

Danke! Ich benutze ConEmu Konsole, ich teste mit Windows cmd und es funktioniert! – giovannivl

+0

Gut. Wenn es hilfreich war, kannst du meine Antwort als korrekt markieren. – ShpielMeister

Verwandte Themen