| 1 |
"""Complex Demo Application - Comprehensive edge case testing for documentation generation. |
| 2 |
|
| 3 |
This application tests all known edge cases for cyclopts documentation plugins: |
| 4 |
- Dataclass parameter flattening with @Parameter(name="*") |
| 5 |
- Pydantic model support |
| 6 |
- attrs class support |
| 7 |
- 3+ level nested command hierarchies |
| 8 |
- Complex union types (int | Literal[...]) |
| 9 |
- Custom groups with Group.create_ordered() |
| 10 |
- meta.default pattern for global options |
| 11 |
- Count parameters, parse=False, allow_leading_hyphen |
| 12 |
- Validators |
| 13 |
- Hidden commands/parameters |
| 14 |
- Multiple docstring formats |
| 15 |
- Enums and Flags |
| 16 |
""" |
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
import sys |
| 21 |
from dataclasses import dataclass, field |
| 22 |
from enum import Enum, Flag, auto |
| 23 |
from pathlib import Path |
| 24 |
from typing import Annotated, Literal |
| 25 |
|
| 26 |
import cyclopts |
| 27 |
from cyclopts import App, Group, Parameter, validators |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class LogLevel(Enum): |
| 35 |
"""Log level enumeration.""" |
| 36 |
|
| 37 |
DEBUG = "debug" |
| 38 |
INFO = "info" |
| 39 |
WARNING = "warning" |
| 40 |
ERROR = "error" |
| 41 |
CRITICAL = "critical" |
| 42 |
|
| 43 |
|
| 44 |
class OutputFormat(Enum): |
| 45 |
"""Output format options.""" |
| 46 |
|
| 47 |
JSON = "json" |
| 48 |
YAML = "yaml" |
| 49 |
TABLE = "table" |
| 50 |
CSV = "csv" |
| 51 |
|
| 52 |
|
| 53 |
class Permission(Flag): |
| 54 |
"""Permission flags for access control.""" |
| 55 |
|
| 56 |
NONE = 0 |
| 57 |
READ = auto() |
| 58 |
WRITE = auto() |
| 59 |
EXECUTE = auto() |
| 60 |
ADMIN = READ | WRITE | EXECUTE |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@cyclopts.Parameter(name="*") |
| 69 |
@dataclass(frozen=True) |
| 70 |
class DatabaseConfig: |
| 71 |
"""Database connection configuration. |
| 72 |
|
| 73 |
Parameters |
| 74 |
---------- |
| 75 |
host |
| 76 |
Database server hostname. |
| 77 |
port |
| 78 |
Database server port number. |
| 79 |
username |
| 80 |
Authentication username. |
| 81 |
password |
| 82 |
Authentication password (optional). |
| 83 |
ssl_mode |
| 84 |
SSL connection mode. |
| 85 |
pool_size |
| 86 |
Connection pool size. |
| 87 |
""" |
| 88 |
|
| 89 |
host: str = "localhost" |
| 90 |
port: int = 5432 |
| 91 |
username: str = "admin" |
| 92 |
password: str | None = None |
| 93 |
ssl_mode: Literal["disable", "prefer", "require", "verify-full"] = "prefer" |
| 94 |
pool_size: Annotated[int, Parameter(validator=validators.Number(gte=1, lte=100))] = 10 |
| 95 |
|
| 96 |
|
| 97 |
@cyclopts.Parameter(name="*") |
| 98 |
@dataclass |
| 99 |
class ProcessingConfig: |
| 100 |
"""Data processing configuration. |
| 101 |
|
| 102 |
Parameters |
| 103 |
---------- |
| 104 |
batch_size |
| 105 |
Number of items to process per batch. |
| 106 |
num_workers |
| 107 |
Number of parallel workers. Use "auto" for automatic detection. |
| 108 |
quality_level |
| 109 |
Processing quality level. Higher values mean better quality but slower. |
| 110 |
device |
| 111 |
Computing device to use. Can be "cuda", "cpu", "auto", or a GPU index. |
| 112 |
output_formats |
| 113 |
List of output formats to generate. |
| 114 |
""" |
| 115 |
|
| 116 |
batch_size: Annotated[int, Parameter(validator=validators.Number(gt=0))] = 32 |
| 117 |
num_workers: int | Literal["auto"] = "auto" |
| 118 |
quality_level: int | Literal["high", "medium", "low"] = "high" |
| 119 |
device: Literal["cuda", "cpu", "auto"] | int = "auto" |
| 120 |
output_formats: list[OutputFormat] = field(default_factory=lambda: [OutputFormat.JSON]) |
| 121 |
|
| 122 |
|
| 123 |
@cyclopts.Parameter(name="*") |
| 124 |
@dataclass(frozen=True) |
| 125 |
class PathConfig: |
| 126 |
"""Path configuration for input/output directories. |
| 127 |
|
| 128 |
Parameters |
| 129 |
---------- |
| 130 |
input_dir |
| 131 |
Input data directory. |
| 132 |
output_dir |
| 133 |
Output results directory. |
| 134 |
cache_dir |
| 135 |
Cache directory for intermediate files. |
| 136 |
log_dir |
| 137 |
Directory for log files. |
| 138 |
""" |
| 139 |
|
| 140 |
input_dir: Path = Path("data/input") |
| 141 |
output_dir: Path = Path("data/output") |
| 142 |
cache_dir: Path | None = None |
| 143 |
log_dir: Path = Path("logs") |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
@cyclopts.Parameter(name="*") |
| 148 |
@dataclass |
| 149 |
class PipelineConfig: |
| 150 |
"""Complete pipeline configuration combining multiple configs. |
| 151 |
|
| 152 |
Parameters |
| 153 |
---------- |
| 154 |
name |
| 155 |
Pipeline name for identification. |
| 156 |
paths |
| 157 |
Path configuration. |
| 158 |
processing |
| 159 |
Processing configuration. |
| 160 |
dry_run |
| 161 |
If True, simulate execution without making changes. |
| 162 |
""" |
| 163 |
|
| 164 |
name: str = "default-pipeline" |
| 165 |
paths: PathConfig = field(default_factory=PathConfig) |
| 166 |
processing: ProcessingConfig = field(default_factory=ProcessingConfig) |
| 167 |
dry_run: bool = False |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
from pydantic import BaseModel, Field |
| 175 |
|
| 176 |
|
| 177 |
class ServerConfig(BaseModel): |
| 178 |
"""Server configuration using Pydantic. |
| 179 |
|
| 180 |
Parameters |
| 181 |
---------- |
| 182 |
host |
| 183 |
Server bind address. |
| 184 |
port |
| 185 |
Server port number. |
| 186 |
workers |
| 187 |
Number of worker processes. |
| 188 |
timeout |
| 189 |
Request timeout in seconds. |
| 190 |
debug |
| 191 |
Enable debug mode. |
| 192 |
""" |
| 193 |
|
| 194 |
host: str = "0.0.0.0" |
| 195 |
port: int = Field(default=8000, ge=1, le=65535) |
| 196 |
workers: int = Field(default=4, ge=1) |
| 197 |
timeout: float = 30.0 |
| 198 |
debug: bool = False |
| 199 |
|
| 200 |
|
| 201 |
class AuthConfig(BaseModel): |
| 202 |
"""Authentication configuration. |
| 203 |
|
| 204 |
Parameters |
| 205 |
---------- |
| 206 |
provider |
| 207 |
Authentication provider type. |
| 208 |
token_expiry |
| 209 |
Token expiration time in seconds. |
| 210 |
refresh_enabled |
| 211 |
Enable token refresh. |
| 212 |
allowed_origins |
| 213 |
List of allowed CORS origins. |
| 214 |
""" |
| 215 |
|
| 216 |
provider: Literal["oauth2", "jwt", "basic", "none"] = "jwt" |
| 217 |
token_expiry: int = 3600 |
| 218 |
refresh_enabled: bool = True |
| 219 |
allowed_origins: list[str] = Field(default_factory=lambda: ["*"]) |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
import attrs |
| 227 |
|
| 228 |
|
| 229 |
@attrs.define |
| 230 |
class CacheConfig: |
| 231 |
"""Cache configuration using attrs. |
| 232 |
|
| 233 |
Parameters |
| 234 |
---------- |
| 235 |
backend |
| 236 |
Cache backend type. |
| 237 |
ttl |
| 238 |
Time-to-live in seconds. |
| 239 |
max_size |
| 240 |
Maximum cache size in MB. |
| 241 |
compression |
| 242 |
Enable compression. |
| 243 |
""" |
| 244 |
|
| 245 |
backend: Literal["memory", "redis", "memcached", "disk"] = "memory" |
| 246 |
ttl: int = 300 |
| 247 |
max_size: int = 1024 |
| 248 |
compression: bool = False |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
global_group = Group.create_ordered("Global Options", sort_key=0) |
| 259 |
subcommands_group = Group.create_ordered("Subcommands", sort_key=1) |
| 260 |
utilities_group = Group.create_ordered("Utilities", sort_key=2) |
| 261 |
hidden_group = Group.create_ordered("Hidden", sort_key=99, show=False) |
| 262 |
|
| 263 |
app = App( |
| 264 |
name="complex-cli", |
| 265 |
help="Complex CLI application for comprehensive documentation testing.", |
| 266 |
version="1.0.0", |
| 267 |
) |
| 268 |
app.meta.group_parameters = global_group |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
@app.meta.default |
| 277 |
def main_launcher( |
| 278 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 279 |
config_file: Annotated[Path | None, Parameter(parse=False, group=global_group)] = None, |
| 280 |
verbose: Annotated[int, Parameter(name=["-v", "--verbose"], count=True, group=global_group)] = 0, |
| 281 |
quiet: Annotated[bool, Parameter(name=["-q", "--quiet"], group=global_group)] = False, |
| 282 |
log_level: Annotated[LogLevel, Parameter(group=global_group)] = LogLevel.INFO, |
| 283 |
no_color: Annotated[bool, Parameter(group=global_group, help="Disable colored output")] = False, |
| 284 |
): |
| 285 |
"""Global option handler that intercepts all commands. |
| 286 |
|
| 287 |
This demonstrates the meta.default pattern for handling global options |
| 288 |
that apply to all subcommands. |
| 289 |
|
| 290 |
Parameters |
| 291 |
---------- |
| 292 |
tokens |
| 293 |
Command tokens to forward. |
| 294 |
config_file |
| 295 |
Configuration file path (not parsed from CLI). |
| 296 |
verbose |
| 297 |
Verbosity level (-v, -vv, -vvv). |
| 298 |
quiet |
| 299 |
Suppress non-essential output. |
| 300 |
log_level |
| 301 |
Logging level. |
| 302 |
no_color |
| 303 |
Disable colored output. |
| 304 |
""" |
| 305 |
|
| 306 |
app(tokens) |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
@app.command(alias=["ver", "v"], group=utilities_group) |
| 315 |
def version(): |
| 316 |
"""Show version information. |
| 317 |
|
| 318 |
Displays the application version and system information. |
| 319 |
""" |
| 320 |
print("complex-cli version 1.0.0") |
| 321 |
print(f"Python {sys.version}") |
| 322 |
|
| 323 |
|
| 324 |
@app.command(alias="i", group=utilities_group) |
| 325 |
def info( |
| 326 |
detailed: bool = False, |
| 327 |
format: OutputFormat = OutputFormat.TABLE, |
| 328 |
): |
| 329 |
"""Show application information. |
| 330 |
|
| 331 |
Parameters |
| 332 |
---------- |
| 333 |
detailed |
| 334 |
Show detailed information including dependencies. |
| 335 |
format |
| 336 |
Output format for the information. |
| 337 |
""" |
| 338 |
print(f"Info: detailed={detailed}, format={format.value}") |
| 339 |
|
| 340 |
|
| 341 |
@app.command(group=hidden_group, show=False) |
| 342 |
def debug_internal(): |
| 343 |
"""Internal debug command (hidden from help). |
| 344 |
|
| 345 |
This command is for internal debugging purposes only. |
| 346 |
""" |
| 347 |
print("Debug internal command executed") |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
admin_app = App( |
| 355 |
name="admin", |
| 356 |
help="Administrative commands for system management.", |
| 357 |
group=subcommands_group, |
| 358 |
default_parameter=Parameter(negative=""), |
| 359 |
) |
| 360 |
app.command(admin_app) |
| 361 |
|
| 362 |
|
| 363 |
@admin_app.command |
| 364 |
def status( |
| 365 |
services: list[str] | None = None, |
| 366 |
*, |
| 367 |
watch: Annotated[bool, Parameter(name=["-w", "--watch"])] = False, |
| 368 |
interval: Annotated[int, Parameter(validator=validators.Number(gte=1))] = 5, |
| 369 |
): |
| 370 |
"""Show system status. |
| 371 |
|
| 372 |
Parameters |
| 373 |
---------- |
| 374 |
services |
| 375 |
Specific services to check (all if not specified). |
| 376 |
watch |
| 377 |
Continuously watch status. |
| 378 |
interval |
| 379 |
Refresh interval in seconds when watching. |
| 380 |
""" |
| 381 |
print(f"Status: services={services}, watch={watch}, interval={interval}") |
| 382 |
|
| 383 |
|
| 384 |
@admin_app.command |
| 385 |
def config_cmd( |
| 386 |
*, |
| 387 |
db: DatabaseConfig = DatabaseConfig(), |
| 388 |
): |
| 389 |
"""Configure database settings. |
| 390 |
|
| 391 |
Parameters |
| 392 |
---------- |
| 393 |
db |
| 394 |
Database configuration options. |
| 395 |
""" |
| 396 |
print(f"Config: db={db}") |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
users_app = App( |
| 404 |
name="users", |
| 405 |
help="User management commands.", |
| 406 |
) |
| 407 |
admin_app.command(users_app) |
| 408 |
|
| 409 |
|
| 410 |
@users_app.command |
| 411 |
def list_users( |
| 412 |
active_only: bool = False, |
| 413 |
role: Literal["admin", "user", "guest"] | None = None, |
| 414 |
limit: Annotated[int, Parameter(validator=validators.Number(gte=1, lte=1000))] = 100, |
| 415 |
format: OutputFormat = OutputFormat.TABLE, |
| 416 |
): |
| 417 |
"""List all users. |
| 418 |
|
| 419 |
Parameters |
| 420 |
---------- |
| 421 |
active_only |
| 422 |
Show only active users. |
| 423 |
role |
| 424 |
Filter by user role. |
| 425 |
limit |
| 426 |
Maximum number of users to display. |
| 427 |
format |
| 428 |
Output format. |
| 429 |
""" |
| 430 |
print(f"List users: active_only={active_only}, role={role}, limit={limit}") |
| 431 |
|
| 432 |
|
| 433 |
@users_app.command |
| 434 |
def create( |
| 435 |
username: str, |
| 436 |
email: str, |
| 437 |
/, |
| 438 |
*, |
| 439 |
role: Literal["admin", "user", "guest"] = "user", |
| 440 |
permissions: Permission = Permission.READ, |
| 441 |
send_welcome: bool = True, |
| 442 |
): |
| 443 |
"""Create a new user. |
| 444 |
|
| 445 |
Parameters |
| 446 |
---------- |
| 447 |
username |
| 448 |
Unique username for the new user. |
| 449 |
email |
| 450 |
Email address for the new user. |
| 451 |
role |
| 452 |
User role assignment. |
| 453 |
permissions |
| 454 |
Initial permission flags. |
| 455 |
send_welcome |
| 456 |
Send welcome email after creation. |
| 457 |
""" |
| 458 |
print(f"Create user: {username}, {email}, role={role}, permissions={permissions}") |
| 459 |
|
| 460 |
|
| 461 |
@users_app.command |
| 462 |
def delete( |
| 463 |
username: str, |
| 464 |
/, |
| 465 |
*, |
| 466 |
force: Annotated[bool, Parameter(name=["-f", "--force"])] = False, |
| 467 |
backup: bool = True, |
| 468 |
): |
| 469 |
"""Delete a user. |
| 470 |
|
| 471 |
Parameters |
| 472 |
---------- |
| 473 |
username |
| 474 |
Username to delete. |
| 475 |
force |
| 476 |
Skip confirmation prompt. |
| 477 |
backup |
| 478 |
Create backup before deletion. |
| 479 |
""" |
| 480 |
print(f"Delete user: {username}, force={force}, backup={backup}") |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
permissions_app = App( |
| 488 |
name="permissions", |
| 489 |
help="Permission management for users.", |
| 490 |
) |
| 491 |
users_app.command(permissions_app) |
| 492 |
|
| 493 |
|
| 494 |
@permissions_app.command |
| 495 |
def grant( |
| 496 |
username: str, |
| 497 |
permission: Permission, |
| 498 |
/, |
| 499 |
*, |
| 500 |
resource: str | None = None, |
| 501 |
expires: str | None = None, |
| 502 |
): |
| 503 |
"""Grant permissions to a user. |
| 504 |
|
| 505 |
Parameters |
| 506 |
---------- |
| 507 |
username |
| 508 |
Target username. |
| 509 |
permission |
| 510 |
Permission flags to grant. |
| 511 |
resource |
| 512 |
Specific resource to grant access to. |
| 513 |
expires |
| 514 |
Expiration date (ISO format). |
| 515 |
""" |
| 516 |
print(f"Grant: {username}, {permission}, resource={resource}") |
| 517 |
|
| 518 |
|
| 519 |
@permissions_app.command |
| 520 |
def revoke( |
| 521 |
username: str, |
| 522 |
permission: Permission, |
| 523 |
/, |
| 524 |
): |
| 525 |
"""Revoke permissions from a user. |
| 526 |
|
| 527 |
Parameters |
| 528 |
---------- |
| 529 |
username |
| 530 |
Target username. |
| 531 |
permission |
| 532 |
Permission flags to revoke. |
| 533 |
""" |
| 534 |
print(f"Revoke: {username}, {permission}") |
| 535 |
|
| 536 |
|
| 537 |
@permissions_app.command |
| 538 |
def audit( |
| 539 |
username: str | None = None, |
| 540 |
days: int = 30, |
| 541 |
format: OutputFormat = OutputFormat.TABLE, |
| 542 |
): |
| 543 |
"""Audit permission changes. |
| 544 |
|
| 545 |
Parameters |
| 546 |
---------- |
| 547 |
username |
| 548 |
Filter by username (all users if not specified). |
| 549 |
days |
| 550 |
Number of days to look back. |
| 551 |
format |
| 552 |
Output format for audit report. |
| 553 |
""" |
| 554 |
print(f"Audit: username={username}, days={days}, format={format}") |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
roles_app = App( |
| 562 |
name="roles", |
| 563 |
help="Role template management.", |
| 564 |
) |
| 565 |
permissions_app.command(roles_app) |
| 566 |
|
| 567 |
|
| 568 |
@roles_app.command |
| 569 |
def list_roles( |
| 570 |
include_system: bool = False, |
| 571 |
): |
| 572 |
"""List all role templates. |
| 573 |
|
| 574 |
Parameters |
| 575 |
---------- |
| 576 |
include_system |
| 577 |
Include built-in system roles. |
| 578 |
""" |
| 579 |
print(f"List roles: include_system={include_system}") |
| 580 |
|
| 581 |
|
| 582 |
@roles_app.command |
| 583 |
def create_role( |
| 584 |
name: str, |
| 585 |
/, |
| 586 |
*, |
| 587 |
permissions: Permission = Permission.READ, |
| 588 |
description: str = "", |
| 589 |
): |
| 590 |
"""Create a new role template. |
| 591 |
|
| 592 |
Parameters |
| 593 |
---------- |
| 594 |
name |
| 595 |
Role name. |
| 596 |
permissions |
| 597 |
Default permissions for this role. |
| 598 |
description |
| 599 |
Role description. |
| 600 |
""" |
| 601 |
print(f"Create role: {name}, permissions={permissions}") |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
data_app = App( |
| 609 |
name="data", |
| 610 |
help="Data processing commands.", |
| 611 |
group=subcommands_group, |
| 612 |
) |
| 613 |
app.command(data_app) |
| 614 |
|
| 615 |
|
| 616 |
@data_app.command |
| 617 |
def process( |
| 618 |
input_files: Annotated[list[Path], Parameter(help="Input files to process")], |
| 619 |
/, |
| 620 |
*, |
| 621 |
config: ProcessingConfig = ProcessingConfig(), |
| 622 |
paths: PathConfig = PathConfig(), |
| 623 |
): |
| 624 |
"""Process data files with configurable options. |
| 625 |
|
| 626 |
This command demonstrates dataclass parameter flattening where |
| 627 |
all fields from ProcessingConfig and PathConfig become CLI options. |
| 628 |
|
| 629 |
Parameters |
| 630 |
---------- |
| 631 |
input_files |
| 632 |
List of input files to process. |
| 633 |
config |
| 634 |
Processing configuration. |
| 635 |
paths |
| 636 |
Path configuration. |
| 637 |
""" |
| 638 |
print(f"Process: files={input_files}, config={config}, paths={paths}") |
| 639 |
|
| 640 |
|
| 641 |
@data_app.command |
| 642 |
def pipeline( |
| 643 |
*, |
| 644 |
config: PipelineConfig = PipelineConfig(), |
| 645 |
): |
| 646 |
"""Run a complete data pipeline. |
| 647 |
|
| 648 |
Demonstrates nested dataclass flattening (PipelineConfig contains |
| 649 |
PathConfig and ProcessingConfig). |
| 650 |
|
| 651 |
Parameters |
| 652 |
---------- |
| 653 |
config |
| 654 |
Complete pipeline configuration. |
| 655 |
""" |
| 656 |
print(f"Pipeline: config={config}") |
| 657 |
|
| 658 |
|
| 659 |
@data_app.command |
| 660 |
def validate( |
| 661 |
input_path: Path, |
| 662 |
/, |
| 663 |
*, |
| 664 |
strict: bool = False, |
| 665 |
schema_file: Annotated[Path | None, Parameter(validator=validators.Path(exists=True))] = None, |
| 666 |
ignore_patterns: list[str] | None = None, |
| 667 |
): |
| 668 |
"""Validate data files against schema. |
| 669 |
|
| 670 |
Parameters |
| 671 |
---------- |
| 672 |
input_path |
| 673 |
Path to validate. |
| 674 |
strict |
| 675 |
Enable strict validation mode. |
| 676 |
schema_file |
| 677 |
Custom schema file (must exist). |
| 678 |
ignore_patterns |
| 679 |
Patterns to ignore during validation. |
| 680 |
""" |
| 681 |
print(f"Validate: {input_path}, strict={strict}") |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
server_app = App( |
| 689 |
name="server", |
| 690 |
help="Server management commands.", |
| 691 |
group=subcommands_group, |
| 692 |
) |
| 693 |
app.command(server_app) |
| 694 |
|
| 695 |
|
| 696 |
@server_app.command |
| 697 |
def start( |
| 698 |
*, |
| 699 |
server: ServerConfig = ServerConfig(), |
| 700 |
auth: AuthConfig = AuthConfig(), |
| 701 |
): |
| 702 |
"""Start the server with configuration. |
| 703 |
|
| 704 |
Demonstrates Pydantic model support for CLI parameters. |
| 705 |
|
| 706 |
Parameters |
| 707 |
---------- |
| 708 |
server |
| 709 |
Server configuration. |
| 710 |
auth |
| 711 |
Authentication configuration. |
| 712 |
""" |
| 713 |
print(f"Start server: {server}, auth={auth}") |
| 714 |
|
| 715 |
|
| 716 |
@server_app.command |
| 717 |
def stop( |
| 718 |
*, |
| 719 |
graceful: bool = True, |
| 720 |
timeout: Annotated[int, Parameter(validator=validators.Number(gte=0))] = 30, |
| 721 |
force: Annotated[bool, Parameter(name=["-f", "--force"])] = False, |
| 722 |
): |
| 723 |
"""Stop the server. |
| 724 |
|
| 725 |
Parameters |
| 726 |
---------- |
| 727 |
graceful |
| 728 |
Perform graceful shutdown. |
| 729 |
timeout |
| 730 |
Shutdown timeout in seconds. |
| 731 |
force |
| 732 |
Force immediate shutdown. |
| 733 |
""" |
| 734 |
print(f"Stop server: graceful={graceful}, timeout={timeout}, force={force}") |
| 735 |
|
| 736 |
|
| 737 |
@server_app.command |
| 738 |
def restart( |
| 739 |
rolling: bool = False, |
| 740 |
delay: int = 5, |
| 741 |
): |
| 742 |
"""Restart the server. |
| 743 |
|
| 744 |
Parameters |
| 745 |
---------- |
| 746 |
rolling |
| 747 |
Perform rolling restart (zero downtime). |
| 748 |
delay |
| 749 |
Delay between worker restarts in seconds. |
| 750 |
""" |
| 751 |
print(f"Restart server: rolling={rolling}, delay={delay}") |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
cache_app = App( |
| 759 |
name="cache", |
| 760 |
help="Cache management commands.", |
| 761 |
group=utilities_group, |
| 762 |
) |
| 763 |
app.command(cache_app) |
| 764 |
|
| 765 |
|
| 766 |
@cache_app.command |
| 767 |
def configure( |
| 768 |
*, |
| 769 |
config: CacheConfig = CacheConfig(), |
| 770 |
): |
| 771 |
"""Configure cache settings. |
| 772 |
|
| 773 |
Demonstrates attrs class support for CLI parameters. |
| 774 |
|
| 775 |
Parameters |
| 776 |
---------- |
| 777 |
config |
| 778 |
Cache configuration. |
| 779 |
""" |
| 780 |
print(f"Configure cache: {config}") |
| 781 |
|
| 782 |
|
| 783 |
@cache_app.command |
| 784 |
def clear( |
| 785 |
pattern: str = "*", |
| 786 |
dry_run: bool = False, |
| 787 |
): |
| 788 |
"""Clear cache entries. |
| 789 |
|
| 790 |
Parameters |
| 791 |
---------- |
| 792 |
pattern |
| 793 |
Pattern to match cache keys. |
| 794 |
dry_run |
| 795 |
Show what would be cleared without actually clearing. |
| 796 |
""" |
| 797 |
print(f"Clear cache: pattern={pattern}, dry_run={dry_run}") |
| 798 |
|
| 799 |
|
| 800 |
@cache_app.command |
| 801 |
def stats( |
| 802 |
detailed: bool = False, |
| 803 |
format: OutputFormat = OutputFormat.TABLE, |
| 804 |
): |
| 805 |
"""Show cache statistics. |
| 806 |
|
| 807 |
Parameters |
| 808 |
---------- |
| 809 |
detailed |
| 810 |
Show detailed statistics. |
| 811 |
format |
| 812 |
Output format. |
| 813 |
""" |
| 814 |
print(f"Cache stats: detailed={detailed}, format={format}") |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
@app.command(group=utilities_group) |
| 823 |
def complex_types( |
| 824 |
|
| 825 |
worker_count: int | Literal["auto"] = "auto", |
| 826 |
|
| 827 |
quality: Literal["low", "medium", "high"] | Literal["custom"] = "medium", |
| 828 |
|
| 829 |
tags: list[str] | None = None, |
| 830 |
|
| 831 |
formats: list[OutputFormat] = [OutputFormat.JSON], |
| 832 |
|
| 833 |
thresholds: list[float] | Literal["default"] = "default", |
| 834 |
|
| 835 |
config_path: Annotated[Path | None, Parameter(validator=validators.Path(exists=True))] = None, |
| 836 |
): |
| 837 |
"""Demonstrate complex type annotations. |
| 838 |
|
| 839 |
This command showcases various complex type patterns that the |
| 840 |
documentation system needs to handle correctly. |
| 841 |
|
| 842 |
Parameters |
| 843 |
---------- |
| 844 |
worker_count |
| 845 |
Number of workers or "auto" for automatic detection. |
| 846 |
quality |
| 847 |
Quality preset or "custom" for manual configuration. |
| 848 |
tags |
| 849 |
Optional list of tags. |
| 850 |
formats |
| 851 |
List of output formats. |
| 852 |
thresholds |
| 853 |
Threshold values or "default" for defaults. |
| 854 |
config_path |
| 855 |
Optional config file path (must exist if provided). |
| 856 |
""" |
| 857 |
print(f"Complex types: worker_count={worker_count}, quality={quality}") |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
@app.command(group=utilities_group) |
| 866 |
def numpy_style( |
| 867 |
name: str, |
| 868 |
count: int = 1, |
| 869 |
): |
| 870 |
"""Command with NumPy-style docstring. |
| 871 |
|
| 872 |
This command demonstrates NumPy docstring format which is the |
| 873 |
default for cyclopts. |
| 874 |
|
| 875 |
Parameters |
| 876 |
---------- |
| 877 |
name : str |
| 878 |
The name parameter. |
| 879 |
count : int, optional |
| 880 |
The count parameter, by default 1. |
| 881 |
|
| 882 |
Returns |
| 883 |
------- |
| 884 |
None |
| 885 |
This function doesn't return anything. |
| 886 |
|
| 887 |
Raises |
| 888 |
------ |
| 889 |
ValueError |
| 890 |
If name is empty. |
| 891 |
|
| 892 |
Examples |
| 893 |
-------- |
| 894 |
>>> numpy_style("test", count=5) |
| 895 |
""" |
| 896 |
print(f"NumPy style: name={name}, count={count}") |
| 897 |
|
| 898 |
|
| 899 |
@app.command(group=utilities_group) |
| 900 |
def google_style( |
| 901 |
name: str, |
| 902 |
count: int = 1, |
| 903 |
): |
| 904 |
"""Command with Google-style docstring. |
| 905 |
|
| 906 |
This command demonstrates Google docstring format. |
| 907 |
|
| 908 |
Args: |
| 909 |
name: The name parameter. |
| 910 |
count: The count parameter. Defaults to 1. |
| 911 |
|
| 912 |
Returns |
| 913 |
------- |
| 914 |
None: This function doesn't return anything. |
| 915 |
|
| 916 |
Raises |
| 917 |
------ |
| 918 |
ValueError: If name is empty. |
| 919 |
|
| 920 |
Example: |
| 921 |
>>> google_style("test", count=5) |
| 922 |
""" |
| 923 |
print(f"Google style: name={name}, count={count}") |
| 924 |
|
| 925 |
|
| 926 |
@app.command(group=utilities_group) |
| 927 |
def sphinx_style( |
| 928 |
name: str, |
| 929 |
count: int = 1, |
| 930 |
): |
| 931 |
"""Command with Sphinx-style docstring. |
| 932 |
|
| 933 |
This command demonstrates Sphinx/reST docstring format. |
| 934 |
|
| 935 |
:param name: The name parameter. |
| 936 |
:type name: str |
| 937 |
:param count: The count parameter. |
| 938 |
:type count: int |
| 939 |
:returns: None |
| 940 |
:rtype: None |
| 941 |
:raises ValueError: If name is empty. |
| 942 |
""" |
| 943 |
print(f"Sphinx style: name={name}, count={count}") |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
@app.command(show=False) |
| 952 |
def internal_maintenance(): |
| 953 |
"""Internal maintenance command. |
| 954 |
|
| 955 |
This command is hidden from the main help but can still be invoked. |
| 956 |
""" |
| 957 |
print("Running internal maintenance...") |
| 958 |
|
| 959 |
|
| 960 |
@app.command(group=hidden_group) |
| 961 |
def secret_feature( |
| 962 |
enable: bool = False, |
| 963 |
code: Annotated[str, Parameter(show=False)] = "default", |
| 964 |
): |
| 965 |
"""Secret feature command. |
| 966 |
|
| 967 |
This command has a hidden parameter. |
| 968 |
|
| 969 |
Parameters |
| 970 |
---------- |
| 971 |
enable |
| 972 |
Enable the secret feature. |
| 973 |
code |
| 974 |
Secret activation code (hidden from help). |
| 975 |
""" |
| 976 |
print(f"Secret feature: enable={enable}, code={code}") |
| 977 |
|
| 978 |
|
| 979 |
if __name__ == "__main__": |
| 980 |
app() |