-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_main.py
More file actions
109 lines (90 loc) · 3.5 KB
/
cli_main.py
File metadata and controls
109 lines (90 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
"""
REDit CLI - Command-line interface for REDit server
Main entry point for the ga-red command
"""
import sys
import os
import argparse
from pathlib import Path
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
# Add the CLI directory to Python path for imports
cli_dir = Path(__file__).parent
sys.path.insert(0, str(cli_dir))
# Import commands
import commands.jobs as jobs
import commands.algorithms as algorithms
import commands.datasets as datasets
console = Console()
def print_main_help():
"""Print main help using Rich formatting"""
console.print(Panel.fit(
"[bold red]REDit CLI[/bold red] - Manage and execute adversarial attacks",
title="REDit Command Line Interface",
border_style="red"
))
console.print("\n[bold]Usage:[/bold]")
console.print(" ga-red [red]<command>[/red] [dim][options][/dim]")
console.print(" ga-red --help\n")
# Create commands table
table = Table(title="Available Commands", show_header=True, header_style="bold red")
table.add_column("Command", style="red", no_wrap=True)
table.add_column("Description", style="white")
table.add_row("jobs", "Manage jobs (list, show, run, attach, export results, etc.)")
table.add_row("datasets", "Manage datasets (list, show, create, export, etc.)")
table.add_row("algorithms", "View available attack algorithms")
console.print(table)
console.print("\n[bold]Common Workflows:[/bold]")
console.print(" [red]1.[/red] Run an attack: ga-red jobs run config.yaml")
console.print(" [red]2.[/red] Check job status: ga-red jobs show")
console.print(" [red]3.[/red] Get results: ga-red jobs results")
console.print(" [red]4.[/red] Export results: ga-red jobs export --csv output.csv")
console.print(" [red]5.[/red] List datasets: ga-red datasets list")
console.print(" [red]6.[/red] View algorithms: ga-red algorithms list")
console.print("\n[dim]For help on a specific command:[/dim]")
console.print(" ga-red [red]<command>[/red] --help")
console.print()
def main():
"""Main CLI entry point"""
# Check if asking for help
if len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] in ['--help', '-h']):
print_main_help()
sys.exit(0)
parser = argparse.ArgumentParser(
prog='ga-red',
description='REDit CLI - Manage and execute adversarial attacks',
formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False # We'll handle help ourselves
)
# Create subparsers for different commands
subparsers = parser.add_subparsers(
title='Commands',
description='Available commands',
dest='command',
help='Command to execute'
)
# Add command parsers
jobs.add_parser(subparsers)
datasets.add_parser(subparsers)
algorithms.add_parser(subparsers)
# Parse arguments
args = parser.parse_args()
# If no command specified, show help
if not args.command:
print_main_help()
sys.exit(0)
# Execute the appropriate command
if args.command == 'jobs':
jobs.execute(args)
elif args.command == 'datasets':
datasets.execute(args)
elif args.command == 'algorithms':
algorithms.execute(args)
else:
console.print(f"[red]Unknown command: {args.command}[/red]")
print_main_help()
sys.exit(1)
if __name__ == "__main__":
main()