
from __future__ import print_function
import argparse
import json

parser = argparse.ArgumentParser(description='Pretty-print LDJSON.')
parser.add_argument('--indent', metavar='N', type=int, default=2,
                    dest='indent', help='indentation for pretty-printing')
parser.add_argument('--file', metavar='FILE', required=True, dest='file',
                    type=argparse.FileType('r'), help='input LDJSON file')
parser.add_argument('--sort', action='store_true', dest='sortkeys',
                    help='sort object keys')

args = parser.parse_args()

for line in args.file:
    record = json.loads(line)
    print(json.dumps(record, indent=args.indent, sort_keys=args.sortkeys))


from __future__ import print_function
import click
import json
from collections import OrderedDict


def json_loader(sortkeys):
    def _loader(line):
        if sortkeys:
            return json.loads(line)
        else:
            # if --no-sortkeys, let's preserve file order
            return json.JSONDecoder(object_pairs_hook=OrderedDict).decode(line)

    return _loader


@click.command()
@click.option('indent', '-i', '--indent', default=2,
              help='indentation for pretty-printing')
@click.option('--sortkeys/--no-sortkeys', default=False,
              help='sort object keys')
@click.argument('infile', type=click.File())
def cli(indent, sortkeys, infile):
    """Pretty-print LDJSON."""
    loader = json_loader(sortkeys)
    for line in infile:
        record = loader(line)
        print(json.dumps(record, indent=indent, sort_keys=sortkeys))

if __name__ == '__main__':
    cli()

Use click instead of argparse
Use click instead of argparse

  
  from __future__ import print_function
- import argparse
+ import click
  import json
+ from collections import OrderedDict
  
- parser = argparse.ArgumentParser(description='Pretty-print LDJSON.')
- parser.add_argument('--indent', metavar='N', type=int, default=2,
-                     dest='indent', help='indentation for pretty-printing')
- parser.add_argument('--file', metavar='FILE', required=True, dest='file',
-                     type=argparse.FileType('r'), help='input LDJSON file')
- parser.add_argument('--sort', action='store_true', dest='sortkeys',
-                     help='sort object keys')
  
- args = parser.parse_args()
+ def json_loader(sortkeys):
+     def _loader(line):
+         if sortkeys:
+             return json.loads(line)
+         else:
+             # if --no-sortkeys, let's preserve file order
+             return json.JSONDecoder(object_pairs_hook=OrderedDict).decode(line)
  
+     return _loader
- for line in args.file:
-     record = json.loads(line)
-     print(json.dumps(record, indent=args.indent, sort_keys=args.sortkeys))
  
+ 
+ @click.command()
+ @click.option('indent', '-i', '--indent', default=2,
+               help='indentation for pretty-printing')
+ @click.option('--sortkeys/--no-sortkeys', default=False,
+               help='sort object keys')
+ @click.argument('infile', type=click.File())
+ def cli(indent, sortkeys, infile):
+     """Pretty-print LDJSON."""
+     loader = json_loader(sortkeys)
+     for line in infile:
+         record = loader(line)
+         print(json.dumps(record, indent=indent, sort_keys=sortkeys))
+ 
+ if __name__ == '__main__':
+     cli()
+ 
Use click instead of argparse
## Code Before:

from __future__ import print_function
import argparse
import json

parser = argparse.ArgumentParser(description='Pretty-print LDJSON.')
parser.add_argument('--indent', metavar='N', type=int, default=2,
                    dest='indent', help='indentation for pretty-printing')
parser.add_argument('--file', metavar='FILE', required=True, dest='file',
                    type=argparse.FileType('r'), help='input LDJSON file')
parser.add_argument('--sort', action='store_true', dest='sortkeys',
                    help='sort object keys')

args = parser.parse_args()

for line in args.file:
    record = json.loads(line)
    print(json.dumps(record, indent=args.indent, sort_keys=args.sortkeys))

## Instruction:
Use click instead of argparse
## Code After:

from __future__ import print_function
import click
import json
from collections import OrderedDict


def json_loader(sortkeys):
    def _loader(line):
        if sortkeys:
            return json.loads(line)
        else:
            # if --no-sortkeys, let's preserve file order
            return json.JSONDecoder(object_pairs_hook=OrderedDict).decode(line)

    return _loader


@click.command()
@click.option('indent', '-i', '--indent', default=2,
              help='indentation for pretty-printing')
@click.option('--sortkeys/--no-sortkeys', default=False,
              help='sort object keys')
@click.argument('infile', type=click.File())
def cli(indent, sortkeys, infile):
    """Pretty-print LDJSON."""
    loader = json_loader(sortkeys)
    for line in infile:
        record = loader(line)
        print(json.dumps(record, indent=indent, sort_keys=sortkeys))

if __name__ == '__main__':
    cli()
