ngpt 2.9.2__tar.gz → 2.10.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. {ngpt-2.9.2 → ngpt-2.10.0}/PKG-INFO +10 -8
  2. {ngpt-2.9.2 → ngpt-2.10.0}/README.md +9 -7
  3. {ngpt-2.9.2 → ngpt-2.10.0}/docs/examples/cli_components.md +4 -6
  4. {ngpt-2.9.2 → ngpt-2.10.0}/docs/usage/cli_framework.md +35 -0
  5. ngpt-2.10.0/ngpt/cli/args.py +161 -0
  6. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/main.py +31 -121
  7. {ngpt-2.9.2 → ngpt-2.10.0}/pyproject.toml +1 -1
  8. {ngpt-2.9.2 → ngpt-2.10.0}/uv.lock +1 -1
  9. {ngpt-2.9.2 → ngpt-2.10.0}/.github/workflows/python-publish.yml +0 -0
  10. {ngpt-2.9.2 → ngpt-2.10.0}/.gitignore +0 -0
  11. {ngpt-2.9.2 → ngpt-2.10.0}/.python-version +0 -0
  12. {ngpt-2.9.2 → ngpt-2.10.0}/COMMIT_GUIDELINES.md +0 -0
  13. {ngpt-2.9.2 → ngpt-2.10.0}/CONTRIBUTING.md +0 -0
  14. {ngpt-2.9.2 → ngpt-2.10.0}/LICENSE +0 -0
  15. {ngpt-2.9.2 → ngpt-2.10.0}/docs/CONTRIBUTING.md +0 -0
  16. {ngpt-2.9.2 → ngpt-2.10.0}/docs/LICENSE.md +0 -0
  17. {ngpt-2.9.2 → ngpt-2.10.0}/docs/README.md +0 -0
  18. {ngpt-2.9.2 → ngpt-2.10.0}/docs/_config.yml +0 -0
  19. {ngpt-2.9.2 → ngpt-2.10.0}/docs/api/README.md +0 -0
  20. {ngpt-2.9.2 → ngpt-2.10.0}/docs/api/cli.md +0 -0
  21. {ngpt-2.9.2 → ngpt-2.10.0}/docs/api/client.md +0 -0
  22. {ngpt-2.9.2 → ngpt-2.10.0}/docs/api/config.md +0 -0
  23. {ngpt-2.9.2 → ngpt-2.10.0}/docs/assets/css/style.scss +0 -0
  24. {ngpt-2.9.2 → ngpt-2.10.0}/docs/configuration.md +0 -0
  25. {ngpt-2.9.2 → ngpt-2.10.0}/docs/examples/README.md +0 -0
  26. {ngpt-2.9.2 → ngpt-2.10.0}/docs/examples/advanced.md +0 -0
  27. {ngpt-2.9.2 → ngpt-2.10.0}/docs/examples/basic.md +0 -0
  28. {ngpt-2.9.2 → ngpt-2.10.0}/docs/examples/integrations.md +0 -0
  29. {ngpt-2.9.2 → ngpt-2.10.0}/docs/installation.md +0 -0
  30. {ngpt-2.9.2 → ngpt-2.10.0}/docs/overview.md +0 -0
  31. {ngpt-2.9.2 → ngpt-2.10.0}/docs/usage/README.md +0 -0
  32. {ngpt-2.9.2 → ngpt-2.10.0}/docs/usage/cli_config.md +0 -0
  33. {ngpt-2.9.2 → ngpt-2.10.0}/docs/usage/cli_usage.md +0 -0
  34. {ngpt-2.9.2 → ngpt-2.10.0}/docs/usage/library_usage.md +0 -0
  35. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/__init__.py +0 -0
  36. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/__init__.py +0 -0
  37. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/config_manager.py +0 -0
  38. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/formatters.py +0 -0
  39. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/interactive.py +0 -0
  40. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/modes/__init__.py +0 -0
  41. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/modes/chat.py +0 -0
  42. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/modes/code.py +0 -0
  43. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/modes/shell.py +0 -0
  44. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/modes/text.py +0 -0
  45. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/renderers.py +0 -0
  46. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli/ui.py +0 -0
  47. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli.py +0 -0
  48. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/cli_config.py +0 -0
  49. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/client.py +0 -0
  50. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/config.py +0 -0
  51. {ngpt-2.9.2 → ngpt-2.10.0}/ngpt/utils/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ngpt
