Repository: unnonouno/jqp

Issue:
Make an option to suppress newline characters
jq has `--join-output / -j` option that surpress newlines.

Discussion:

Patch:
diff --git a/README.rst b/README.rst
index 04a53d0..cdeb66f 100644
--- a/README.rst
+++ b/README.rst
@@ -46,6 +46,7 @@ optional arguments:
   --version   show version and exit
   --import IMPORT  modules to import
   --sort-keys, -S  sort keys in objects when the command print it
+  --join-output, -j  do not show newlines
 
 
 Example
diff --git a/jqp/__init__.py b/jqp/__init__.py
index def3f62..c2f9405 100644
--- a/jqp/__init__.py
+++ b/jqp/__init__.py
@@ -20,7 +20,8 @@ def _exit(error, return_code, message):
     sys.exit(return_code)
 
 
-def run(in_io, out_io, cmd, imports=[], sort_keys=False, raw_output=False):
+def run(in_io, out_io, cmd, imports=[], sort_keys=False, raw_output=False,
+        join_output=False):
     environment = {}
     for mod_name in imports:
         try:
@@ -53,7 +54,8 @@ def run(in_io, out_io, cmd, imports=[], sort_keys=False, raw_output=False):
             except Exception as e:
                 _exit(e, 3, 'Cannot dump result: line %d' % line_no)
 
-        out_io.write('\n')
+        if not join_output:
+            out_io.write('\n')
 
 
 def main():
@@ -71,8 +73,12 @@ def main():
     parser.add_argument(
         '--raw-output', '-r', action='store_true',
         help='when a result is string, the command shows a raw string')
+    parser.add_argument(
+        '--join-output', '-j', action='store_true',
+        help='do not show newlines')
 
     args = parser.parse_args()
 
     run(sys.stdin, sys.stdout, args.cmd, imports=getattr(args, 'import'),
-        sort_keys=args.sort_keys, raw_output=args.raw_output)
+        sort_keys=args.sort_keys, raw_output=args.raw_output,
+        join_output=args.join_output)


Test patch:
diff --git a/tests/run_test.py b/tests/run_test.py
index f2fb837..954a304 100644
--- a/tests/run_test.py
+++ b/tests/run_test.py
@@ -39,6 +39,13 @@ class RunTest(unittest.TestCase):
         jqp.run(inputs, outputs, '"a"', raw_output=True)
         self.assertEqual(outputs.getvalue(), 'a\n')
 
+    def test_join_output(self):
+        inputs = StringIO('''1
+2''')
+        outputs = StringIO()
+        jqp.run(inputs, outputs, 'j', join_output=True)
+        self.assertEqual(outputs.getvalue(), '12')
+
     def test_parse_error(self):
         inputs = StringIO('invalid\n')
         outputs = StringIO()