| 
 | 
 
还是上代码吧: 
- import sys
 
 - #====================
 
 - def main(argv):
 
 -     #debug:
 
 -     print('Len(argv)=' + str(len(argv)) + '; argv[0]=' + argv[0])
 
 -     if len(argv) < 3:
 
 -         if len(argv) == 1 :
 
 -             print("Lack arguments. Not run. Notice: Place the data-file in /home/cuibe/work/replace/, and invoke it explicitly.")
 
 -             return -1
 
 -         elif len(argv) == 2 and (argv[1] == '--help' or argv[1] == '-h'):
 
 -             print("\nReplacer.py [-t|--exec|-h|--help] datafile \n-h --help   get the help notice.\n--exec   do real copy\n-t   do not real copy,test and verbos it only. ")
 
 -             return -1
 
 -         elif len(argv) == 2:
 
 -             print("Bad or lack option. Please reference help to give correct one.")
 
 -             return -1
 
 -     else:
 
 -         if argv[1] == '--exec':
 
 -             notTest = True
 
 -         elif argv[1] == '-t':
 
 -             notTest = False
 
 -         else:
 
 -             print("You provides wrong option.Try again.")
 
 -             return -1
 
 -     try:
 
 -         file = str(argv[2])
 
 -         filePath = "./" + file
 
  
-         define_outfile = False
 
 -         if len(argv) > 3 :
 
 -             outfile = './' + str(argv[3])  # argv[3] must have if len > 3.
 
 -             define_outfile = True
 
 -         #debug:
 
 -         print(filePath, outfile)  # may raise exception : reference before assignment 
 
 -         print(argv)
 
 -     except Exception, err:
 
 -         print('Make path error: ' + str(Exception) + str(err))
 
  
-     result = -1
 
 -     try:
 
 -         if define_outfile == True:
 
 -             result = dosomething(, notTest, outfile)
 
 -         else:
 
 -             result = printAttr(entities, notTest)
 
 -     except Exception, err:
 
 -         print('Changing error: ' + str(Exception) + str(err))
 
 -         traceback.print_exc()
 
 -     
 
 -     print("------------------------------")
 
 -     if result == 0:
 
 -         print("Done.")
 
 -         return 0
 
 -     else:
 
 -         print("Proceed with error, check logs.")
 
 -         return -1
 
  
 
- if __name__ == '__main__' : main(sys.argv)
 
 
  复制代码 
另一种代码: 
- #====================
 
 - def main(argv):
 
 -         #debug:
 
 -         #print('Len(argv)=' + str(len(argv)) + '; argv[0]=' + argv[0], end='')
 
 -         #end
 
 -         arg_dict = dict()
 
 -         arg_dict["k_words"] = []
 
 -         length = len(argv)
 
  
-         if len(argv) >= 2 and argv[1][0] != '-':
 
 -                 try:
 
 -                         arg_dict['k_num'] = int(argv[1])
 
 -                         length -= 1
 
 -                         #debug:
 
 -                         #print("; argv[1]=" + argv[1])
 
 -                         #print('length=', length, end='')
 
 -                         #end
 
 -                         
 
 -                 except Exception as err:
 
 -                         # asset  Todo:  Write to log file.
 
 -                         #print("\nConver argv[1] to int error:", str(Exception), str(err))
 
 -                         # end
 
 -                         pass
 
 -         for arg in argv[len(argv)-length+1:]:  # minus first num arg if has. Plus arg[0].
 
 -         # Switch option
 
 -                 
 
 -                 if (arg == '-H' or arg == '--Hash256'):
 
 -                         # arg = '-H'  # Flush --Hash256
 
 -                         
 
 -                         if (len(arg_dict["k_words"]) > 0) and not in_all_words("-H", "--Hash256", arg_dict):
 
 -                                 print("\n-H can not occur after words. Quit.\n")
 
 -                                 return -1
 
 -                         if "k_H" in arg_dict:  # And arg_dict['k_H']==Ture;
 
 -                                 arg_dict["k_words"].append(arg)
 
 -                         else:
 
 -                                 arg_dict["k_H"] = True
 
 -                         length -= 1
 
 -                         continue
 
 -                 elif (arg == '-R'):
 
 -                         if len(arg_dict["k_words"]) > 0 and not all_words("-R",arg_dict):
 
 -                                 print("\n-R can not occur after words. Quit.\n")
 
 -                                 return -1
 
 -                         if "k_R" in arg_dict:  # And arg_dict['k_R']==Ture;
 
 -                                 arg_dict["k_words"].append(arg)
 
 -                         else:
 
 -                                 arg_dict['k_R'] = True
 
 -                         length -= 1
 
 -                         continue
 
 -                 elif (arg == '--help' or arg == '-h'):
 
 -                         arg_dict["k_help"] = True
 
 -                         def validate_help():
 
 -                                 if len(argv) > 2:  # Notice here is not length.
 
 -                                         print("\nMiss using the help: --help (-h) do not occur with other arguments.\n")
 
 -                                         return -1
 
 -                                 else:
 
 -                                         return 0
 
 -                         if validate_help() == 0:
 
 -                                 break  # Jump out of loop.
 
 -                         else:
 
 -                                 return -1
 
 -                 else:
 
 -                         # "(-M) or (R)"
 
 -                         arg_dict['k_words'].append(arg)
 
 -                         length -= 1
 
  
 
 
