package org.musetest.commandline;

import io.airlift.airline.*;
import org.musetest.core.commandline.*;
import org.reflections.*;

import javax.imageio.spi.*;
import java.util.*;

/**
 * @author Christopher L Merrill (see LICENSE.txt for license details)
 */
public class Launcher
    {
    @SuppressWarnings("unchecked")
    public static void main(String[] args)
        {
        // dynamically lookup the commands using Java's ServiceRegistry. This looks at the META-INF/service files in jars on the classpath.
        Iterator<MuseCommand> commands = ServiceRegistry.lookupProviders(MuseCommand.class);
        List<Class<? extends Runnable>> implementors = new ArrayList<>();
        while (commands.hasNext())
            implementors.add((commands.next().getClass()));

        Cli.CliBuilder<Runnable> builder = Cli.<Runnable>builder("muse")
            .withDescription("Muse command-line tools")
            .withDefaultCommand(Help.class)
            .withCommands(Help.class)
            .withCommands(implementors);
        Cli<Runnable> muse_parser = builder.build();

        try
            {
            muse_parser.parse(args).run();
            }
        catch (Exception e)
            {
            muse_parser.parse(new String[0]).run();
            }
        }

    static
        {
        Reflections.log = null;
        }
    }



@@ -1077,24 +1077,44 @@
 ild();%0A%0A
-        
+%09%09final Runnable command;%0A%09%09
 try%0A    
@@ -1119,35 +1119,36 @@
           %7B%0A
-           
+%09%09%09command =
  muse_parser
@@ -1159,22 +1159,16 @@
 se(args)
-.run()
 ;%0A      
@@ -1273,32 +1273,319 @@
 ();%0A            
+return;%0A            %7D%0A        try%0A            %7B%0A            command.run();%0A            %7D%0A        catch (Exception e)%0A            %7B%0A%09%09%09System.out.println(String.format(%22Command failed due to a %25s.%5Cn%25s%22, e.getClass().getSimpleName(), e.getMessage()));%0A%09%09%09e.printStackTrace(System.err);%0A%09%09%09
 %7D%0A        %7D%0A%0A   

Handle command-line parsing errors separately from command execution errors.