python getopt詳解及簡單實例
python getopt詳解
函數(shù)原型:
getopt.getopt(args, shortopts, longopts=[])
參數(shù)解釋:
- args:args為需要解析的參數(shù)列表。一般使用sys.argv[1:],這樣可以過濾掉第一個參數(shù)(ps:第一個參數(shù)是腳本的名稱,它不應該作為參數(shù)進行解析)
- shortopts:簡寫參數(shù)列表
- longopts:長參數(shù)列表
返回值:
- opts:分析出的(option, value)列表對。
- args:不屬于格式信息的剩余命令行參數(shù)列表。
源碼分析
在Android生成OTA的build系統(tǒng)中,common.py文件中的ParseOptions函數(shù)就是用來解析輸入?yún)?shù)的,我們來通過該函數(shù)的實現(xiàn)來分析一下getopt的使用。
函數(shù)源碼如下:
def ParseOptions(argv, docstring, extra_opts="", extra_long_opts=(), extra_option_handler=None): try: opts, args = getopt.getopt( argv, "hvp:s:x" + extra_opts, ["help", "verbose", "path=", "signapk_path=", "extra_signapk_args=", "java_path=", "public_key_suffix=", "private_key_suffix=", "device_specific=", "extra="] + list(extra_long_opts)) except getopt.GetoptError, err: Usage(docstring) print "**", str(err), "**" sys.exit(2) path_specified = False for o, a in opts: if o in ("-h", "--help"): Usage(docstring) sys.exit() elif o in ("-v", "--verbose"): OPTIONS.verbose = True elif o in ("-p", "--path"): OPTIONS.search_path = a elif o in ("--signapk_path",): OPTIONS.signapk_path = a elif o in ("--extra_singapk_args",): OPTIONS.extra_signapk_args = shlex.split(a) elif o in ("--java_path",): OPTIONS.java_path = a else: if extra_option_handler is None or not extra_option_handler(o, a): assert False, "unknown option \"%s\"" % (o,) os.environ["PATH"] = (os.path.join(OPTIONS.search_path, "bin") + os.pathsep + os.environ["PATH"]) return args
其中,extra_option_handler可以理解為函數(shù)指針,它的功能也是解析opts的鍵值對。
extra_option_handler源碼如下:
def option_handler(o, a): if o in ("-b", "--board_config"): pass # deprecated elif o in ("-k", "--package_key"): OPTIONS.package_key = a elif o in ("-i", "--incremental_from"): OPTIONS.incremental_source = a elif o in ("-w", "--wipe_user_data"): OPTIONS.wipe_user_data = True elif o in ("-n", "--no_prereq"): OPTIONS.omit_prereq = True elif o in ("-e", "--extra_script"): OPTIONS.extra_script = a elif o in ("-a", "--aslr_mode"): if a in ("on", "On", "true", "True", "yes", "Yes"): OPTIONS.aslr_mode = True else: OPTIONS.aslr_mode = False elif o in ("--worker_threads"): OPTIONS.worker_threads = int(a) else: return False return True
一般生成OAT全量包的參數(shù)argv如下:
argv = ['-v', '-p', 'out/host/linux-xxx', '-k', 'build/target/product/security/testkey', 'out/target/product/xxx/obj/PACKAGING/target_files_intermediates/xxx-target_files.zip', 'out/target/product/xxx/xxx_20150723.1340-ota.zip']
首先,對參數(shù)進行分析,其中短參數(shù)包括:
-v,-p,-k,
經(jīng)過解析后,生成的結果如下所示:
opts = [('-v', ''), ('-p', 'out/host/linux-x86'), ('-k', 'build/target/product/security/testkey')] args =['out/target/product/xxx/obj/PACKAGING/target_files_intermediates/xxx-target_files.zip', 'out/target/product/xxx/xxx_20150723.1340-ota.zip']
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
pandas中的DataFrame按指定順序輸出所有列的方法
下面小編就為大家分享一篇pandas中的DataFrame按指定順序輸出所有列的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04python enumerate內(nèi)置函數(shù)用法總結
這篇文章主要介紹了python enumerate內(nèi)置函數(shù)用法總結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01Python中l(wèi)ogging日志記錄到文件及自動分割的操作代碼
這篇文章主要介紹了Python中l(wèi)ogging日志記錄到文件及自動分割,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08使用Python3+PyQT5+Pyserial 實現(xiàn)簡單的串口工具方法
今天小編就為大家分享一篇使用Python3+PyQT5+Pyserial 實現(xiàn)簡單的串口工具方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02