11import typer
2+ import os
23from pathlib import Path
4+ from typing import Optional
35from ..server .auth import Auth
46from ..server .solution_manager import SolutionManager
57
68auth_manager = Auth ()
79solution_manager = SolutionManager (auth_manager .get_session ())
810
11+ LANGUAGE_MAP = {
12+ '.py' : 'python3' ,
13+ '.cpp' : 'cpp' ,
14+ '.c' : 'c' ,
15+ '.java' : 'java' ,
16+ '.js' : 'javascript' ,
17+ '.ts' : 'typescript' ,
18+ '.go' : 'golang' ,
19+ '.rs' : 'rust' ,
20+ '.rb' : 'ruby' ,
21+ '.cs' : 'csharp' ,
22+ '.swift' : 'swift' ,
23+ '.php' : 'php' ,
24+ }
25+
926def submit (
10- problem : str = typer .Argument (..., help = "Problem slug (e.g., 'two-sum')" ),
27+ problem : str = typer .Argument (..., help = "Problem slug or number (e.g., 'two-sum' or '1 ')" ),
1128 file : Path = typer .Argument (..., help = "Path to solution file" ),
12- lang : str = typer .Option ("python3" , help = "Programming language" )
29+ lang : Optional [str ] = typer .Option (None , help = "Programming language (auto-detected if not specified)" ),
30+ force : bool = typer .Option (False , "--force" , "-f" , help = "Skip confirmation prompt" )
1331):
14- """Submit a solution to LeetCode"""
32+ """
33+ Submit a solution to LeetCode
34+
35+ Uploads your solution file to LeetCode and returns the verdict.
36+ Language is auto-detected from file extension if not specified.
37+ """
1538 if not auth_manager .is_authenticated :
1639 typer .echo (typer .style ("❌ Please login first using the login command" , fg = typer .colors .RED ))
1740 raise typer .Exit (1 )
@@ -20,17 +43,50 @@ def submit(
2043 typer .echo (typer .style (f"❌ File not found: { file } " , fg = typer .colors .RED ))
2144 raise typer .Exit (1 )
2245
23- with open (file , 'r' ) as f :
24- code = f .read ()
46+ try :
47+ # Auto-detect language from file extension if not provided
48+ if not lang :
49+ extension = os .path .splitext (file )[1 ].lower ()
50+ if extension in LANGUAGE_MAP :
51+ lang = LANGUAGE_MAP [extension ]
52+ typer .echo (typer .style (f"🔍 Auto-detected language: { lang } " , fg = typer .colors .BLUE ))
53+ else :
54+ typer .echo (typer .style (f"❌ Could not detect language for { extension } files. Please specify with --lang" , fg = typer .colors .RED ))
55+ raise typer .Exit (1 )
56+
57+ with open (file , 'r' ) as f :
58+ code = f .read ()
59+
60+ # Confirm submission unless forced
61+ if not force :
62+ typer .echo (typer .style (f"Problem: { problem } " , fg = typer .colors .BLUE ))
63+ typer .echo (typer .style (f"Language: { lang } " , fg = typer .colors .BLUE ))
64+ typer .echo (typer .style (f"File: { file } " , fg = typer .colors .BLUE ))
65+ if not typer .confirm ("Do you want to submit this solution?" ):
66+ typer .echo (typer .style ("Submission canceled" , fg = typer .colors .YELLOW ))
67+ raise typer .Exit (0 )
68+
69+ typer .echo (typer .style ("📤 Submitting solution..." , fg = typer .colors .YELLOW ))
70+ result = solution_manager .submit_solution (problem , code , lang )
71+
72+ if result ["success" ]:
73+ status = result ['status' ]
74+ status_color = (
75+ typer .colors .GREEN if status == "Accepted"
76+ else typer .colors .YELLOW if status == "Runtime Error" or status == "Time Limit Exceeded"
77+ else typer .colors .RED
78+ )
79+
80+ typer .echo (typer .style (f"\n ✨ Status: { status } " , fg = status_color ))
81+ typer .echo (f"⏱️ Runtime: { result ['runtime' ]} " )
82+ typer .echo (f"💾 Memory: { result ['memory' ]} " )
83+ typer .echo (f"✅ Passed: { result ['passed_testcases' ]} /{ result ['total_testcases' ]} test cases" )
2584
26- typer .echo (typer .style ("📤 Submitting solution..." , fg = typer .colors .YELLOW ))
27- result = solution_manager .submit_solution (problem , code , lang )
85+ if status != "Accepted" :
86+ typer .echo (typer .style (f"\n ❗ Error message: { result .get ('error_message' , 'No details available' )} " , fg = typer .colors .RED ))
87+ else :
88+ typer .echo (typer .style (f"\n ❌ Submission failed: { result ['error' ]} " , fg = typer .colors .RED ))
2889
29- if result ["success" ]:
30- status_color = typer .colors .GREEN if result ["status" ] == "Accepted" else typer .colors .RED
31- typer .echo (typer .style (f"\n ✨ Status: { result ['status' ]} " , fg = status_color ))
32- typer .echo (f"⏱️ Runtime: { result ['runtime' ]} " )
33- typer .echo (f"💾 Memory: { result ['memory' ]} " )
34- typer .echo (f"✅ Passed: { result ['passed_testcases' ]} /{ result ['total_testcases' ]} test cases" )
35- else :
36- typer .echo (typer .style (f"\n ❌ Submission failed: { result ['error' ]} " , fg = typer .colors .RED ))
90+ except Exception as e :
91+ typer .echo (typer .style (f"❌ Error: { str (e )} " , fg = typer .colors .RED ))
92+ raise typer .Exit (1 )
0 commit comments