Skip to content

Commit

Permalink
Merge pull request #13 from henryiii/henryiii/feat/main
Browse files Browse the repository at this point in the history
feat: script improvements
  • Loading branch information
sirosen authored Oct 31, 2024
2 parents dd53ef7 + f01ef35 commit 2a8d14e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 37 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ dependencies = [
[project.scripts]
lint-dependency-groups = "dependency_groups._lint_dependency_groups:main"
pip-install-dependency-groups = "dependency_groups._pip_wrapper:main"
dependency-groups = "dependency_groups.__main__:main"

[project.optional-dependencies]
cli = ["tomli; python_version<'3.11'"]

[project.urls]
source = "https://github.com/sirosen/dependency-groups"
Expand Down
93 changes: 56 additions & 37 deletions src/dependency_groups/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,62 @@
except ImportError:
try:
import tomli as tomllib # type: ignore[no-redef]
except ImportError:
tomllib = None # type: ignore[assignment]
except ModuleNotFoundError:
print(
"Usage error: dependency-groups CLI requires tomli or Python 3.11+",
file=sys.stderr,
)
raise SystemExit(2) from None

if not tomllib:
print(
"Usage error: dependency-groups CLI requires tomli or Python 3.11+",
file=sys.stderr,
)
sys.exit(2)

parser = argparse.ArgumentParser(
description=(
"A dependency-groups CLI. Prints out a resolved group, newline-delimited."
def main() -> None:
parser = argparse.ArgumentParser(
description=(
"A dependency-groups CLI. Prints out a resolved group, newline-delimited."
)
)
parser.add_argument(
"GROUP_NAME", nargs="*", help="The dependency group(s) to resolve."
)
parser.add_argument(
"-f",
"--pyproject-file",
default="pyproject.toml",
help="The pyproject.toml file. Defaults to trying in the current directory.",
)
)
parser.add_argument("GROUP_NAME", help="The dependency group to resolve.")
parser.add_argument(
"-f",
"--pyproject-file",
default="pyproject.toml",
help="The pyproject.toml file. Defaults to trying in the current directory.",
)
parser.add_argument(
"-o",
"--output",
help="An output file. Defaults to stdout.",
)
args = parser.parse_args()

with open(args.pyproject_file, "rb") as fp:
pyproject = tomllib.load(fp)

dependency_groups_raw = pyproject.get("dependency-groups", {})
content = "\n".join(resolve(dependency_groups_raw, args.GROUP_NAME))

if args.output is None or args.output == "-":
print(content)
else:
with open(args.output, "w") as fp:
print(content, file=fp)
parser.add_argument(
"-o",
"--output",
help="An output file. Defaults to stdout.",
)
parser.add_argument(
"-l",
"--list",
action="store_true",
help="List the available dependency groups",
)
args = parser.parse_args()

with open(args.pyproject_file, "rb") as fp:
pyproject = tomllib.load(fp)

dependency_groups_raw = pyproject.get("dependency-groups", {})

if args.list:
print(*dependency_groups_raw.keys())
return
if not args.GROUP_NAME:
print("A GROUP_NAME is required", file=sys.stderr)
raise SystemExit(3)

content = "\n".join(resolve(dependency_groups_raw, *args.GROUP_NAME))

if args.output is None or args.output == "-":
print(content)
else:
with open(args.output, "w") as fp:
print(content, file=fp)


if __name__ == "__main__":
main()

0 comments on commit 2a8d14e

Please sign in to comment.