Navigation Menu

Skip to content

Commit

Permalink
fix parsing options
Browse files Browse the repository at this point in the history
  • Loading branch information
ceteri committed May 4, 2012
1 parent 49800db commit b306b01
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 34 deletions.
4 changes: 2 additions & 2 deletions build.xml
Expand Up @@ -21,7 +21,7 @@
~ along with Cascading. If not, see <http://www.gnu.org/licenses/>.
-->

<project name="multitool" default="compile" basedir="." xmlns:ivy="antlib:org.apache.ivy.ant">
<project name="multitool" default="retrieve" basedir="." xmlns:ivy="antlib:org.apache.ivy.ant">

<dirname property="multitool.basedir" file="${ant.file.multitool}"/>

Expand Down Expand Up @@ -241,7 +241,7 @@

<target name="publish" depends="clean,retrieve,test,tar,s3-package"/>

<target name="retrieve">
<target name="retrieve" description="use Ivy to download dependencies">
<ivy:settings id="ivy.conjars.settings" url="http://conjars.org/repo/ivysettings.xml"/>

<ivy:retrieve file="${multitool.basedir}/ivy.xml" settingsRef="ivy.conjars.settings" conf="default"
Expand Down
96 changes: 64 additions & 32 deletions src/java/multitool/Main.java
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.String;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -95,14 +96,27 @@ public class Main
new UniqueFactory( "unique" )};

static Map<String, Factory> factoryMap = new HashMap<String, Factory>();
static Map<String, Option> optionMap = new HashMap<String, Option>();

static
{
optionMap.put( "-h", new Option( "-h", false, null ) );
optionMap.put( "--help", new Option( "--help", false, null ) );
optionMap.put( "-t", new Option( "-t", false, null ) );
optionMap.put( "--text", new Option( "--text", false, null ) );
optionMap.put( "--dot", new Option( "--dot", true, null ) );

for( Factory factory : TAP_FACTORIES )
factoryMap.put( factory.getAlias(), factory );
{
factoryMap.put( factory.getAlias(), factory );
optionMap.put( factory.getAlias(), new Option( factory.getAlias(), true, factory ) );
}

for( Factory factory : PIPE_FACTORIES )
factoryMap.put( factory.getAlias(), factory );
{
factoryMap.put( factory.getAlias(), factory );
optionMap.put( factory.getAlias(), new Option( factory.getAlias(), true, factory ) );
}
}

private Map<String, String> options;
Expand All @@ -115,21 +129,29 @@ public static void main( String[] args )

for( String arg : args )
{
int index = arg.indexOf( "=" );
String arg_name = arg;
String arg_verb = arg;
String arg_data = null;

int dot_index = arg.indexOf( "." );
int equals_index = arg.indexOf( "=" );

if( arg.startsWith( "-" ) )
if( dot_index != -1 )
arg_name = arg.substring( 0, dot_index );

if( equals_index != -1 )
{
if( index != -1 )
options.put( arg.substring( 0, index ), arg.substring( index + 1 ) );
else
options.put( arg, null );
arg_name = arg.substring( 0, equals_index );
arg_verb = arg.substring( 0, equals_index );
arg_data = arg.substring( equals_index + 1 );
}
else

if( optionMap.keySet().contains( arg_name ) )
{
if( index != -1 )
params.add( new String[]{arg.substring( 0, index ), arg.substring( index + 1 )} );
if( arg.startsWith( "-" ) )
options.put( arg_verb, arg_data );
else
params.add( new String[]{arg, null} );
params.add( new String[]{ arg_verb, arg_data } );
}
}

Expand All @@ -142,7 +164,6 @@ public static void main( String[] args )
System.out.println( exception.getMessage() );
printUsage();
}

}

private static void printUsage()
Expand All @@ -155,11 +176,12 @@ private static void printUsage()
System.out.println( "" );
System.out.println( "Usage:" );

System.out.println( "options:" );
System.out.println( String.format( " %-25s %s", "-dot=<file>", "filename to write a plan DOT file then exit" ) );
System.out.println( "taps:" );
System.out.println( "\n options:" );
System.out.println( "-h" );
System.out.println( String.format( " %-25s %s", "--dot=<file>", "filename to write a plan DOT file then exit" ) );
System.out.println( "\n taps:" );
printFactoryUsage( TAP_FACTORIES );
System.out.println( "operations:" );
System.out.println( "\n operations:" );
printFactoryUsage( PIPE_FACTORIES );

System.exit( 1 );
Expand Down Expand Up @@ -203,7 +225,7 @@ private static void printLicense()
{
try
{
InputStream stream = Main.class.getResourceAsStream( "/MULTITOOL-LICENSE.txt" );
InputStream stream = Main.class.getResourceAsStream( "/LICENSE.txt" );
BufferedReader reader = new BufferedReader( new InputStreamReader( stream ) );

System.out.print( "This release is licensed under the " );
Expand Down Expand Up @@ -252,14 +274,12 @@ public Main( Map<String, String> options, List<String[]> params )

this.params = params;

validateParams();
if( this.params.size() > 0 )
validateParams();
}

private void validateParams()
{
if( params.size() == 0 )
throw new IllegalArgumentException( "error: no args given" );

for( String[] param : params )
{
String alias = param[ 0 ].replaceFirst( "^([^.]+).*$", "$1" );
Expand Down Expand Up @@ -313,26 +333,38 @@ private int getNumTaskTrackers()

public void execute()
{
if(( this.params.size() == 0 ) || options.containsKey("-h") || options.containsKey("--help") )
{
printUsage();
return;
}

String dot_key = "--dot";

try
{
Flow flow = plan( getDefaultProperties() );

if( options.containsKey( "-dot" ) )
if( options.containsKey( dot_key ) )
{
flow.writeDOT( options.get( "-dot" ) );
System.out.println( "wrote DOT file to: " + options.get( "-dot" ) );
String dot_file = options.get( dot_key );
flow.writeDOT( dot_file );
System.out.println( "wrote DOT file to: " + dot_file );
System.out.println( "exiting" );
return;
}

flow.complete();
else
{
flow.complete();
}
}
catch( PlannerException exception )
{
if( options.containsKey( "-dot" ) )
if( options.containsKey( dot_key ) )
{
exception.writeDOT( options.get( "-dot" ) );
System.out.println( "wrote DOT file to: " + options.get( "-dot" ) );
String dot_file = options.get( dot_key );

exception.writeDOT( dot_file );
System.out.println( "wrote DOT file to: " + dot_file );
}

throw exception;
Expand Down Expand Up @@ -378,7 +410,7 @@ else if( factory instanceof SinkFactory )
}

if( sources.isEmpty() )
throw new IllegalArgumentException( "error: must have atleast one source" );
throw new IllegalArgumentException( "error: must have at least one source" );

if( sinks.isEmpty() )
throw new IllegalArgumentException( "error: must have one sink" );
Expand Down
41 changes: 41 additions & 0 deletions src/java/multitool/Option.java
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2007-2009 Concurrent, Inc. All Rights Reserved.
*
* Project and contact information: http://www.cascading.org/
*
* This file is part of the Cascading project.
*
* Cascading is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cascading is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cascading. If not, see <http://www.gnu.org/licenses/>.
*/

package multitool;

import multitool.factory.Factory;

/**
*
*/
public class Option
{
String name = null;
boolean needs_data = false;
Factory factory = null;

public Option (String name, boolean needs_data, Factory factory )
{
this.name = name;
this.needs_data = needs_data;
this.factory = factory;
}
}

0 comments on commit b306b01

Please sign in to comment.