From: Rob Lanphier Date: Sun, 20 Nov 2011 02:07:15 +0000 (-0800) Subject: Added optparse options for min/max length, numwords, etc. X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=2808fc557d57f2620ba041c6346dfede1fe340b1;p=redacted-XKCD-password-generator.git Added optparse options for min/max length, numwords, etc. --- diff --git a/xkcd-password.py b/xkcd-password.py index 7536e6b..b4ab5fd 100755 --- a/xkcd-password.py +++ b/xkcd-password.py @@ -31,13 +31,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import random import os import sys +import optparse -def generate_wordlist(wordfile="", min_length=5, max_length=9): +def generate_wordlist(wordfile=None, min_length=5, max_length=9): """ generate a word list from either a kwarg word_file, or a system default """ - if not wordfile: + if wordfile is None: if "darwin" in sys.platform: ## OS X wordfile = "/usr/share/dict/words" @@ -94,10 +95,39 @@ def generate_xkcdpassword(wordlist, n_words=4, interactive=False): if __name__ == '__main__': - custom_wordfile = "~/local/share/dict/common" - - my_wordlist = generate_wordlist(custom_wordfile) - print generate_xkcdpassword(my_wordlist, interactive=True) - + usage = "usage: %prog [options]" + parser = optparse.OptionParser(usage) + + parser.add_option("-w", "--wordfile", dest="wordfile", + default=None, + help="List of valid words for password") + parser.add_option("--min", dest="min_length", + default=5, type="int", + help="Minimum length of words to make password") + parser.add_option("--max", dest="max_length", + default=9, type="int", + help="Maximum length of words to make password") + parser.add_option("-n", "--numwords", dest="numwords", + default=4, type="int", + help="Number of words to make password") + parser.add_option("-i", "--interactive", dest="interactive", + default=False, action="store_true", + help="Interactively select a password") + + (options, args) = parser.parse_args() + + if len(args) > 1: + parser.error("Too many arguments.") + if len(args) == 1: + # supporting either -w or args[0] for wordlist, but not both + if options.wordfile is None: + options.wordfile = args[0] + else: + parser.error("Conflicting values for wordlist: " + args[0] + + " and " + options.wordfile) + my_wordlist = generate_wordlist(wordfile=options.wordfile, + min_length=options.min_length, + max_length=options.max_length) + print generate_xkcdpassword(my_wordlist, interactive=options.interactive)