返回提交历史
Modified
g4f/cli/__init__.py
+22
-20
XFEstudio/gpt4free
Add debug mode to CLI and improve argument parsing error handling
9f9d18cc
代码差异
1 个文件
+22
-20
@@ -17,7 +17,6 @@ import os
17
17
import sys
18
18
from argparse import ArgumentParser
19
19
20
# Local imports (within g4f package)
21
20
from .client import get_parser, run_client_args
22
21
from ..requests import BrowserConfig
23
22
from ..gui.run import gui_parser, run_gui_args
@@ -25,7 +24,7 @@ from ..config import DEFAULT_PORT, DEFAULT_TIMEOUT, DEFAULT_STREAM_TIMEOUT
25
24
from ..Provider.needs_auth.Antigravity import cli_main as antigravity_cli_main
26
25
from ..Provider.qwen.QwenCode import cli_main as qwen_cli_main
27
26
from ..Provider.github.GithubCopilot import cli_main as github_cli_main
28
from g4f.Provider.needs_auth.GeminiCLI import cli_main as gemini_cli_main
27
from ..Provider.needs_auth.GeminiCLI import cli_main as gemini_cli_main
29
28
from .. import Provider
30
29
from .. import cookies
31
30
@@ -301,26 +300,33 @@ def main():
301
300
302
301
303
302
mode_parser = ArgumentParser(description="Select mode to run g4f in.", exit_on_error=False)
304
mode_parser.add_argument("mode", nargs="?", choices=["api", "gui", "client", "mcp", "auth"], default="api", help="Mode to run g4f in (default: api).")
303
mode_parser.add_argument("mode", nargs="?", choices=["api", "gui", "client", "mcp", "auth", "debug"], default="api", help="Mode to run g4f in (default: api).")
305
304
306
305
# Preserve original remaining so the API parser gets all args if mode
307
306
# detection fails (e.g. `python -m g4f --port 8080` without a mode prefix).
308
307
original_remaining = remaining
309
308
try:
310
args, remaining = mode_parser.parse_known_args(remaining)
311
except (argparse.ArgumentError, SystemExit):
312
# If mode parsing fails (e.g. a port number or unknown flag appears
313
# before the mode positional), fall back to API mode and restore the
314
# original argument list so the API parser can handle them.
315
args = argparse.Namespace(mode="api")
316
remaining = original_remaining
317
try:
309
try:
310
args, remaining = mode_parser.parse_known_args(remaining)
311
except argparse.ArgumentError:
312
parser = get_api_parser(exit_on_error=False)
313
args = parser.parse_args(remaining)
314
run_api_args(args)
315
return
318
316
if args.mode == "auth":
319
317
parser = get_auth_parser()
320
318
args, remaining = parser.parse_known_args(remaining)
321
319
print(f"Handling auth for provider: {args.provider}, action: {args.action}")
322
320
handle_auth(args.provider, args.action, remaining)
323
321
return
322
elif args.mode == "debug":
323
parser = get_api_parser()
324
args = parser.parse_args(remaining)
325
args.debug = True
326
args.reload = True
327
if args.port is None:
328
args.port = 8080
329
run_api_args(args)
324
330
elif args.mode == "api":
325
331
parser = get_api_parser()
326
332
args = parser.parse_args(remaining)
@@ -345,16 +351,12 @@ def main():
345
351
)
346
352
347
353
except argparse.ArgumentError:
348
# Fallback chain:
349
# 1. Try client mode
354
# Try client mode
350
355
try:
351
run_client_args(
352
get_parser(exit_on_error=False).parse_args(),
353
exit_on_error=False
354
)
355
except argparse.ArgumentError:
356
# 2. Try API mode with default arguments
357
run_api_args(get_api_parser().parse_args())
356
run_client_args(
357
get_parser(exit_on_error=False).parse_args(),
358
exit_on_error=False
359
)
358
360
359
361
def generate_autocomplete():
360
362
# Top-level commands and their subcommands/options