fromcli.exportimportapi_to_dictdef_build_lookup_table():all_keywords={}importcopydefbuild_invert_index(d,usage='jina'):forkind['methods']:usg=f'{usage}{k["name"]}'if'methods'ink:build_invert_index(k,usage=usg)ifk['name']notinall_keywords:all_keywords[k['name']]=[]_k={'name':k['name'],'type':'command','usage':usg,'help':k['help']}all_keywords[k['name']].append(_k)if'options'ink:forkkink['options']:ifkk['name']notinall_keywords:all_keywords[kk['name']]=[]_kk=copy.deepcopy(kk)_kk['usage']=usgall_keywords[kk['name']].append(_kk)defbuild_noisy_index(d):noise2key={}fork,zind.items():forvinz:noises=[k]noises.append(v.get('name',[]))noises.extend(v.get('option_strings',[]))dash_to_space=[k.replace('-',' ').replace('_',' ')forkinnoises]no_dash=[k.replace('-','').replace('_','')forkinnoises]no_leading_dash=[k.replace('--','')forkinnoises]noises.extend(dash_to_space)noises.extend(no_dash)noises.extend(no_leading_dash)no_ending_plural=[k[:-1]ifk.endswith('s')elsekforkinnoises]noises.extend(no_ending_plural)forkkinset(noises):noise2key[kk]=kreturnnoise2keybuild_invert_index(api_to_dict(show_all_args=True))nkw2kw=build_noisy_index(all_keywords)returnnkw2kw,all_keywordsdef_prettyprint_help(d,also_in=None):fromjina.helperimportcoloredifd['type']=='command':print(f'''{colored(d['name'],attrs='bold')} is a CLI command of Jina.{colored(d['help'],attrs='bold')} More info: {d['usage']} --help ''')else:availables=' '.join(colored(v,attrs='underline')forvin(set(h['usage']forhinalso_in)ifalso_inelse{d['usage']}))option_str=' '.join(colored(v,attrs='bold')forvind['option_strings'])ifoption_str:option_str=f'as {option_str}'table={}table['Type']=d['type']table['Required']=d['required']ifd['choices']:table['Choices']=' | '.join(d['choices'])ifnotd['default_random']andd['default']isnotNone:table['Default']=d['default']ifd['default_random']:table['Remark']=colored('This argument has a random default value!','on yellow')table_str='\n '.join(f'{k+": "+colored(v,attrs="bold")}'fork,vintable.items())lb='\033[F'importargparseprint(f'''{colored(d['name'],attrs='bold')} is {colored('an internal CLI of Jina, should not be used directly',color='yellow')ifd['help']==argparse.SUPPRESSelse'a CLI argument of Jina.'}. It is available in {availables}{option_str}{colored(d['help'],attrs='bold')ifd['help']!=argparse.SUPPRESSelselb*2}{table_str} ''')
[docs]deflookup_and_print(query:str):"""Lookup argument name in Jina API and prettyprint the result. :param query: the argument (fuzzy) name """nkw2kw,kw2info=_build_lookup_table()ifquerynotinnkw2kw:fromjina.helperimportcoloredprint(f'Can not find argument {colored(query,attrs="bold")}, 'f'maybe it\'s a misspelling or Jina does not have this argument.')else:helps=kw2info[nkw2kw[query]]# type: listiflen(helps)==1:_prettyprint_help(helps[0])eliflen(helps)>1andlen(set(h['help']forhinhelps))==1:_prettyprint_help(helps[0],also_in=helps)eliflen(helps)>1:fromcollectionsimportdefaultdictfromjina.helperimportcoloredhelp_group=defaultdict(list)forhinhelps:help_group[h['help']].append(h)print(colored(f'Found {len(help_group)} mentions in Jina API.',attrs='dark'))forhginhelp_group.values():_prettyprint_help(hg[0],also_in=hg)print(colored('─'*40,attrs='dark'))