Package lejos.pc.tools

Source Code of lejos.pc.tools.NXJBrowserCommandLineParser

package lejos.pc.tools;

import java.io.PrintWriter;
import java.io.StringWriter;

import js.tinyvm.TinyVMException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
* CommandLineParser
*/
public class NXJBrowserCommandLineParser
{
   /**
    * Parse commandline.
    *
    * @param args command line
    * @throws TinyVMException
    */
   public CommandLine parse (String[] args) throws TinyVMException
   {
      assert args != null: "Precondition: args != null";

      Options options = new Options();
      options.addOption("h", "help", false, "help");
      options.addOption("b", "bluetooth", false,
      "use bluetooth");
      options.addOption("u", "usb", false,
      "use usb");
     
      Option nameOption = new Option("n", "name", true,
      "look for named NXT");
      nameOption.setArgName("name");
      options.addOption(nameOption);
     
      Option addressOption = new Option("d", "address", true,
     "look for NXT with given address");
      addressOption.setArgName("address");
      options.addOption(addressOption);
     
      CommandLine result;
      try
      {
         try
         {
            result = new GnuParser().parse(options, args);
         }
         catch (ParseException e)
         {
            throw new TinyVMException(e.getMessage(), e);
         }

         if (result.hasOption("h"))
         {
            throw new TinyVMException("Help:");
         }

      }
      catch (TinyVMException e)
      {
         StringWriter writer = new StringWriter();
         PrintWriter printWriter = new PrintWriter(writer);
         printWriter.println(e.getMessage());
        
         String commandName = System.getProperty("COMMAND_NAME", "java lejos.pc.tools.NXJBrowser");

         String usage = commandName + " [options]";
         new HelpFormatter().printHelp(printWriter, 80, usage.toString(), null,
            options, 0, 2, null);

         throw new TinyVMException(writer.toString());
      }

      assert result != null: "Postconditon: result != null";
      return result;
   }
}
TOP

Related Classes of lejos.pc.tools.NXJBrowserCommandLineParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.