|
20 | 20 | """Argument for CLI."""
|
21 | 21 |
|
22 | 22 | import argparse
|
23 |
| -import os |
24 |
| -import sys |
25 | 23 |
|
26 | 24 | 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 |
30 | 26 | from rubisco.lib.l10n import _
|
31 |
| -from rubisco.lib.pathlib import Path |
32 |
| -from rubisco.lib.variable import format_str, make_pretty |
33 | 27 |
|
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 | +] |
35 | 36 |
|
| 37 | +# Root argument parser. |
36 | 38 | arg_parser = argparse.ArgumentParser(
|
37 | 39 | description="Rubisco CLI",
|
38 | 40 | formatter_class=RUHelpFormatter,
|
39 | 41 | )
|
40 | 42 |
|
41 | 43 | arg_parser.register("action", "version", CLIVersionAction)
|
42 | 44 |
|
| 45 | +# For "rubisco --version". |
43 | 46 | arg_parser.add_argument(
|
44 | 47 | "-v",
|
45 | 48 | "--version",
|
46 | 49 | action="version",
|
47 | 50 | version="",
|
48 | 51 | )
|
49 | 52 |
|
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 | +) |
51 | 71 |
|
| 72 | +# For "rubisco --log=LOGFILE". |
52 | 73 | arg_parser.add_argument(
|
53 | 74 | "--log",
|
54 | 75 | action="store_true",
|
55 | 76 | help=_("Save log to the log file."),
|
56 | 77 | )
|
57 | 78 |
|
| 79 | +# For "rubisco --debug". This argument will be parsed in "lib/log.py". |
58 | 80 | arg_parser.add_argument(
|
59 | 81 | "--debug",
|
60 | 82 | action="store_true",
|
61 | 83 | help=_("Run rubisco in debug mode."),
|
62 | 84 | )
|
63 | 85 |
|
| 86 | +# For "rubisco --usage". |
64 | 87 | arg_parser.add_argument(
|
65 | 88 | "--usage",
|
66 | 89 | action="store_true",
|
67 | 90 | help=_("Show usage."),
|
68 | 91 | )
|
69 | 92 |
|
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( |
79 | 95 | title=_("Available commands"),
|
80 | 96 | dest="command",
|
81 | 97 | metavar="",
|
82 | 98 | required=False,
|
83 | 99 | )
|
84 | 100 |
|
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, |
88 | 106 | )
|
89 | 107 |
|
| 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 | +) |
90 | 114 |
|
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 | +) |
0 commit comments