1818from .cli import CLIHandler
1919from .interactive import InteractiveHandler
2020from .utils import load_json
21+ from .codegen .cli_integration import add_codegen_args , handle_codegen_command
2122from rich .console import Console
2223
2324
@@ -37,16 +38,23 @@ def load_data(self, file_path=None, url=None):
3738 self .source , self .data = load_json (file_path , url )
3839 return True
3940 except Exception as e :
40- self .console .print (f"⌛ [red]Error loading data: { e } [/red]" )
41+ self .console .print (f"❌ [red]Error loading data: { e } [/red]" )
4142 return False
4243
4344 def run (self , args ):
4445 """Main execution method."""
45- # Handle special commands that don't require data
46- if args .list_codegen_targets :
47- self .cli_handler .list_codegen_targets ()
48- return 0
49-
46+ # Handle codegen commands (which may not require data for --list-languages)
47+ if hasattr (args , "generate" ) or hasattr (args , "list_languages" ):
48+ if args .list_languages :
49+ # List languages doesn't need data
50+ return handle_codegen_command (args )
51+ elif args .generate :
52+ # Code generation needs data
53+ if not self .load_data (args .file , args .url ):
54+ return 1
55+ return handle_codegen_command (args )
56+
57+ # For other operations, load data first
5058 if not self .load_data (args .file , args .url ):
5159 return 1
5260
@@ -60,7 +68,7 @@ def run(self, args):
6068
6169 def _has_cli_actions (self , args ) -> bool :
6270 """Check if any CLI-specific actions are requested."""
63- return any ([args .tree , args .search , args .stats , args .plot , args . codegen ])
71+ return any ([args .tree , args .search , args .stats , args .plot ])
6472
6573
6674def create_parser ():
@@ -75,8 +83,12 @@ def create_parser():
7583 %(prog)s data.json --search "name" --search-type key
7684 %(prog)s data.json --search "isinstance(value, int) and value > 10" --search-type filter
7785 %(prog)s --url https://api.example.com/data --plot --tree-results
78- %(prog)s data.json --codegen java --codegen-root User --codegen-config "lombok=true,jackson=true"
79- %(prog)s data.json --codegen all --codegen-root ApiResponse
86+
87+ Code Generation:
88+ %(prog)s data.json --generate go --output user.go --root-name User
89+ %(prog)s data.json --generate go --package-name models --no-pointers
90+ %(prog)s data.json --generate go --config my-config.json
91+ %(prog)s --list-languages
8092 """ ,
8193 )
8294
@@ -157,29 +169,8 @@ def create_parser():
157169 help = "Don't open browser for HTML visualizations" ,
158170 )
159171
160- # Code generation options
161- codegen_group = parser .add_argument_group ("code generation options" )
162- codegen_group .add_argument (
163- "--codegen" ,
164- type = str ,
165- help = "Generate code for target language (java, python, typescript, go, rust, openapi, graphql, all)" ,
166- )
167- codegen_group .add_argument (
168- "--codegen-root" ,
169- type = str ,
170- default = "Root" ,
171- help = "Root class/type name for generated code (default: Root)" ,
172- )
173- codegen_group .add_argument (
174- "--codegen-config" ,
175- type = str ,
176- help = "Code generation configuration (key=value,key2=value2). Example: lombok=true,jackson=true,package=com.example" ,
177- )
178- codegen_group .add_argument (
179- "--list-codegen-targets" ,
180- action = "store_true" ,
181- help = "List all available code generation targets" ,
182- )
172+ # Add codegen arguments from the dedicated module
173+ add_codegen_args (parser )
183174
184175 return parser
185176
@@ -194,12 +185,20 @@ def main():
194185 return 1
195186
196187 # Handle special commands that don't need file/url
197- if args . list_codegen_targets :
188+ if hasattr ( args , "list_languages" ) and args . list_languages :
198189 explorer = JSONExplorer ()
199190 return explorer .run (args )
200191
201- if not (args .file or args .url ):
202- print ("⌛ Error: You must provide a file path or --url" )
192+ # For codegen, we need either file or url (unless just listing languages)
193+ if hasattr (args , "generate" ) and args .generate :
194+ if not (args .file or args .url ):
195+ print ("❌ Error: Code generation requires a file path or --url" )
196+ parser .print_help ()
197+ return 1
198+
199+ # For other operations, file or url is required
200+ if not hasattr (args , "generate" ) and not (args .file or args .url ):
201+ print ("❌ Error: You must provide a file path or --url" )
203202 parser .print_help ()
204203 return 1
205204
0 commit comments