3
- Version: 2.9.2
3
+ Version: 2.10.0
4
4
  Summary: A lightweight Python CLI and library for interacting with OpenAI-compatible APIs, supporting both official and self-hosted LLM endpoints.
5
5
  Project-URL: Homepage, https://github.com/nazdridoy/ngpt
6
6
  Project-URL: Repository, https://github.com/nazdridoy/ngpt
@@ -135,6 +135,7 @@ For more examples and detailed usage, visit the [CLI Usage Guide](https://nazdri
135
135
  - 🎭 **System Prompts**: Customize model behavior with custom system prompts
136
136
  - 📃 **Conversation Logging**: Save your conversations to text files for later reference
137
137
  - 🧰 **CLI Components**: Reusable components for building custom AI-powered command-line tools
138
+ - 🔌 **Modular Architecture**: Well-structured codebase with clean separation of concerns
138
139
 
139
140
  See the [Feature Overview](https://nazdridoy.github.io/ngpt/overview.html) for more details.
140
141
 
@@ -292,16 +293,14 @@ nGPT can also be used as a framework to build your own AI-powered command-line t
292
293
 
293
294
  ```python
294
295
  from ngpt import NGPTClient, load_config
295
- from ngpt.cli.main import interactive_chat_session
296
+ from ngpt.cli.interactive import interactive_chat_session
296
297
  from ngpt.cli.renderers import prettify_markdown
297
- from ngpt.cli.formatters import ColoredHelpFormatter
298
- import argparse
298
+ from ngpt.cli.args import setup_argument_parser
299
+ import sys
299
300
 
300
301
  # Create a custom CLI tool with colorized help
301
- parser = argparse.ArgumentParser(
302
- description="Specialized Code Assistant",
303
- formatter_class=ColoredHelpFormatter
304
- )
302
+ parser = setup_argument_parser()
303
+ parser.description = "Specialized Code Assistant"
305
304
  parser.add_argument("prompt", nargs="?", help="Code description")
306
305
  parser.add_argument("--language", "-l", default="python", help="Programming language")
307
306
  parser.add_argument("--interactive", "-i", action="store_true", help="Start interactive mode")
@@ -318,6 +317,9 @@ elif args.prompt:
318
317
  # Generate and prettify code
319
318
  code = client.generate_code(args.prompt, language=args.language)
320
319
  print(prettify_markdown(f"```{args.language}\n{code}\n```"))
320
+ else:
321
+ parser.print_help()
322
+ sys.exit(1)
321
323
  ```
322
324
 
323
325
  This allows you to build specialized AI tools like:
@@ -100,6 +100,7 @@ For more examples and detailed usage, visit the [CLI Usage Guide](https://nazdri
100
100
  - 🎭 **System Prompts**: Customize model behavior with custom system prompts
101
101
  - 📃 **Conversation Logging**: Save your conversations to text files for later reference
102
102
  - 🧰 **CLI Components**: Reusable components for building custom AI-powered command-line tools
103
+ - 🔌 **Modular Architecture**: Well-structured codebase with clean separation of concerns
103
104
 
104
105
  See the [Feature Overview](https://nazdridoy.github.io/ngpt/overview.html) for more details.
105
106
 
@@ -257,16 +258,14 @@ nGPT can also be used as a framework to build your own AI-powered command-line t
257
258
 
258
259
  ```python
259
260
  from ngpt import NGPTClient, load_config
260
- from ngpt.cli.main import interactive_chat_session
261
+ from ngpt.cli.interactive import interactive_chat_session
261
262
  from ngpt.cli.renderers import prettify_markdown
262
- from ngpt.cli.formatters import ColoredHelpFormatter
263
- import argparse
263
+ from ngpt.cli.args import setup_argument_parser
264
+ import sys
264
265
 
265
266
  # Create a custom CLI tool with colorized help
266
- parser = argparse.ArgumentParser(
267
- description="Specialized Code Assistant",
268
- formatter_class=ColoredHelpFormatter
269
- )
267
+ parser = setup_argument_parser()
268
+ parser.description = "Specialized Code Assistant"
270
269
  parser.add_argument("prompt", nargs="?", help="Code description")
271
270
  parser.add_argument("--language", "-l", default="python", help="Programming language")
272
271
  parser.add_argument("--interactive", "-i", action="store_true", help="Start interactive mode")
@@ -283,6 +282,9 @@ elif args.prompt:
283
282
  # Generate and prettify code
284
283
  code = client.generate_code(args.prompt, language=args.language)
285
284
  print(prettify_markdown(f"```{args.language}\n{code}\n```"))
285
+ else:
286
+ parser.print_help()
287
+ sys.exit(1)
286
288
  ```
287
289
 
288
290
  This allows you to build specialized AI tools like:
@@ -18,19 +18,17 @@ Here's a simple CLI tool that uses nGPT to generate and explain code:
18
18
 
19
19
  ```python
20
20
  #!/usr/bin/env python3
21
- import argparse
22
21
  import sys
23
22
  from ngpt import NGPTClient, load_config
24
- from ngpt.cli.formatters import ColoredHelpFormatter
23
+ from ngpt.cli.args import setup_argument_parser, validate_args
25
24
  from ngpt.cli.renderers import prettify_markdown, has_markdown_renderer
26
25
 
27
26
  def main():
28
27
  # Create parser with colorized help
29
- parser = argparse.ArgumentParser(
30
- description="Simple code generation tool",
31
- formatter_class=ColoredHelpFormatter
32
- )
28
+ parser = setup_argument_parser()
33
29
 
30
+ # Customize the parser for our specific needs
31
+ parser.description = "Simple code generation tool"
34
32
  parser.add_argument("prompt", help="Code description")
35
33
  parser.add_argument("--language", "-l", default="python", help="Programming language")
36
34
  parser.add_argument("--explain", "-e", action="store_true", help="Include explanation")
@@ -10,6 +10,7 @@ nGPT's CLI module has been modularized into several components that you can inco
10
10
  - **Markdown Rendering**: Beautiful formatting for markdown with syntax highlighting (`ngpt.cli.renderers`)
11
11
  - **Real-time Streaming**: Tools for handling streaming content with live updates (`ngpt.cli.ui`)
12
12
  - **CLI Configuration System**: Robust configuration management (`ngpt.cli.main`)
13
+ - **Argument Parsing**: Sophisticated argument parsing and validation (`ngpt.cli.args`)
13
14
  - **Terminal Utilities**: Helpers for colorized output and terminal formatting (`ngpt.cli.formatters`)
14
15
  - **Mode-specific functionality**: Specialized code, shell, chat and text mode handlers (`ngpt.cli.modes`)
15
16
 
@@ -28,6 +29,40 @@ This will install nGPT with all required dependencies, including:
28
29
 
29
30
  ## Available Components
30
31
 
32
+ ### Argument Parsing
33
+
34
+ The `args` module provides utilities for building colorful, sophisticated command-line interfaces:
35
+
36
+ ```python
37
+ from ngpt.cli.args import setup_argument_parser, validate_args, validate_markdown_renderer
38
+
39
+ # Create and configure the parser
40
+ parser = setup_argument_parser()
41
+ args = parser.parse_args()
42
+
43
+ # Validate arguments for correctness and compatibility
44
+ try:
45
+ args = validate_args(args)
46
+ except ValueError as e:
47
+ print(f"Error: {e}")
48
+ sys.exit(1)
49
+
50
+ # Check if markdown renderer is available
51
+ has_renderer, args = validate_markdown_renderer(args)
52
+ if not has_renderer:
53
+ print("Warning: No markdown renderer available. Using plain text.")
54
+ ```
55
+
56
+ The argument parsing module provides these key functions:
57
+
58
+ - `setup_argument_parser()`: Creates a fully configured argument parser with rich formatting
59
+ - `parse_args()`: Parses command-line arguments
60
+ - `validate_args(args)`: Validates parsed arguments for correctness and compatibility
61
+ - `validate_markdown_renderer(args)`: Checks if markdown rendering is available
62
+ - `handle_cli_config_args(args)`: Processes CLI configuration commands
63
+
64
+ This modular approach makes it easy to create sophisticated CLI tools with consistent behavior.
65
+
31
66
  ### Interactive Chat Session
32
67
 
33
68
  The `interactive_chat_session` function provides a complete interactive chat experience:
@@ -0,0 +1,161 @@
1
+ import argparse
2
+ import sys
3
+ from .. import __version__
4
+ from .formatters import COLORS, ColoredHelpFormatter
5
+ from .renderers import has_markdown_renderer, warn_if_no_markdown_renderer
6
+
7
+ def setup_argument_parser():
8
+ """Set up and return a fully configured argument parser for nGPT CLI."""
9
+ # Colorize description - use a shorter description to avoid line wrapping issues
10
+ description = f"{COLORS['cyan']}{COLORS['bold']}nGPT{COLORS['reset']} - Interact with AI language models via OpenAI-compatible APIs"
11
+
12
+ # Minimalist, clean epilog design
13
+ epilog = f"\n{COLORS['yellow']}nGPT {COLORS['bold']}v{__version__}{COLORS['reset']} • {COLORS['green']}Docs: {COLORS['bold']}https://nazdridoy.github.io/ngpt/usage/cli_usage.html{COLORS['reset']}"
14
+
15
+ parser = argparse.ArgumentParser(description=description, formatter_class=ColoredHelpFormatter, epilog=epilog)
16
+
17
+ # Add custom error method with color
18
+ original_error = parser.error
19
+ def error_with_color(message):
20
+ parser.print_usage(sys.stderr)
21
+ parser.exit(2, f"{COLORS['bold']}{COLORS['yellow']}error: {COLORS['reset']}{message}\n")
22
+ parser.error = error_with_color
23
+
24
+ # Custom version action with color
25
+ class ColoredVersionAction(argparse.Action):
26
+ def __call__(self, parser, namespace, values, option_string=None):
27
+ print(f"{COLORS['green']}{COLORS['bold']}nGPT{COLORS['reset']} version {COLORS['yellow']}{__version__}{COLORS['reset']}")
28
+ parser.exit()
29
+
30
+ # Version flag
31
+ parser.add_argument('-v', '--version', action=ColoredVersionAction, nargs=0, help='Show version information and exit')
32
+
33
+ # Config options
34
+ config_group = parser.add_argument_group('Configuration Options')
35
+ config_group.add_argument('--config', nargs='?', const=True, help='Path to a custom config file or, if no value provided, enter interactive configuration mode to create a new config')
36
+ config_group.add_argument('--config-index', type=int, default=0, help='Index of the configuration to use or edit (default: 0)')
37
+ config_group.add_argument('--provider', help='Provider name to identify the configuration to use')
38
+ config_group.add_argument('--remove', action='store_true', help='Remove the configuration at the specified index (requires --config and --config-index)')
39
+ config_group.add_argument('--show-config', action='store_true', help='Show the current configuration(s) and exit')
40
+ config_group.add_argument('--all', action='store_true', help='Show details for all configurations (requires --show-config)')
41
+ config_group.add_argument('--list-models', action='store_true', help='List all available models for the current configuration and exit')
42
+ config_group.add_argument('--list-renderers', action='store_true', help='Show available markdown renderers for use with --prettify')
43
+
44
+ # Global options
45
+ global_group = parser.add_argument_group('Global Options')
46
+ global_group.add_argument('--api-key', help='API key for the service')
47
+ global_group.add_argument('--base-url', help='Base URL for the API')
48
+ global_group.add_argument('--model', help='Model to use')
49
+ global_group.add_argument('--web-search', action='store_true',
50
+ help='Enable web search capability (Note: Your API endpoint must support this feature)')
51
+ global_group.add_argument('-n', '--no-stream', action='store_true',
52
+ help='Return the whole response without streaming')
53
+ global_group.add_argument('--temperature', type=float, default=0.7,
54
+ help='Set temperature (controls randomness, default: 0.7)')
55
+ global_group.add_argument('--top_p', type=float, default=1.0,
56
+ help='Set top_p (controls diversity, default: 1.0)')
57
+ global_group.add_argument('--max_tokens', type=int,
58
+ help='Set max response length in tokens')
59
+ global_group.add_argument('--log', metavar='FILE',
60
+ help='Set filepath to log conversation to (For interactive modes)')
61
+ global_group.add_argument('--preprompt',
62
+ help='Set custom system prompt to control AI behavior')
63
+ global_group.add_argument('--prettify', action='store_const', const='auto',
64
+ help='Render markdown responses and code with syntax highlighting and formatting')
65
+ global_group.add_argument('--stream-prettify', action='store_true',
66
+ help='Enable streaming with markdown rendering (automatically uses Rich renderer)')
67
+ global_group.add_argument('--renderer', choices=['auto', 'rich', 'glow'], default='auto',
68
+ help='Select which markdown renderer to use with --prettify (auto, rich, or glow)')
69
+
70
+ # Mode flags (mutually exclusive)
71
+ mode_group = parser.add_argument_group('Modes (mutually exclusive)')
72
+ mode_exclusive_group = mode_group.add_mutually_exclusive_group()
73
+ mode_exclusive_group.add_argument('-i', '--interactive', action='store_true', help='Start an interactive chat session')
74
+ mode_exclusive_group.add_argument('-s', '--shell', action='store_true', help='Generate and execute shell commands')
75
+ mode_exclusive_group.add_argument('-c', '--code', action='store_true', help='Generate code')
76
+ mode_exclusive_group.add_argument('-t', '--text', action='store_true', help='Enter multi-line text input (submit with Ctrl+D)')
77
+ # Note: --show-config is handled separately and implicitly acts as a mode
78
+
79
+ # Language option for code mode
80
+ parser.add_argument('--language', default="python", help='Programming language to generate code in (for code mode)')
81
+
82
+ # Prompt argument
83
+ parser.add_argument('prompt', nargs='?', default=None, help='The prompt to send')
84
+
85
+ # Add CLI configuration command
86
+ config_group.add_argument('--cli-config', nargs='*', metavar='COMMAND',
87
+ help='Manage CLI configuration (set, get, unset, list)')
88
+
89
+ return parser
90
+
91
+ def parse_args():
92
+ """Parse command line arguments using the configured parser."""
93
+ parser = setup_argument_parser()
94
+ return parser.parse_args()
95
+
96
+ def validate_args(args):
97
+ """Validate parsed arguments for correctness and compatibility."""
98
+ # Validate --all usage
99
+ if args.all and not args.show_config:
100
+ raise ValueError("--all can only be used with --show-config")
101
+
102
+ # Check if --prettify is used with --stream-prettify (conflict)
103
+ if args.prettify and args.stream_prettify:
104
+ raise ValueError("--prettify and --stream-prettify cannot be used together. Choose one option.")
105
+
106
+ # Check if --stream-prettify is used but Rich is not available
107
+ if args.stream_prettify and not has_markdown_renderer('rich'):
108
+ raise ValueError("--stream-prettify requires Rich to be installed. Install with: pip install \"ngpt[full]\" or pip install rich")
109
+
110
+ return args
111
+
112
+ def validate_markdown_renderer(args):
113
+ """Validate that required markdown renderers are available.
114
+
115
+ Args:
116
+ args: The parsed command line arguments.
117
+
118
+ Returns:
119
+ tuple: (has_renderer, args)
120
+ - has_renderer: Boolean indicating if a renderer is available
121
+ - args: Potentially modified args with prettify disabled if no renderer is available
122
+ """
123
+ has_renderer = True
124
+ if args.prettify:
125
+ has_renderer = warn_if_no_markdown_renderer(args.renderer)
126
+ if not has_renderer:
127
+ # Set a flag to disable prettify since we already warned the user
128
+ print(f"{COLORS['yellow']}Continuing without markdown rendering.{COLORS['reset']}")
129
+ args.prettify = False
130
+
131
+ return has_renderer, args
132
+
133
+ def handle_cli_config_args(args):
134
+ """Process CLI configuration arguments and determine command parameters.
135
+
136
+ Args:
137
+ args: The parsed command line arguments.
138
+
139
+ Returns:
140
+ tuple: (should_handle, action, option, value)
141
+ - should_handle: True if --cli-config was specified and should be handled
142
+ - action: The action to perform (set, get, unset, list, help)
143
+ - option: The option name (or None)
144
+ - value: The option value (or None)
145
+ """
146
+ if args.cli_config is None:
147
+ return (False, None, None, None)
148
+
149
+ # Show help if no arguments or "help" argument
150
+ if len(args.cli_config) == 0 or (len(args.cli_config) > 0 and args.cli_config[0].lower() == "help"):
151
+ return (True, "help", None, None)
152
+
153
+ action = args.cli_config[0].lower()
154
+ option = args.cli_config[1] if len(args.cli_config) > 1 else None
155
+ value = args.cli_config[2] if len(args.cli_config) > 2 else None
156
+
157
+ if action in ("set", "get", "unset", "list", "help"):
158
+ return (True, action, option, value)
159
+ else:
160
+ # Unknown action, show help
161
+ return (True, "help", None, None)
@@ -22,6 +22,7 @@ from .modes.chat import chat_mode
22
22
  from .modes.code import code_mode
23
23
  from .modes.shell import shell_mode
24
24
  from .modes.text import text_mode
25
+ from .args import parse_args, validate_args, handle_cli_config_args, setup_argument_parser, validate_markdown_renderer
25
26
 
26
27
  def show_cli_config_help():
27
28
  """Display help information about CLI configuration."""
@@ -171,109 +172,20 @@ def handle_cli_config(action, option=None, value=None):
171
172
  show_cli_config_help()
172
173
 
173
174
  def main():
174
- # Colorize description - use a shorter description to avoid line wrapping issues
175
- description = f"{COLORS['cyan']}{COLORS['bold']}nGPT{COLORS['reset']} - Interact with AI language models via OpenAI-compatible APIs"
175
+ # Parse command line arguments using args.py
176
+ args = parse_args()
176
177
 
177
- # Minimalist, clean epilog design
178
- epilog = f"\n{COLORS['yellow']}nGPT {COLORS['bold']}v{__version__}{COLORS['reset']} • {COLORS['green']}Docs: {COLORS['bold']}https://nazdridoy.github.io/ngpt/usage/cli_usage.html{COLORS['reset']}"
179
-
180
- parser = argparse.ArgumentParser(description=description, formatter_class=ColoredHelpFormatter, epilog=epilog)
181
-
182
- # Add custom error method with color
183
- original_error = parser.error
184
- def error_with_color(message):
185
- parser.print_usage(sys.stderr)
186
- parser.exit(2, f"{COLORS['bold']}{COLORS['yellow']}error: {COLORS['reset']}{message}\n")
187
- parser.error = error_with_color
188
-
189
- # Custom version action with color
190
- class ColoredVersionAction(argparse.Action):
191
- def __call__(self, parser, namespace, values, option_string=None):
192
- print(f"{COLORS['green']}{COLORS['bold']}nGPT{COLORS['reset']} version {COLORS['yellow']}{__version__}{COLORS['reset']}")
193
- parser.exit()
194
-
195
- # Version flag
196
- parser.add_argument('-v', '--version', action=ColoredVersionAction, nargs=0, help='Show version information and exit')
197
-
198
- # Config options
199
- config_group = parser.add_argument_group('Configuration Options')
200
- config_group.add_argument('--config', nargs='?', const=True, help='Path to a custom config file or, if no value provided, enter interactive configuration mode to create a new config')
201
- config_group.add_argument('--config-index', type=int, default=0, help='Index of the configuration to use or edit (default: 0)')
202
- config_group.add_argument('--provider', help='Provider name to identify the configuration to use')
203
- config_group.add_argument('--remove', action='store_true', help='Remove the configuration at the specified index (requires --config and --config-index)')
204
- config_group.add_argument('--show-config', action='store_true', help='Show the current configuration(s) and exit')
205
- config_group.add_argument('--all', action='store_true', help='Show details for all configurations (requires --show-config)')
206
- config_group.add_argument('--list-models', action='store_true', help='List all available models for the current configuration and exit')
207
- config_group.add_argument('--list-renderers', action='store_true', help='Show available markdown renderers for use with --prettify')
208
-
209
- # Global options
210
- global_group = parser.add_argument_group('Global Options')
211
- global_group.add_argument('--api-key', help='API key for the service')
212
- global_group.add_argument('--base-url', help='Base URL for the API')
213
- global_group.add_argument('--model', help='Model to use')
214
- global_group.add_argument('--web-search', action='store_true',
215
- help='Enable web search capability (Note: Your API endpoint must support this feature)')
216
- global_group.add_argument('-n', '--no-stream', action='store_true',
217
- help='Return the whole response without streaming')
218
- global_group.add_argument('--temperature', type=float, default=0.7,
219
- help='Set temperature (controls randomness, default: 0.7)')
220
- global_group.add_argument('--top_p', type=float, default=1.0,
221
- help='Set top_p (controls diversity, default: 1.0)')
222
- global_group.add_argument('--max_tokens', type=int,
223
- help='Set max response length in tokens')
224
- global_group.add_argument('--log', metavar='FILE',
225
- help='Set filepath to log conversation to (For interactive modes)')
226
- global_group.add_argument('--preprompt',
227
- help='Set custom system prompt to control AI behavior')
228
- global_group.add_argument('--prettify', action='store_const', const='auto',
229
- help='Render markdown responses and code with syntax highlighting and formatting')
230
- global_group.add_argument('--stream-prettify', action='store_true',
231
- help='Enable streaming with markdown rendering (automatically uses Rich renderer)')
232
- global_group.add_argument('--renderer', choices=['auto', 'rich', 'glow'], default='auto',
233
- help='Select which markdown renderer to use with --prettify (auto, rich, or glow)')
234
-
235
- # Mode flags (mutually exclusive)
236
- mode_group = parser.add_argument_group('Modes (mutually exclusive)')
237
- mode_exclusive_group = mode_group.add_mutually_exclusive_group()
238
- mode_exclusive_group.add_argument('-i', '--interactive', action='store_true', help='Start an interactive chat session')
239
- mode_exclusive_group.add_argument('-s', '--shell', action='store_true', help='Generate and execute shell commands')
240
- mode_exclusive_group.add_argument('-c', '--code', action='store_true', help='Generate code')
241
- mode_exclusive_group.add_argument('-t', '--text', action='store_true', help='Enter multi-line text input (submit with Ctrl+D)')
242
- # Note: --show-config is handled separately and implicitly acts as a mode
243
-
244
- # Language option for code mode
245
- parser.add_argument('--language', default="python", help='Programming language to generate code in (for code mode)')
246
-
247
- # Prompt argument
248
- parser.add_argument('prompt', nargs='?', default=None, help='The prompt to send')
249
-
250
- # Add CLI configuration command
251
- config_group.add_argument('--cli-config', nargs='*', metavar='COMMAND',
252
- help='Manage CLI configuration (set, get, unset, list)')
253
-
254
- args = parser.parse_args()
178
+ try:
179
+ args = validate_args(args)
180
+ except ValueError as e:
181
+ print(f"{COLORS['bold']}{COLORS['yellow']}error: {COLORS['reset']}{str(e)}\n")
182
+ sys.exit(2)
255
183
 
256
184
  # Handle CLI configuration command
257
- if args.cli_config is not None:
258
- # Show help if no arguments or "help" argument
259
- if len(args.cli_config) == 0 or (len(args.cli_config) > 0 and args.cli_config[0].lower() == "help"):
260
- show_cli_config_help()
261
- return
262
-
263
- action = args.cli_config[0].lower()
264
- option = args.cli_config[1] if len(args.cli_config) > 1 else None
265
- value = args.cli_config[2] if len(args.cli_config) > 2 else None
266
-
267
- if action in ("set", "get", "unset", "list", "help"):
268
- handle_cli_config(action, option, value)
269
- return
270
- else:
271
- show_cli_config_help()
272
- return
273
-
274
- # Validate --all usage
275
- if args.all and not args.show_config:
276
- parser.error("--all can only be used with --show-config")
185
+ should_handle_cli_config, action, option, value = handle_cli_config_args(args)
186
+ if should_handle_cli_config:
187
+ handle_cli_config(action, option, value)
188
+ return
277
189
 
278
190
  # Handle --renderers flag to show available markdown renderers
279
191
  if args.list_renderers:
@@ -293,15 +205,23 @@ def main():
293
205
  effective_config_index = args.config_index
294
206
 
295
207
  # Only apply CLI config for provider/config-index if not explicitly set on command line
296
- if not effective_provider and 'provider' in cli_config and '--provider' not in sys.argv:
208
+ # If --config-index is explicitly provided, we should ignore provider from CLI config
209
+ config_index_from_cli = '--config-index' in sys.argv
210
+ provider_from_cli = '--provider' in sys.argv
211
+
212
+ if not provider_from_cli and 'provider' in cli_config and not config_index_from_cli:
297
213
  effective_provider = cli_config['provider']
298
214
 
299
- if effective_config_index == 0 and 'config-index' in cli_config and '--config-index' not in sys.argv:
215
+ if not config_index_from_cli and 'config-index' in cli_config and not provider_from_cli:
300
216
  effective_config_index = cli_config['config-index']
301
217
 
302
218
  # Check for mutual exclusivity between provider and config-index
303
219
  if effective_config_index != 0 and effective_provider:
304
- parser.error("--config-index and --provider cannot be used together")
220
+ from_cli_config = not provider_from_cli and 'provider' in cli_config
221
+ provider_source = "CLI config file (ngpt-cli.conf)" if from_cli_config else "command-line arguments"
222
+ error_msg = f"--config-index and --provider cannot be used together. Provider from {provider_source}."
223
+ print(f"{COLORS['bold']}{COLORS['yellow']}error: {COLORS['reset']}{error_msg}\n")
224
+ sys.exit(2)
305
225
 
306
226
  # Handle interactive configuration mode
307
227
  if args.config is True: # --config was used without a value
@@ -311,7 +231,8 @@ def main():
311
231
  if args.remove:
312
232
  # Validate that config_index is explicitly provided
313
233
  if '--config-index' not in sys.argv and not effective_provider:
314
- parser.error("--remove requires explicitly specifying --config-index or --provider")
234
+ print(f"{COLORS['bold']}{COLORS['yellow']}error: {COLORS['reset']}--remove requires explicitly specifying --config-index or --provider\n")
235
+ sys.exit(2)
315
236
 
316
237
  # Show config details before asking for confirmation
317
238
  configs = load_configs(str(config_path))
@@ -489,6 +410,8 @@ def main():
489
410
 
490
411
  # For interactive mode, we'll allow continuing without a specific prompt
491
412
  if not args.prompt and not (args.shell or args.code or args.text or args.interactive or args.show_config or args.list_models):
413
+ # Simply use the parser's help
414
+ parser = setup_argument_parser()
492
415
  parser.print_help()
493
416
  return
494
417
 
@@ -498,23 +421,10 @@ def main():
498
421
 
499
422
  # Check if --prettify is used but no markdown renderer is available
500
423
  # This will warn the user immediately if they request prettify but don't have the tools
501
- has_renderer = True
502
- if args.prettify:
503
- has_renderer = warn_if_no_markdown_renderer(args.renderer)
504
- if not has_renderer:
505
- # Set a flag to disable prettify since we already warned the user
506
- print(f"{COLORS['yellow']}Continuing without markdown rendering.{COLORS['reset']}")
507
- show_available_renderers()
508
- args.prettify = False
509
-
510
- # Check if --prettify is used with --stream-prettify (conflict)
511
- if args.prettify and args.stream_prettify:
512
- parser.error("--prettify and --stream-prettify cannot be used together. Choose one option.")
513
-
514
- # Check if --stream-prettify is used but Rich is not available
515
- if args.stream_prettify and not has_markdown_renderer('rich'):
516
- parser.error("--stream-prettify requires Rich to be installed. Install with: pip install \"ngpt[full]\" or pip install rich")
517
-
424
+ has_renderer, args = validate_markdown_renderer(args)
425
+ if not has_renderer:
426
+ show_available_renderers()
427
+
518
428
  # Initialize client using the potentially overridden active_config
519
429
  client = NGPTClient(**active_config)
520
430
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "ngpt"
3
- version = "2.9.2"
3
+ version = "2.10.0"
4
4
  description = "A lightweight Python CLI and library for interacting with OpenAI-compatible APIs, supporting both official and self-hosted LLM endpoints."
5
5
  authors = [
6
6
  {name = "nazDridoy", email = "nazdridoy399@gmail.com"},
@@ -134,7 +134,7 @@ wheels = [
134
134
 
135
135
  [[package]]
136
136
  name = "ngpt"
137
- version = "2.9.2"
137
+ version = "2.10.0"
138
138
  source = { editable = "." }
139
139
  dependencies = [
140
140
  { name = "prompt-toolkit" },
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes