Skip to content

Commit

Permalink
cli clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
petrnymsa committed Jan 8, 2020
1 parent 6f22524 commit 32bbeb7
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions jdog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
from faker.config import AVAILABLE_LOCALES


def callback_lang_help(ctx, prop, value):
def _lang_help(ctx, prop, value):
if not value:
return
click.echo('Use one of the following language code: ')
click.echo(AVAILABLE_LOCALES)
ctx.exit()


def validate_lang(ctx, prop, value):
def _validate_lang(ctx, prop, value):
if value not in AVAILABLE_LOCALES:
raise click.BadParameter('Language code is invalid. See --lang-help for accepted values')

return value


def print_output(result, pretty, output):
def _print_output(result, pretty, output):
if pretty:
pretty_output = json.dumps(json.loads(result), indent=4)
if output:
Expand All @@ -43,17 +43,18 @@ def print_output(result, pretty, output):
@click.argument('scheme', type=click.File('r'))
@click.option('-p', '--pretty', is_flag=True, default=False, help='Output as pretty JSON.')
@click.option('-s', '--strict', is_flag=True, default=False, help='Raise error when no matching placeholder is found.')
@click.option('-l', '--lang', default='en_US', help='Language to use', callback=validate_lang)
@click.option('--lang-help', is_flag=True, default=False, help='Displays available language codes and exit.', callback=callback_lang_help)
@click.option('-l', '--lang', default='en_US', help='Language to use.', callback=_validate_lang)
@click.option('--lang-help', is_flag=True, default=False, help='Displays available language codes and exit.',
callback=_lang_help)
@click.option('-o', '--output', type=click.File('w'), help='Output file where result is written.')
def run(scheme,strict, pretty, lang, lang_help, output):
def run(scheme, strict, pretty, lang, lang_help, output):
"""Accepts SCHEME and generate new data to stdin or to specified OUTPUT"""
try:
jdog = Jdog(lang, strict)
scheme_text = scheme.read()
jdog.parse_scheme(scheme_text)
result = jdog.generate()
print_output(result, pretty, output)
_print_output(result, pretty, output)
except json.JSONDecodeError as e:
raise click.UsageError(f'Provided SCHEME does not have valid JSON format.\n\tJsonError: {e}')
except NoMatchingPlaceholder as e:
Expand Down

0 comments on commit 32bbeb7

Please sign in to comment.