@@ -250,57 +250,41 @@ def albums_main(xml_path: str, output_json: str, not_found_json: str):
250250 import argparse
251251 import sys
252252
253- # Check if the first argument is a subcommand
254- has_subcommand = len (sys .argv ) > 1 and sys .argv [1 ] in ['tracks' , 'albums' ]
253+ # Set up argument parser with subcommands
254+ parser = argparse .ArgumentParser (description = "Convert Apple Music Library.xml to Lidarr JSON import format with not-found items exported." )
255+ subparsers = parser .add_subparsers (dest = 'command' , help = 'Available commands' )
255256
256- if has_subcommand :
257- # Use subcommands
258- parser = argparse .ArgumentParser (description = "Convert Apple Music Library.xml to Lidarr JSON import format with not-found items exported." )
259- subparsers = parser .add_subparsers (dest = 'command' , help = 'Available commands' )
260-
261- # Tracks subcommand
262- tracks_parser = subparsers .add_parser ('tracks' , help = 'Process individual tracks (default)' )
263- tracks_parser .add_argument ("--recheck" , action = "store_true" ,
264- help = "Recheck mode: process items from not_found_json instead of parsing XML" )
265- tracks_parser .add_argument ("xml_file" , nargs = "?" , help = "Path to Apple Music Library.xml (not needed in recheck mode)" )
266- tracks_parser .add_argument ("output_json" , help = "Output JSON file path for found items" )
267- tracks_parser .add_argument ("not_found_json" , help = "Output JSON file path for not found items" )
268-
269- # Albums subcommand
270- albums_parser = subparsers .add_parser ('albums' , help = 'Process unique albums' )
271- albums_parser .add_argument ("xml_file" , help = "Path to Apple Music Library.xml" )
272- albums_parser .add_argument ("output_json" , help = "Output JSON file path for found albums (default: albums.json)" )
273- albums_parser .add_argument ("not_found_json" , help = "Output JSON file path for not found albums (default: albums_notfound.json)" )
274-
275- args = parser .parse_args ()
276-
277- # Handle subcommands
278- if args .command == 'albums' :
279- albums_main (args .xml_file , args .output_json , args .not_found_json )
280- elif args .command == 'tracks' :
281- if args .recheck :
282- if args .xml_file :
283- logging .warning ("XML file argument ignored in recheck mode" )
284- recheck_not_found (args .output_json , args .not_found_json )
285- else :
286- if not args .xml_file :
287- tracks_parser .error ("xml_file is required when not in recheck mode" )
288- main (args .xml_file , args .output_json , args .not_found_json )
289- else :
290- # Backward compatibility: use original argument structure
291- parser = argparse .ArgumentParser (description = "Convert Apple Music Library.xml to Lidarr JSON import format with not-found items exported." )
292- parser .add_argument ("--recheck" , action = "store_true" ,
293- help = "Recheck mode: process items from not_found_json instead of parsing XML" )
294- parser .add_argument ("xml_file" , nargs = "?" , help = "Path to Apple Music Library.xml (not needed in recheck mode)" )
295- parser .add_argument ("output_json" , help = "Output JSON file path for found items" )
296- parser .add_argument ("not_found_json" , help = "Output JSON file path for not found items" )
297- args = parser .parse_args ()
298-
257+ # Tracks subcommand
258+ tracks_parser = subparsers .add_parser ('tracks' , help = 'Process individual tracks (default)' )
259+ tracks_parser .add_argument ("--recheck" , action = "store_true" ,
260+ help = "Recheck mode: process items from not_found_json instead of parsing XML" )
261+ tracks_parser .add_argument ("xml_file" , nargs = "?" , help = "Path to Apple Music Library.xml (not needed in recheck mode)" )
262+ tracks_parser .add_argument ("output_json" , help = "Output JSON file path for found items" )
263+ tracks_parser .add_argument ("not_found_json" , help = "Output JSON file path for not found items" )
264+
265+ # Albums subcommand
266+ albums_parser = subparsers .add_parser ('albums' , help = 'Process unique albums' )
267+ albums_parser .add_argument ("xml_file" , help = "Path to Apple Music Library.xml" )
268+ albums_parser .add_argument ("output_json" , help = "Output JSON file path for found albums" )
269+ albums_parser .add_argument ("not_found_json" , help = "Output JSON file path for not found albums" )
270+
271+ # Parse arguments, defaulting to 'tracks' if no subcommand provided
272+ args = parser .parse_args ()
273+
274+ # If no subcommand was provided, show help
275+ if args .command is None :
276+ parser .print_help ()
277+ sys .exit (1 )
278+
279+ # Handle subcommands
280+ if args .command == 'albums' :
281+ albums_main (args .xml_file , args .output_json , args .not_found_json )
282+ elif args .command == 'tracks' :
299283 if args .recheck :
300284 if args .xml_file :
301285 logging .warning ("XML file argument ignored in recheck mode" )
302286 recheck_not_found (args .output_json , args .not_found_json )
303287 else :
304288 if not args .xml_file :
305- parser .error ("xml_file is required when not in recheck mode" )
289+ tracks_parser .error ("xml_file is required when not in recheck mode" )
306290 main (args .xml_file , args .output_json , args .not_found_json )
0 commit comments