From e8c1af86062dbef68837d17e2f17f4b3c975c85c Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Thu, 10 Feb 2022 21:15:11 +0100 Subject: use parameters to click.Path instead of validating paths manually --- src/hydrilla/builder/__main__.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/hydrilla/builder/__main__.py b/src/hydrilla/builder/__main__.py index 5b98202..32ac2d9 100644 --- a/src/hydrilla/builder/__main__.py +++ b/src/hydrilla/builder/__main__.py @@ -30,32 +30,22 @@ import click from .build import Build -def validate_dir_path(ctx, param, value): - path = Path(value) - if path.is_dir(): - return path.resolve() - - raise click.BadParameter(f'{param.human_readable_name} must be a directory path') - -def validate_path(ctx, param, value): - return Path(value) +dir_type = click.Path(exists=True, file_okay=False, resolve_path=True) +index_type = click.Path(path_type=Path) @click.command() -@click.option('-s', '--srcdir', default='.', type=click.Path(), - callback=validate_dir_path, +@click.option('-s', '--srcdir', default='.', type=dir_type, help='Source directory to build from.') -@click.option('-i', '--index-json', default='index.json', type=click.Path(), - callback=validate_path, +@click.option('-i', '--index-json', default='index.json', type=index_type, help='Path to file to be processed instead of index.json (if not absolute, resolved relative to srcdir).') -@click.option('-d', '--dstdir', type=click.Path(), required=True, - callback=validate_dir_path, +@click.option('-d', '--dstdir', type=dir_type, required=True, help='Destination directory to write built package files to.') def preform_build(srcdir, index_json, dstdir): """ Build Hydrilla package from scrdir and write the resulting files under dstdir. """ - build = Build(srcdir, index_json) - build.write_package_files(dstdir) + build = Build(Path(srcdir), Path(index_json.decode())) + build.write_package_files(Path(dstdir)) preform_build() -- cgit v1.2.3