From 3912dbcbc02787f41dea69d932f1dbdb4bd3365e Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Wed, 28 Aug 2019 22:29:45 +0200 Subject: Add --human-readable to pmatch --- patternutils/commands/pmatch.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/patternutils/commands/pmatch.py b/patternutils/commands/pmatch.py index 5f473e8..23ef6fc 100644 --- a/patternutils/commands/pmatch.py +++ b/patternutils/commands/pmatch.py @@ -50,6 +50,13 @@ def apply_regex( return matches, has_invalid_matches +def json_dumps(data: Any, human_readable: bool = False) -> str: + if human_readable: + return json.dumps(data, indent=4, sort_keys=True) + else: + return json.dumps(data) + + @click.command(context_settings={'help_option_names': ('-h', '--help', '-?')}) @click.option('-d', '--directory', multiple=True, default=['./']) @click.option('-m', '--match-directories', is_flag=True, default=False, @@ -64,6 +71,8 @@ def apply_regex( help='use predefined regex pattern') @click.option('--stream', is_flag=True, default=False, help='Output a stream of objects instead a list of objects') +@click.option('--human-readable', is_flag=True, default=False, + help='output the JSON in a human readable format') @click.option('-v', '--verbose', is_flag=True, default=False, help='explain what is being done') @click.version_option() @@ -77,6 +86,7 @@ def pmatch( match_full_path: bool, use_predefined_pattern: bool, stream: bool, + human_readable: bool, verbose: bool, ) -> None: @@ -109,9 +119,9 @@ def pmatch( if stream: for m in matches: - click.echo(json.dumps(m)) + click.echo(json_dumps(m, human_readable)) else: - click.echo(json.dumps(matches)) + click.echo(json_dumps(matches, human_readable)) if __name__ == '__main__': -- cgit v1.3.1