Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conquer the land of clicketing snakes #13

Merged
merged 17 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

setup(
name="rich-click",
entry_points={"console_scripts": ["rich-click = rich_click.cli:main"]},
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
install_requires=[
"click",
"rich",
"importlib-metadata; python_version < '3.8'",
],
)
6 changes: 2 additions & 4 deletions src/rich_click/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
__version__ = "1.1.0.dev0"

from click import *
from click import group as click_group
from click import command as click_command
from .rich_click import RichGroup
from .rich_click import RichCommand

Expand All @@ -18,8 +20,6 @@ def group(*args, cls=RichGroup, **kwargs):

Defines the group() function so that it uses the RichGroup class by default
"""
from click import group as click_group

return click_group(*args, cls=cls, **kwargs)


Expand All @@ -28,6 +28,4 @@ def command(*args, cls=RichCommand, **kwargs):

Defines the command() function so that it uses the RichCommand class by default
"""
from click import command as click_command

return click_command(*args, cls=cls, **kwargs)
15 changes: 15 additions & 0 deletions src/rich_click/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
Entry-point module, in case you use `python -m griffe`.
pawamoy marked this conversation as resolved.
Show resolved Hide resolved

Why does this file exist, and why `__main__`? For more info, read:

- https://www.python.org/dev/peps/pep-0338/
- https://docs.python.org/3/using/cmdline.html#cmdoption-m
"""

import sys

from rich_click.cli import main

if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
46 changes: 46 additions & 0 deletions src/rich_click/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""The command line interface."""

import sys
from importlib import import_module
try:
from importlib.metadata import entry_points
except ImportError:
from importlib_metadata import entry_points

import click
from rich_click import group, command


def main(args=None):
args = args or sys.argv[1:]
if not args:
# without args we assume we want to run rich-click on itself
# TODO: rewrite using argparse
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
script_name = "rich-click"
else:
script_name = args[0]
scripts = {script.name: script for script in entry_points().get("console_scripts")}
if script_name in scripts:
# a valid script was passed
script = scripts[script_name]
module_path = script.module
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
function_name = script.attr
prog = script_name
elif ":" in script_name:
# the path to a function was passed
module_path, function_name = args[0].split(":")
prog = module_path.split(".", 1)[0]
else:
print("usage: rich-click [SCRIPT | MODULE:FUNCTION] [-- SCRIPT_ARGS...]", file=sys.stderr)
sys.exit(1)
if len(args) > 1:
if args[1] == "--":
del args[1]
# patch click before importing the program function
click.group = group
click.command = command
# import the program function
module = import_module(module_path)
function = getattr(module, function_name)
# simply run it: it should be patched as well
return function(args[1:])