-         if length > 1:  # argv[0] is programme itself. So length of left is larger than 1 (not 0).
 
 -                 for word in argv[-(length-1):]:  # Reason -1 ref up.
 
 -                         arg_dict['k_words'].append(word)  # validate len(arg_dict['k_words']) to make sure it has value.
 
 -         if "k_help" in arg_dict:
 
 -                 print("\nTo generate a more complex passwd. \
 
 -                                 \ngenpasswd.py ([num] [-R] [-H|--Hash256] [words])|[-h|--help] \
 
 -                                 \nnum  passwd length \
 
 -                                 \nwords  words that you want to include in, seperate by blank space. \
 
 -                                 \n-R shuffle the charactors. \
 
 -                                 \n-H --Hash256 progress hash layer. \
 
 -                                 \n-h --help   get the help notice. ")
 
 -                 return 0
 
  
-         if len(argv) == 1 :  # Run main prog without arguments.
 
 -                 # print("Using default arguments. Notice: You can input length of passwd or other arg, see --help.")
 
 -                 genPasswd(20)
 
 -                 return 0
 
 -         #debug
 
 -         print("\narg_dict:", arg_dict, "len(argv):", len(argv), "length:", length)
 
  
-         if 'k_words' in arg_dict:
 
 -                 addWords(arg_dict['k_words'])
 
 -         if 'k_help' in arg_dict:
 
 -                 print("arg_dict['k_help']", arg_dict['k_help'])  # Has return , not proceed this line.
 
 -         if 'k_R' in arg_dict:
 
 -                 print("arg_dict['k_R']", arg_dict['k_R'])
 
 -         if 'k_H' in arg_dict:
 
 -                 print("arg_dict['k_H']", arg_dict['k_H'])
 
 -         #end
 
 -         if 'k_num' in arg_dict:
 
 -                 print("argv[1]", argv[1])
 
 -                 #print("arg_dict['k_num']", arg_dict['k_num'])
 
 -                 genPasswd(arg_dict['k_num'])
 
 -         else:
 
 -                 genPasswd()
 
 -         return 0
 
 -         # try:
 
 -         #         file = str(argv[2])
 
 -         #         filePath = "/" + file
 
 -                 
 
 -         #         #debug:
 
 -         #         print(filePath)
 
 -         #         print(argv)
 
 -         # except Exception, err:
 
 -         #         print('Make path error: ' + str(Exception) + str(err))
 
 -                 
 
 -         # entities = []
 
 -         # try:
 
 -         #         entities = readEntries(filePath)
 
 -         # except Exception, err:
 
 -         #         print('Reading file error: ' + str(Exception) + str(err))
 
 -         # result = -1
 
 -         # if len(entities) > 0:
 
 -         #         try:
 
 -         #                 result = replaceThem(entities, notTest)
 
 -         #         except Exception, err:
 
 -         #                 print('Replacing error: ' + str(Exception) + str(err))
 
 -         #                 traceback.print_exc()
 
 -         # else:
 
 -         #         print("No file to copy, check your source-data file.")
 
 -         # print("------------------------------")
 
 -         # if result == 0:
 
 -         #         print("Done.")
 
 -         #         return 0
 
 -         # else:
 
 -         #         print("Proceed with error, check logs.")
 
 -         #         return -1
 
  
- if __name__ == '__main__' : main(sys.argv)
 
 
  复制代码 |   
 
 
 
 |