Add argument to specify password line ending: --separator
authorFrançois Freitag <redacted>
Sat, 2 Dec 2017 21:12:49 +0000 (13:12 -0800)
committerFrançois Freitag <redacted>
Sat, 20 Jan 2018 21:51:34 +0000 (13:51 -0800)
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
```

tests/test_xkcdpass.py
xkcdpass/xkcd_password.py

index c21e6602cf7479c5e7c9a5b3bebd2f72a5239ccf..9ae1f7cc8c27cdd228fcd49b4cb1f70e66faac3d 100644 (file)
@@ -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)
index f30cbd30abf94fe41e021d0aa7bca7e034432983..4bf2dc7e7e626ef8e26355f59808a6e30bf4b543 100755 (executable)
@@ -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,
git clone https://git.99rst.org/PROJECT