这是我要寻找的解决方案,它调用Windows
GetCommandLineArgvW函数:
在Windows下从ActiveState获取带有Unipre字符的sys.argv
但是我进行了一些更改,以简化其用法并更好地处理某些用法。这是我用的:
win32_unipre_argv.py
"""win32_unipre_argv.pyimporting this will replace sys.argv with a full Unipre form.Windows only.From this site, with adaptations: http://pre.activestate.com/recipes/572200/Usage: simply import this module into a script. sys.argv is changed tobe a list of Unipre strings."""import sysdef win32_unipre_argv(): """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unipre strings. Versions 2.x of Python don't support Unipre in sys.argv on Windows, with the underlying Windows API instead replacing multi-byte characters with '?'. """ from ctypes import POINTER, byref, cdll, c_int, windll from ctypes.wintypes import LPCWSTR, LPWSTR GetCommandLineW = cdll.kernel32.GetCommandLineW GetCommandLineW.argtypes = [] GetCommandLineW.restype = LPCWSTR CommandLineToArgvW = windll.shell32.CommandLineToArgvW CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)] CommandLineToArgvW.restype = POINTER(LPWSTR) cmd = GetCommandLineW() argc = c_int(0) argv = CommandLineToArgvW(cmd, byref(argc)) if argc.value > 0: # Remove Python executable and commands if present start = argc.value - len(sys.argv) return [argv[i] for i in xrange(start, argc.value)]sys.argv = win32_unipre_argv()
现在,我使用它的方法就是:
import sysimport win32_unipre_argv
从那时起,
sys.argv是Unipre字符串列表。Python
optparse模块似乎很高兴解析它,这很棒。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)