| 1 |
|
| 2 |
"""Cyclopts Demo Application - Testing completion features.""" |
| 3 |
|
| 4 |
from pathlib import Path |
| 5 |
from typing import Annotated, Literal |
| 6 |
|
| 7 |
from cyclopts import App, Parameter, validators |
| 8 |
|
| 9 |
app = App( |
| 10 |
name="cyclopts-demo", |
| 11 |
help="Demo application for testing Cyclopts completion features.", |
| 12 |
) |
| 13 |
app.register_install_completion_command() |
| 14 |
|
| 15 |
|
| 16 |
@app.default |
| 17 |
def main( |
| 18 |
verbose: bool = False, |
| 19 |
config: Path | None = None, |
| 20 |
): |
| 21 |
"""Main command. |
| 22 |
|
| 23 |
Parameters |
| 24 |
---------- |
| 25 |
verbose : bool |
| 26 |
Enable verbose output. |
| 27 |
config : Path, optional |
| 28 |
Configuration file path. |
| 29 |
""" |
| 30 |
print(f"Main command: verbose={verbose}, config={config}") |
| 31 |
|
| 32 |
|
| 33 |
files_app = App(name="files", help="File operations commands.") |
| 34 |
app.command(files_app) |
| 35 |
|
| 36 |
|
| 37 |
@files_app.command |
| 38 |
def cp( |
| 39 |
source: Path, |
| 40 |
destination: Path, |
| 41 |
/, |
| 42 |
*, |
| 43 |
recursive: Annotated[bool, Parameter(help="Copy directories recursively")] = False, |
| 44 |
preserve: Annotated[bool, Parameter(help="Preserve file attributes")] = False, |
| 45 |
verbose: bool = False, |
| 46 |
): |
| 47 |
"""Copy files or directories. |
| 48 |
|
| 49 |
Parameters |
| 50 |
---------- |
| 51 |
source : Path |
| 52 |
Source file or directory. |
| 53 |
destination : Path |
| 54 |
Destination file or directory. |
| 55 |
recursive : bool |
| 56 |
Copy directories recursively. |
| 57 |
preserve : bool |
| 58 |
Preserve file attributes (timestamps, permissions). |
| 59 |
verbose : bool |
| 60 |
Show verbose output. |
| 61 |
""" |
| 62 |
print(f"Copy: {source} -> {destination} (recursive={recursive}, preserve={preserve}, verbose={verbose})") |
| 63 |
|
| 64 |
|
| 65 |
@files_app.command |
| 66 |
def mv( |
| 67 |
source: Path, |
| 68 |
destination: Path, |
| 69 |
/, |
| 70 |
*, |
| 71 |
force: Annotated[bool, Parameter(help="Force overwrite if destination exists")] = False, |
| 72 |
backup: Annotated[bool, Parameter(help="Create backup of existing destination")] = False, |
| 73 |
verbose: bool = False, |
| 74 |
): |
| 75 |
"""Move files or directories. |
| 76 |
|
| 77 |
Parameters |
| 78 |
---------- |
| 79 |
source : Path |
| 80 |
Source file or directory. |
| 81 |
destination : Path |
| 82 |
Destination file or directory. |
| 83 |
force : bool |
| 84 |
Force overwrite if destination exists. |
| 85 |
backup : bool |
| 86 |
Create backup of existing destination files. |
| 87 |
verbose : bool |
| 88 |
Show verbose output. |
| 89 |
""" |
| 90 |
print(f"Move: {source} -> {destination} (force={force}, backup={backup}, verbose={verbose})") |
| 91 |
|
| 92 |
|
| 93 |
@files_app.command |
| 94 |
def ls( |
| 95 |
path: Path = Path(), |
| 96 |
/, |
| 97 |
*, |
| 98 |
all: Annotated[bool, Parameter(help="Show hidden files")] = False, |
| 99 |
long: Annotated[bool, Parameter(help="Use long listing format")] = False, |
| 100 |
sort_by: Literal["name", "size", "time", "extension"] = "name", |
| 101 |
reverse: bool = False, |
| 102 |
): |
| 103 |
"""List directory contents. |
| 104 |
|
| 105 |
Parameters |
| 106 |
---------- |
| 107 |
path : Path |
| 108 |
Directory path to list (defaults to current directory). |
| 109 |
all : bool |
| 110 |
Show hidden files and directories. |
| 111 |
long : bool |
| 112 |
Use long listing format with details. |
| 113 |
sort_by : Literal["name", "size", "time", "extension"] |
| 114 |
Sort order. |
| 115 |
reverse : bool |
| 116 |
Reverse sort order. |
| 117 |
""" |
| 118 |
print(f"List: {path} (all={all}, long={long}, sort_by={sort_by}, reverse={reverse})") |
| 119 |
|
| 120 |
|
| 121 |
@files_app.command |
| 122 |
def find( |
| 123 |
pattern: str, |
| 124 |
/, |
| 125 |
*, |
| 126 |
path: Path = Path(), |
| 127 |
type: Literal["file", "directory", "symlink", "any"] = "any", |
| 128 |
case_sensitive: bool = True, |
| 129 |
max_depth: int | None = None, |
| 130 |
): |
| 131 |
"""Find files matching a pattern. |
| 132 |
|
| 133 |
Parameters |
| 134 |
---------- |
| 135 |
pattern : str |
| 136 |
Search pattern (supports wildcards). |
| 137 |
path : Path |
| 138 |
Root directory to search from. |
| 139 |
type : Literal["file", "directory", "symlink", "any"] |
| 140 |
Type of filesystem entry to find. |
| 141 |
case_sensitive : bool |
| 142 |
Case-sensitive pattern matching. |
| 143 |
max_depth : int, optional |
| 144 |
Maximum search depth (None for unlimited). |
| 145 |
""" |
| 146 |
print(f"Find: pattern={pattern}, path={path}, type={type}, case_sensitive={case_sensitive}, max_depth={max_depth}") |
| 147 |
|
| 148 |
|
| 149 |
@app.command |
| 150 |
def positional_choice(param: Literal["foo", "bar", "baz"], /): |
| 151 |
"""Test positional-only parameter with Literal choices. |
| 152 |
|
| 153 |
Parameters |
| 154 |
---------- |
| 155 |
param : Literal["foo", "bar", "baz"] |
| 156 |
Choose one: foo, bar, or baz. |
| 157 |
""" |
| 158 |
print(f"Called with: {param}") |
| 159 |
|
| 160 |
|
| 161 |
@app.command |
| 162 |
def multi_positional( |
| 163 |
first: Literal["alpha", "beta", "gamma"], |
| 164 |
second: Literal["red", "green", "blue"], |
| 165 |
/, |
| 166 |
): |
| 167 |
"""Test multiple positional-only parameters with Literal choices. |
| 168 |
|
| 169 |
Parameters |
| 170 |
---------- |
| 171 |
first : Literal["alpha", "beta", "gamma"] |
| 172 |
First choice: Greek letters. |
| 173 |
second : Literal["red", "green", "blue"] |
| 174 |
Second choice: colors. |
| 175 |
""" |
| 176 |
print(f"Called with: first={first}, second={second}") |
| 177 |
|
| 178 |
|
| 179 |
@app.command |
| 180 |
def deploy( |
| 181 |
environment: Literal["dev", "staging", "production"], |
| 182 |
region: Literal["us-east-1", "us-west-2", "eu-west-1", "ap-southeast-1"] = "us-east-1", |
| 183 |
dry_run: bool = False, |
| 184 |
skip_tests: bool = False, |
| 185 |
confirm: bool = True, |
| 186 |
): |
| 187 |
"""Deploy application. |
| 188 |
|
| 189 |
Parameters |
| 190 |
---------- |
| 191 |
environment : Literal["dev", "staging", "production"] |
| 192 |
Target environment. |
| 193 |
region : Literal["us-east-1", "us-west-2", "eu-west-1", "ap-southeast-1"] |
| 194 |
AWS region. |
| 195 |
dry_run : bool |
| 196 |
Perform a dry run without making changes. |
| 197 |
skip_tests : bool |
| 198 |
Skip running tests. |
| 199 |
confirm : bool |
| 200 |
Require confirmation before deploying. |
| 201 |
""" |
| 202 |
print(f"Deploy: env={environment}, region={region}, dry_run={dry_run}, skip_tests={skip_tests}, confirm={confirm}") |
| 203 |
|
| 204 |
|
| 205 |
@app.command |
| 206 |
def process( |
| 207 |
items: list[str], |
| 208 |
count: Annotated[int, Parameter(validator=validators.Number(gt=0, lte=100))] = 1, |
| 209 |
threshold: float = 0.5, |
| 210 |
tags: list[str] | None = None, |
| 211 |
exclude: list[str] | None = None, |
| 212 |
): |
| 213 |
"""Process items. |
| 214 |
|
| 215 |
Parameters |
| 216 |
---------- |
| 217 |
items : list[str] |
| 218 |
Items to process. |
| 219 |
count : int |
| 220 |
Number of iterations (1-100). |
| 221 |
threshold : float |
| 222 |
Processing threshold. |
| 223 |
tags : list[str], optional |
| 224 |
Tags to apply. |
| 225 |
exclude : list[str] |
| 226 |
Items to exclude. |
| 227 |
""" |
| 228 |
if exclude is None: |
| 229 |
exclude = [] |
| 230 |
print(f"Process: items={items}, count={count}, threshold={threshold}, tags={tags}, exclude={exclude}") |
| 231 |
|
| 232 |
|
| 233 |
@app.command |
| 234 |
def concat( |
| 235 |
files: Annotated[list[Path], Parameter(help="Files to concatenate")], |
| 236 |
output: Annotated[Path | None, Parameter(help="Output file (defaults to stdout)")] = None, |
| 237 |
add_separators: bool = False, |
| 238 |
): |
| 239 |
"""Concatenate multiple files. |
| 240 |
|
| 241 |
This command demonstrates list[Path] completion (issue #654). |
| 242 |
Try tab-completing after --files to see file completion work! |
| 243 |
|
| 244 |
Parameters |
| 245 |
---------- |
| 246 |
files : list[Path] |
| 247 |
Files to concatenate together. |
| 248 |
output : Path, optional |
| 249 |
Output file path. If not specified, prints to stdout. |
| 250 |
add_separators : bool |
| 251 |
Add separator lines between files. |
| 252 |
""" |
| 253 |
print(f"Concat: files={files}, output={output}, add_separators={add_separators}") |
| 254 |
|
| 255 |
|
| 256 |
@app.command |
| 257 |
def image_convert( |
| 258 |
input_file: Annotated[Path, Parameter(help="Input image file")], |
| 259 |
output_file: Annotated[Path, Parameter(help="Output image file")], |
| 260 |
|
| 261 |
width: Annotated[int | None, Parameter(group="Image Settings", help="Output width in pixels")] = None, |
| 262 |
height: Annotated[int | None, Parameter(group="Image Settings", help="Output height in pixels")] = None, |
| 263 |
quality: Annotated[int, Parameter(group="Image Settings", help="Output quality (1-100)")] = 90, |
| 264 |
|
| 265 |
grayscale: Annotated[bool, Parameter(group="Processing Options", help="Convert to grayscale")] = False, |
| 266 |
rotate: Annotated[int, Parameter(group="Processing Options", help="Rotation angle (degrees)")] = 0, |
| 267 |
flip: Annotated[ |
| 268 |
Literal["none", "horizontal", "vertical", "both"], |
| 269 |
Parameter(group="Processing Options", help="Flip direction"), |
| 270 |
] = "none", |
| 271 |
|
| 272 |
format: Annotated[ |
| 273 |
Literal["auto", "jpeg", "png", "webp"], |
| 274 |
Parameter(group="Format Options", help="Output format"), |
| 275 |
] = "auto", |
| 276 |
progressive: Annotated[bool, Parameter(group="Format Options", help="Use progressive encoding")] = False, |
| 277 |
): |
| 278 |
"""Convert and transform images. |
| 279 |
|
| 280 |
This command demonstrates parameter grouping for better organization of related options. |
| 281 |
|
| 282 |
Parameters |
| 283 |
---------- |
| 284 |
input_file : Path |
| 285 |
Path to the input image file. |
| 286 |
output_file : Path |
| 287 |
Path where the converted image will be saved. |
| 288 |
width : int, optional |
| 289 |
Desired output width in pixels. Maintains aspect ratio if height not specified. |
| 290 |
height : int, optional |
| 291 |
Desired output height in pixels. Maintains aspect ratio if width not specified. |
| 292 |
quality : int |
| 293 |
JPEG/WebP quality level from 1 (lowest) to 100 (highest). |
| 294 |
grayscale : bool |
| 295 |
Convert the image to grayscale. |
| 296 |
rotate : int |
| 297 |
Rotation angle in degrees (clockwise). |
| 298 |
flip : Literal["none", "horizontal", "vertical", "both"] |
| 299 |
Flip the image horizontally, vertically, both, or none. |
| 300 |
format : Literal["auto", "jpeg", "png", "webp"] |
| 301 |
Output format. Auto-detects from file extension if set to 'auto'. |
| 302 |
progressive : bool |
| 303 |
Use progressive/interlaced encoding for web optimization. |
| 304 |
""" |
| 305 |
print( |
| 306 |
f"Image Convert: {input_file} -> {output_file}, " |
| 307 |
f"size={width}x{height}, quality={quality}, " |
| 308 |
f"grayscale={grayscale}, rotate={rotate}, flip={flip}, " |
| 309 |
f"format={format}, progressive={progressive}" |
| 310 |
) |
| 311 |
|
| 312 |
|
| 313 |
database_app = App(name="database", help="Database commands.") |
| 314 |
app.command(database_app) |
| 315 |
|
| 316 |
|
| 317 |
@database_app.command |
| 318 |
def connect( |
| 319 |
host: str = "localhost", |
| 320 |
port: int = 5432, |
| 321 |
username: str = "admin", |
| 322 |
password: str | None = None, |
| 323 |
ssl: bool = True, |
| 324 |
): |
| 325 |
"""Connect to database. |
| 326 |
|
| 327 |
Parameters |
| 328 |
---------- |
| 329 |
host : str |
| 330 |
Database host. |
| 331 |
port : int |
| 332 |
Database port. |
| 333 |
username : str |
| 334 |
Username. |
| 335 |
password : str, optional |
| 336 |
Password. |
| 337 |
ssl : bool |
| 338 |
Use SSL connection. |
| 339 |
""" |
| 340 |
print(f"DB Connect: host={host}, port={port}, user={username}, ssl={ssl}") |
| 341 |
|
| 342 |
|
| 343 |
@database_app.command |
| 344 |
def migrate( |
| 345 |
direction: Literal["up", "down"] = "up", |
| 346 |
steps: int = 1, |
| 347 |
target: str | None = None, |
| 348 |
force: bool = False, |
| 349 |
): |
| 350 |
"""Run database migrations. |
| 351 |
|
| 352 |
Parameters |
| 353 |
---------- |
| 354 |
direction : Literal["up", "down"] |
| 355 |
Migration direction. |
| 356 |
steps : int |
| 357 |
Number of migration steps. |
| 358 |
target : str, optional |
| 359 |
Target migration version. |
| 360 |
force : bool |
| 361 |
Force migration even with warnings. |
| 362 |
""" |
| 363 |
print(f"DB Migrate: direction={direction}, steps={steps}, target={target}, force={force}") |
| 364 |
|
| 365 |
|
| 366 |
@database_app.command |
| 367 |
def backup( |
| 368 |
output: Path, |
| 369 |
tables: list[str] | None = None, |
| 370 |
compress: bool = True, |
| 371 |
encryption: Literal["none", "aes256", "rsa"] = "none", |
| 372 |
): |
| 373 |
"""Backup database. |
| 374 |
|
| 375 |
Parameters |
| 376 |
---------- |
| 377 |
output : Path |
| 378 |
Backup output file. |
| 379 |
tables : list[str], optional |
| 380 |
Specific tables to backup. |
| 381 |
compress : bool |
| 382 |
Compress backup. |
| 383 |
encryption : Literal["none", "aes256", "rsa"] |
| 384 |
Encryption method. |
| 385 |
""" |
| 386 |
print(f"DB Backup: output={output}, tables={tables}, compress={compress}, encryption={encryption}") |
| 387 |
|
| 388 |
|
| 389 |
server_app = App(name="server", help="Server management commands.") |
| 390 |
app.command(server_app) |
| 391 |
|
| 392 |
|
| 393 |
@server_app.command |
| 394 |
def start( |
| 395 |
port: int = 8000, |
| 396 |
host: str = "0.0.0.0", |
| 397 |
workers: int = 4, |
| 398 |
reload: bool = False, |
| 399 |
log_level: Literal["debug", "info", "warning", "error", "critical"] = "info", |
| 400 |
): |
| 401 |
"""Start the server. |
| 402 |
|
| 403 |
Parameters |
| 404 |
---------- |
| 405 |
port : int |
| 406 |
Server port. |
| 407 |
host : str |
| 408 |
Server host. |
| 409 |
workers : int |
| 410 |
Number of worker processes. |
| 411 |
reload : bool |
| 412 |
Enable auto-reload. |
| 413 |
log_level : Literal["debug", "info", "warning", "error", "critical"] |
| 414 |
Logging level. |
| 415 |
""" |
| 416 |
print(f"Server Start: host={host}, port={port}, workers={workers}, reload={reload}, log_level={log_level}") |
| 417 |
|
| 418 |
|
| 419 |
@server_app.command |
| 420 |
def stop( |
| 421 |
graceful: bool = True, |
| 422 |
timeout: int = 30, |
| 423 |
): |
| 424 |
"""Stop the server. |
| 425 |
|
| 426 |
Parameters |
| 427 |
---------- |
| 428 |
graceful : bool |
| 429 |
Graceful shutdown. |
| 430 |
timeout : int |
| 431 |
Shutdown timeout in seconds. |
| 432 |
""" |
| 433 |
print(f"Server Stop: graceful={graceful}, timeout={timeout}") |
| 434 |
|
| 435 |
|
| 436 |
@server_app.command |
| 437 |
def status( |
| 438 |
detailed: bool = False, |
| 439 |
format: Literal["text", "json", "table"] = "text", |
| 440 |
): |
| 441 |
"""Show server status. |
| 442 |
|
| 443 |
Parameters |
| 444 |
---------- |
| 445 |
detailed : bool |
| 446 |
Show detailed status. |
| 447 |
format : Literal["text", "json", "table"] |
| 448 |
Output format. |
| 449 |
""" |
| 450 |
print(f"Server Status: detailed={detailed}, format={format}") |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
@app.command(name="utility:ping") |
| 455 |
def utility_ping_action( |
| 456 |
host: Annotated[str, Parameter(help="Host to ping")] = "localhost", |
| 457 |
port: Annotated[int, Parameter(help="Port number")] = 8080, |
| 458 |
timeout: Annotated[int, Parameter(help="Timeout in seconds")] = 5, |
| 459 |
count: Annotated[int, Parameter(help="Number of pings")] = 3, |
| 460 |
): |
| 461 |
"""Ping a utility service to check connectivity. |
| 462 |
|
| 463 |
Parameters |
| 464 |
---------- |
| 465 |
host : str |
| 466 |
Hostname or IP address of the service. |
| 467 |
port : int |
| 468 |
Port number to connect to. |
| 469 |
timeout : int |
| 470 |
Connection timeout in seconds. |
| 471 |
count : int |
| 472 |
Number of ping attempts. |
| 473 |
""" |
| 474 |
print(f"Pinging utility at {host}:{port} (timeout={timeout}s, count={count})") |
| 475 |
|
| 476 |
|
| 477 |
@app.command(name="utility:status") |
| 478 |
def utility_status_action( |
| 479 |
foo: Literal["beep", "boop"], |
| 480 |
/, |
| 481 |
service: Annotated[str, Parameter(help="Service name")] = "all", |
| 482 |
verbose: Annotated[bool, Parameter(help="Show detailed status")] = False, |
| 483 |
format: Annotated[Literal["text", "json", "yaml"], Parameter(help="Output format")] = "text", |
| 484 |
): |
| 485 |
"""Check the status of utility services. |
| 486 |
|
| 487 |
Parameters |
| 488 |
---------- |
| 489 |
service : str |
| 490 |
Name of the service to check, or 'all' for all services. |
| 491 |
verbose : bool |
| 492 |
Show detailed status information. |
| 493 |
format : Literal["text", "json", "yaml"] |
| 494 |
Output format for the status report. |
| 495 |
""" |
| 496 |
print(f"Utility status: service={service}, verbose={verbose}, format={format}") |
| 497 |
|
| 498 |
|
| 499 |
@app.command |
| 500 |
def dump_markdown(): |
| 501 |
"""Generate markdown documentation and save to intermediate.md. |
| 502 |
|
| 503 |
This is a debug command to inspect the intermediate markdown output |
| 504 |
that gets generated for the cyclopts-demo app. |
| 505 |
""" |
| 506 |
from cyclopts.docs.markdown import generate_markdown_docs |
| 507 |
|
| 508 |
|
| 509 |
markdown = generate_markdown_docs( |
| 510 |
app, |
| 511 |
recursive=True, |
| 512 |
heading_level=1, |
| 513 |
generate_toc=True, |
| 514 |
) |
| 515 |
|
| 516 |
|
| 517 |
with Path("intermediate.md").open("w") as f: |
| 518 |
f.write(markdown) |
| 519 |
|
| 520 |
print("✓ Markdown documentation written to intermediate.md") |
| 521 |
print(f" ({len(markdown)} characters, {len(markdown.splitlines())} lines)") |
| 522 |
|
| 523 |
|
| 524 |
if __name__ == "__main__": |
| 525 |
app() |