better cli param validation and output

This commit is contained in:
Manuel Barkhau 2018-12-21 19:19:48 +01:00
parent f980139e82
commit e5e87ced19
2 changed files with 28 additions and 19 deletions

View file

@ -56,6 +56,15 @@ def _init_logging(verbose: int = 0) -> None:
log.debug("Logging initialized.") log.debug("Logging initialized.")
def _validate_release_tag(release: str) -> None:
if release == 'final' or release in parse.VALID_RELEASE_VALUES:
return
log.error(f"Invalid argument --release={release}")
log.error(f"Valid arguments are: final, {', '.join(parse.VALID_RELEASE_VALUES)}")
sys.exit(1)
@click.group() @click.group()
@click.version_option(version="v201812.0010-beta") @click.version_option(version="v201812.0010-beta")
@click.help_option() @click.help_option()
@ -76,16 +85,14 @@ def incr(old_version: str, verbose: int = 0, release: str = None) -> None:
"""Increment a version number for demo purposes.""" """Increment a version number for demo purposes."""
_init_logging(verbose=max(_VERBOSE, verbose)) _init_logging(verbose=max(_VERBOSE, verbose))
if release and release not in parse.VALID_RELEASE_VALUES: if release:
log.error(f"Invalid argument --release={release}") _validate_release_tag(release)
log.error(f"Valid arguments are: {', '.join(parse.VALID_RELEASE_VALUES)}")
sys.exit(1)
new_version = version.incr(old_version, release=release) new_version = version.incr(old_version, release=release)
pep440_version = version.pycalver_to_pep440(new_version) pep440_version = version.pycalver_to_pep440(new_version)
print("PyCalVer Version:", new_version) print("PyCalVer Version:", new_version)
print("PEP440 Version:" , pep440_version) print("PEP440 Version :", pep440_version)
def _update_cfg_from_vcs(cfg: config.Config, fetch: bool) -> config.Config: def _update_cfg_from_vcs(cfg: config.Config, fetch: bool) -> config.Config:
@ -116,7 +123,7 @@ def _update_cfg_from_vcs(cfg: config.Config, fetch: bool) -> config.Config:
@cli.command() @cli.command()
@click.option('-v', '--verbose', count=True, help="Control log level. -vv for debug level.") @click.option('-v', '--verbose', count=True, help="Control log level. -vv for debug level.")
@click.option( @click.option(
'-f/-n', "--fetch/--no-fetch", is_flag=True, default=True, help="Sync tags from remote origin." "-f/-n", "--fetch/--no-fetch", is_flag=True, default=True, help="Sync tags from remote origin."
) )
def show(verbose: int = 0, fetch: bool = True) -> None: def show(verbose: int = 0, fetch: bool = True) -> None:
"""Show current version.""" """Show current version."""
@ -126,7 +133,7 @@ def show(verbose: int = 0, fetch: bool = True) -> None:
cfg: config.MaybeConfig = config.parse(ctx) cfg: config.MaybeConfig = config.parse(ctx)
if cfg is None: if cfg is None:
log.error("Could not parse configuration from setup.cfg") log.error("Could not parse configuration. Perhaps try 'pycalver init'.")
sys.exit(1) sys.exit(1)
cfg = _update_cfg_from_vcs(cfg, fetch=fetch) cfg = _update_cfg_from_vcs(cfg, fetch=fetch)
@ -161,7 +168,6 @@ def init(verbose: int = 0, dry: bool = False) -> None:
def _assert_not_dirty(vcs, filepaths: typ.Set[str], allow_dirty: bool): def _assert_not_dirty(vcs, filepaths: typ.Set[str], allow_dirty: bool):
# TODO (mb 2018-11-11): This is mixing concerns. Move this up into __main__
dirty_files = vcs.status() dirty_files = vcs.status()
if dirty_files: if dirty_files:
@ -210,16 +216,21 @@ def _bump(cfg: config.Config, new_version: str, allow_dirty: bool = False) -> No
@cli.command() @cli.command()
@click.option("-v", "--verbose" , count=True , help="Control log level. -vv for debug level.") @click.option("-v", "--verbose", count=True, help="Control log level. -vv for debug level.")
@click.option('-f/-n', "--fetch/--no-fetch", is_flag=True, default=True, help="Sync tags from remote origin.") @click.option(
"-f/-n", "--fetch/--no-fetch", is_flag=True, default=True, help="Sync tags from remote origin."
)
@click.option( @click.option(
"--dry", default=False, is_flag=True, help="Display diff of changes, don't rewrite files." "--dry", default=False, is_flag=True, help="Display diff of changes, don't rewrite files."
) )
@click.option( @click.option(
"--release", default=None, metavar="<name>", help=( "--release",
default=None,
metavar="<name>",
help=(
f"Override release name of current_version. Valid options are: " f"Override release name of current_version. Valid options are: "
f"{', '.join(parse.VALID_RELEASE_VALUES)} and final." f"{', '.join(parse.VALID_RELEASE_VALUES)} and final."
) ),
) )
@click.option( @click.option(
"--allow-dirty", "--allow-dirty",
@ -233,7 +244,7 @@ def _bump(cfg: config.Config, new_version: str, allow_dirty: bool = False) -> No
) )
def bump( def bump(
release : typ.Optional[str] = None, release : typ.Optional[str] = None,
verbose : int = 0, verbose : int = 0,
dry : bool = False, dry : bool = False,
allow_dirty: bool = False, allow_dirty: bool = False,
fetch : bool = True, fetch : bool = True,
@ -242,16 +253,14 @@ def bump(
verbose = max(_VERBOSE, verbose) verbose = max(_VERBOSE, verbose)
_init_logging(verbose) _init_logging(verbose)
if release and release != 'final' and release not in parse.VALID_RELEASE_VALUES: if release:
log.error(f"Invalid argument --release={release}") _validate_release_tag(release)
log.error(f"Valid arguments are: {', '.join(parse.VALID_RELEASE_VALUES)}")
sys.exit(1)
ctx: config.ProjectContext = config.init_project_ctx(project_path=".") ctx: config.ProjectContext = config.init_project_ctx(project_path=".")
cfg: config.MaybeConfig = config.parse(ctx) cfg: config.MaybeConfig = config.parse(ctx)
if cfg is None: if cfg is None:
log.error("Could not parse configuration from setup.cfg") log.error("Could not parse configuration. Perhaps try 'pycalver init'.")
sys.exit(1) sys.exit(1)
cfg = _update_cfg_from_vcs(cfg, fetch=fetch) cfg = _update_cfg_from_vcs(cfg, fetch=fetch)

View file

@ -216,7 +216,7 @@ def _parse_config(raw_cfg: RawConfig) -> Config:
def parse(ctx: ProjectContext) -> MaybeConfig: def parse(ctx: ProjectContext) -> MaybeConfig:
"""Parse config file if available.""" """Parse config file if available."""
if not ctx.config_filepath.exists(): if not ctx.config_filepath.exists():
log.error(f"File not found: {ctx.config_filepath}") log.warning(f"File not found: {ctx.config_filepath}")
return None return None
try: try: