From: François Freitag Date: Sat, 2 Dec 2017 21:12:49 +0000 (-0800) Subject: Add argument to specify password line ending: --separator X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=d88031fea236e598ba36365cd18467b72af3b4cf;p=redacted-XKCD-password-generator.git Add argument to specify password line ending: --separator Adds a new separator argument used to separate passwords in the output. This is useful when piping the output of xkcdpass to another command, e.g. copying a password to the clipboard on Linux with: ``` xkcdpass --count 1 --numwords 10 --separator "" | xsel -b ``` --- diff --git a/tests/test_xkcdpass.py b/tests/test_xkcdpass.py index c21e660..9ae1f7c 100644 --- a/tests/test_xkcdpass.py +++ b/tests/test_xkcdpass.py @@ -47,6 +47,24 @@ class XkcdPasswordTests(unittest.TestCase): delimiter=tdelim) self.assertIsNotNone(re.match('([a-z]+(_|$))+', result)) + def test_separator(self): + count = 3 + result = subprocess.check_output( + ["python", "xkcdpass/xkcd_password.py", + "--count", str(count), + "--delimiter", "|", + "--separator", " "]) + self.assertEqual(result.count(b" "), 3) + + def test_separator_no_end(self): + "Pipe output to other program. e.g. `xkcdpass -c 1 -s "" | xsel -b`" + count = 1 + result = subprocess.check_output( + ["python", "xkcdpass/xkcd_password.py", + "--count", str(count), + "--separator", ""]) + self.assertEqual(result.find(b"\n"), -1) + if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(XkcdPasswordTests) diff --git a/xkcdpass/xkcd_password.py b/xkcdpass/xkcd_password.py index f30cbd3..4bf2dc7 100755 --- a/xkcdpass/xkcd_password.py +++ b/xkcdpass/xkcd_password.py @@ -1,6 +1,8 @@ #!/usr/bin/env python # encoding: utf-8 +from __future__ import print_function + import argparse import math import os @@ -291,12 +293,15 @@ def emit_passwords(wordlist, options): """ Generate the specified number of passwords and output them. """ count = options.count while count > 0: - print(generate_xkcdpassword( - wordlist, - interactive=options.interactive, - numwords=options.numwords, - acrostic=options.acrostic, - delimiter=options.delimiter)) + print( + generate_xkcdpassword( + wordlist, + interactive=options.interactive, + numwords=options.numwords, + acrostic=options.acrostic, + delimiter=options.delimiter + ), + end=options.separator) count -= 1 @@ -358,6 +363,10 @@ class XkcdPassArgumentParser(argparse.ArgumentParser): "-d", "--delimiter", dest="delimiter", default=" ", metavar="DELIM", help="Separate words within a passphrase with DELIM.") + self.add_argument( + "-s", "--separator", + dest="separator", default="\n", metavar="SEP", + help="Separate generated passphrases with SEP.") self.add_argument( "--allow-weak-rng", action="store_true", dest="allow_weak_rng", default=False,