Browse Source

added -n and -N to allow numbers as the separators

Pat Beirne 8 years ago
parent
commit
743a3aad4e
1 changed files with 13 additions and 4 deletions
  1. 13 4
      pwgen.py

+ 13 - 4
pwgen.py

@@ -12,6 +12,7 @@ targetLen = 20		# default pw len
 useConjunction = False
 number = 1
 punc = " !@#$%^&*()-_=+[{]}\|;:/?.>,<`~"
+number_punc = "0123456789"
 
 def toInt(s, chkRange, errStr = "sorry, that's not a number", rangeStr = "sorry, I can't do that number"):
 	try:
@@ -27,12 +28,14 @@ def toInt(s, chkRange, errStr = "sorry, that's not a number", rangeStr = "sorry,
 narg = 1
 if len(sys.argv)>1:
 	if sys.argv[1]=="-h" or sys.argv[1]=="--help":
-		print "pwgen.py  v1.2  generate passphrase; inspired by xkcd/936 and SteveGibson\n"
-		print "Usage: pwgen.py [-p | -P] [length [num_phrases]] "
+		print "pwgen.py  v1.3  generate passphrase; inspired by xkcd/936 and SteveGibson\n"
+		print "Usage: pwgen.py [-p | -P | -n ] [length [num_phrases]] "
 		print "       pwgen.py -c [num_phrases]"
 		print "       pwgen.py -h | --help \n"
 		print "     -p           pad with only spaces (for smartphone)"
 		print "     -P           pad with spaces, period and comma (for smartphone)"
+		print "     -n           pad with numbers"
+		print "     -N           pad with numbers, spaces, period, comma like -P -n"
 		print "     length       make pass phrases padded with punctuation filler; default=20"
 		print "     -c           make 2 word pass phrases with a conjuction filler"
 		print "     num_phrases  make multiple pass phrases, one per line; default=1\n"
@@ -40,6 +43,12 @@ if len(sys.argv)>1:
 	elif sys.argv[1]=="-c":	# conjunction mode
 		useConjunction = True
 		narg = narg + 1
+	elif sys.argv[1]=='-n': # number fill mode
+		punc = number_punc
+		narg = narg + 1
+	elif sys.argv[1]=='-N':
+		punc = number_punc + " ,."
+		narg = narg + 1
 	elif sys.argv[1]=="-p": # use only space
 		punc = " "
 		narg = narg + 1
@@ -79,7 +88,7 @@ for i in range(number):
 
 	if useConjunction == False:
 		if whole + 6 >= targetLen:	# if 2 words is enough
-			r = targetLen - whole
+			r = max(1,targetLen - whole)
 			pw = w[0] + thisPunct*r + w[1]
 		else:		# otherwise use 3
 			whole = whole + len(w[2])
@@ -93,7 +102,7 @@ for i in range(number):
 			if conj[-2:]==" a":
 				conj = conj+'n'
 		pw = w[0] + ' ' + conj + ' ' + w[1]
-		
+
 	print pw