国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python getopt詳解及簡(jiǎn)單實(shí)例

2020-02-23 04:11:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

 python getopt詳解

函數(shù)原型:

getopt.getopt(args, shortopts, longopts=[])

參數(shù)解釋:

args:args為需要解析的參數(shù)列表。一般使用sys.argv[1:],這樣可以過濾掉第一個(gè)參數(shù)(ps:第一個(gè)參數(shù)是腳本的名稱,它不應(yīng)該作為參數(shù)進(jìn)行解析) shortopts:簡(jiǎn)寫參數(shù)列表 longopts:長(zhǎng)參數(shù)列表

返回值:

opts:分析出的(option, value)列表對(duì)。 args:不屬于格式信息的剩余命令行參數(shù)列表。

源碼分析

在Android生成OTA的build系統(tǒng)中,common.py文件中的ParseOptions函數(shù)就是用來(lái)解析輸入?yún)?shù)的,我們來(lái)通過該函數(shù)的實(shí)現(xiàn)來(lái)分析一下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的鍵值對(duì)。

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']            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 上蔡县| 马关县| 贵定县| 庆城县| 灵台县| 仁布县| 灯塔市| 石楼县| 疏勒县| 丰都县| 渝北区| 开远市| 麻栗坡县| 浠水县| 恩施市| 汉源县| 光山县| 嘉禾县| 宜章县| 宁陵县| 津南区| 山东省| 措勤县| 定结县| 岳普湖县| 枝江市| 张家界市| 永济市| 曲阜市| 抚顺县| 苍溪县| 闽清县| SHOW| 大渡口区| 德州市| 聂荣县| 清徐县| 成武县| 涟水县| 凤冈县| 合阳县|