Skip to content

Commit

Permalink
Add --path CLI argument
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Nov 9, 2024
1 parent 99f2b4e commit d0653de
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
9 changes: 9 additions & 0 deletions newversion/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
from collections.abc import Sequence
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional, Union

from newversion.constants import PACKAGE_NAME, Commands, VersionParts
Expand Down Expand Up @@ -58,6 +59,7 @@ class CLINamespace:
log_level: int
package: bool
save: bool
path: Path


class EnumListAction(argparse.Action):
Expand Down Expand Up @@ -156,6 +158,12 @@ def parse_args(args: Sequence[str]) -> CLINamespace:
" Supports pyproject.toml, setup.cfg and setup.py."
),
)
parser.add_argument(
"--path",
type=Path,
default=Path.cwd(),
help="Path to Python package root. Default: current working directory.",
)
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose logging")
parser.add_argument("-q", "--quiet", action="store_true", help="No logging")

Expand Down Expand Up @@ -307,4 +315,5 @@ def parse_args(args: Sequence[str]) -> CLINamespace:
log_level=log_level,
package=result.package,
save=result.save,
path=result.path,
)
6 changes: 4 additions & 2 deletions newversion/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class Executor:
def __init__(
self,
version: Optional[Version] = None,
path: Optional[Path] = None,
) -> None:
self.path = path or Path.cwd()
self.version = version if version is not None else Version.zero()

def command_get(
Expand Down Expand Up @@ -219,7 +221,7 @@ def command_get_version(self) -> Version:
ExecutorError: If there is an error retrieving the package version.
"""
try:
return PackageVersion(Path.cwd()).get()
return PackageVersion(self.path).get()
except PackageVersionError as e:
raise ExecutorError(e) from None

Expand All @@ -235,6 +237,6 @@ def command_set_version(self, version: Version) -> None:
ExecutorError: If there is an error setting the package version.
"""
try:
PackageVersion(Path.cwd()).set(version)
PackageVersion(self.path).set(version)
except PackageVersionError as e:
raise ExecutorError(e) from None
5 changes: 4 additions & 1 deletion newversion/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def main_api(config: CLINamespace) -> str:
"""
Run main API entrypoint.
"""
executor = Executor(config.version)
executor = Executor(
version=config.version,
path=config.path,
)
if config.package:
executor.version = executor.command_get_version()
try:
Expand Down

0 comments on commit d0653de

Please sign in to comment.