Skip to content

Commit

Permalink
Implement join-output option
Browse files Browse the repository at this point in the history
  • Loading branch information
unnonouno committed Oct 19, 2016
1 parent 2cb79f8 commit 48cbc0f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions jqp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand All @@ -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)
7 changes: 7 additions & 0 deletions tests/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def test_raw_output(self):
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()
Expand Down

0 comments on commit 48cbc0f

Please sign in to comment.