Skip to content

Commit 26d7a1f

Browse files
committed
Partial implement rubisco ext, but hook is not working.
1 parent 4d721e4 commit 26d7a1f

29 files changed

+1110
-320
lines changed

rubisco/cli/main/arg_parser.py

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,111 +20,106 @@
2020
"""Argument for CLI."""
2121

2222
import argparse
23-
import os
24-
import sys
2523

2624
from rubisco.cli.main.help_formatter import RUHelpFormatter
27-
from rubisco.cli.main.version_action import CLIVersionAction, show_version
28-
from rubisco.cli.output import output_step
29-
from rubisco.lib.exceptions import RUError
25+
from rubisco.cli.main.version_action import CLIVersionAction
3026
from rubisco.lib.l10n import _
31-
from rubisco.lib.pathlib import Path
32-
from rubisco.lib.variable import format_str, make_pretty
3327

34-
__all__ = ["arg_parser", "early_arg_parse", "hook_commands"]
28+
__all__ = [
29+
"arg_parser",
30+
"commands_parser",
31+
"extman_command_parser",
32+
"extman_parser",
33+
"hook_arg_parser",
34+
"hook_command_parser",
35+
]
3536

37+
# Root argument parser.
3638
arg_parser = argparse.ArgumentParser(
3739
description="Rubisco CLI",
3840
formatter_class=RUHelpFormatter,
3941
)
4042

4143
arg_parser.register("action", "version", CLIVersionAction)
4244

45+
# For "rubisco --version".
4346
arg_parser.add_argument(
4447
"-v",
4548
"--version",
4649
action="version",
4750
version="",
4851
)
4952

50-
arg_parser.add_argument("--root", type=str, help=_("Project root directory."))
53+
# For "rubisco --used-colors=COLORS"
54+
arg_parser.add_argument(
55+
"--used-prompt-colors",
56+
type=set,
57+
help=_("Prompt colors used by rubisco parent process."),
58+
action="store",
59+
default=set(),
60+
dest="used_prompt_colors",
61+
)
62+
63+
# For "rubisco --root=DIRECTORY".
64+
arg_parser.add_argument(
65+
"--root",
66+
type=str,
67+
help=_("Project root directory."),
68+
action="store",
69+
dest="root_directory",
70+
)
5171

72+
# For "rubisco --log=LOGFILE".
5273
arg_parser.add_argument(
5374
"--log",
5475
action="store_true",
5576
help=_("Save log to the log file."),
5677
)
5778

79+
# For "rubisco --debug". This argument will be parsed in "lib/log.py".
5880
arg_parser.add_argument(
5981
"--debug",
6082
action="store_true",
6183
help=_("Run rubisco in debug mode."),
6284
)
6385

86+
# For "rubisco --usage".
6487
arg_parser.add_argument(
6588
"--usage",
6689
action="store_true",
6790
help=_("Show usage."),
6891
)
6992

70-
arg_parser.add_argument(
71-
"command",
72-
action="store",
73-
nargs="?",
74-
help=_("Command to run."),
75-
default=["info"],
76-
)
77-
78-
hook_commands = arg_parser.add_subparsers(
93+
# Rubsisco built-in command line parser.
94+
commands_parser = arg_parser.add_subparsers(
7995
title=_("Available commands"),
8096
dest="command",
8197
metavar="",
8298
required=False,
8399
)
84100

85-
hook_commands.add_parser(
86-
"info",
87-
help=_("Show project information."),
101+
# Rubisco extension manager command line parser.
102+
extman_parser = commands_parser.add_parser(
103+
"ext",
104+
help=_("Manage extensions."),
105+
formatter_class=RUHelpFormatter,
88106
)
89107

108+
# Extension manager command line parser.
109+
extman_command_parser = extman_parser.add_subparsers(
110+
dest="command",
111+
required=True,
112+
help=_("Extension commands."),
113+
)
90114

91-
def early_arg_parse() -> None:
92-
"""Parse arguments without argparse.
93-
94-
Some arguments will be added to argparse later (like hooks).
95-
If we use argparse here, they will inoperative.
96-
"""
97-
if "-h" in sys.argv or "--help" in sys.argv:
98-
arg_parser.print_help()
99-
sys.exit(0)
100-
if "-v" in sys.argv or "--version" in sys.argv:
101-
show_version()
102-
sys.exit(0)
103-
if "--usage" in sys.argv:
104-
arg_parser.print_usage()
105-
sys.exit(0)
106-
for idx, arg in enumerate(sys.argv):
107-
if arg.startswith("--root"):
108-
if "=" in arg:
109-
root = arg.split("=")[1].strip()
110-
else:
111-
if idx + 1 >= len(sys.argv):
112-
arg_parser.print_usage()
113-
raise RUError(
114-
_("Missing argument for '--root' option."),
115-
)
116-
root = sys.argv[idx + 1].strip()
117-
if root.startswith("-"):
118-
arg_parser.print_usage()
119-
raise RUError(
120-
_("Missing argument for '--root' option."),
121-
)
122-
if root:
123-
root = Path(root).absolute().normpath()
124-
output_step(
125-
format_str(
126-
_("Entering directory '${{path}}'"),
127-
fmt={"path": make_pretty(str(root))},
128-
),
129-
)
130-
os.chdir(root)
115+
# Hook argument parser.
116+
hook_arg_parser = argparse.ArgumentParser(
117+
description="Rubisco CLI Hooks",
118+
formatter_class=RUHelpFormatter,
119+
)
120+
121+
hook_command_parser = hook_arg_parser.add_subparsers(
122+
dest="command",
123+
required=True,
124+
help=_("Hook commands."),
125+
)

rubisco/cli/main/builtin_cmds.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- mode: python -*-
2+
# vi: set ft=python :
3+
4+
# Copyright (C) 2024 The C++ Plus Project.
5+
# This file is part of the Rubisco.
6+
#
7+
# Rubisco is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published
9+
# by the Free Software Foundation, either version 3 of the License,
10+
# or (at your option) any later version.
11+
#
12+
# Rubisco is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
"""C++ Plus Rubisco built-in command lines."""
21+
22+
from __future__ import annotations
23+
24+
from rubisco.cli.main.arg_parser import arg_parser, commands_parser
25+
from rubisco.cli.main.help_formatter import RUHelpFormatter
26+
from rubisco.cli.main.project_config import get_project_config
27+
from rubisco.cli.main.version_action import show_version
28+
from rubisco.lib.l10n import _
29+
from rubisco.shared.ktrigger import IKernelTrigger, call_ktrigger
30+
31+
__all__ = ["register_builtin_cmds"]
32+
33+
def show_project_info(_: object) -> None:
34+
"""For 'rubisco info' command."""
35+
call_ktrigger(
36+
IKernelTrigger.on_show_project_info,
37+
project=get_project_config(),
38+
)
39+
40+
def register_builtin_cmds() -> None:
41+
"""Register built-in commands."""
42+
# If user don't provide any arguments. We should show the project info.
43+
arg_parser.set_defaults(func=show_project_info)
44+
45+
# For "rubisco info" command.
46+
commands_parser.add_parser(
47+
"info",
48+
help=_("Show project information."),
49+
formatter_class=RUHelpFormatter,
50+
).set_defaults(func=show_project_info)
51+
52+
# For "rubisco version" command.
53+
commands_parser.add_parser(
54+
"version",
55+
help=_("Show Rubisco version."),
56+
formatter_class=RUHelpFormatter,
57+
).set_defaults(func=lambda _: show_version())

0 commit comments

Comments
 (0)