Execute functions apart from argparse which are defined in main in Python -
i'm using argparse use user arguments run functions in program running fine. i'm unable run other generic functions i'm calling in main(). skips functions without running , showing output. how do or doing wrong ?
let's in below program want functions mytop20 , listapps run using user arguments runs fine if remove boilerplate main() function objective run_in_main() function should run in main()
import argparse def my_top20_func(): print "called my_top20_func" def my_listapps_func(): print "called my_listapps_func" def run_in_main(): print "called main" parser = argparse.argumentparser() function_map = {'top20' : my_top20_func, 'listapps' : my_listapps_func } parser.add_argument('command', choices=function_map.keys()) args = parser.parse_args() func = function_map[args.command] func() if __name__ == "__main__": run_in_main()
as use case quite similar have taken above code here.
often parse_args
put in main
section runs when file used script, not when imported. in mind, i'd reorganize script as:
def main(args): func = function_map[args.command] func() if __name__ == '__main__': args = parser.parse_args() main(args)
wiki
Comments
Post a Comment