All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testtoolinterfaces.cmdline.CmdLineAbstractParser Maven / Gradle / Ivy

package org.testtoolinterfaces.cmdline;

import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.testtoolinterfaces.utils.RunTimeData;
import org.testtoolinterfaces.utils.RunTimeVariable;

import joptsimple.ArgumentAcceptingOptionSpec;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpecBuilder;

public abstract class CmdLineAbstractParser implements CmdLineParser
{
	private String myApplicationName;
	private HashMap myAllowedCommands;
	private ArrayList myAllowedOptions;
	private ArrayList myAllowedFlags;
	private OptionParser myOptionParser;
	
	private ArgumentAcceptingOptionSpec myCommandOptionSpec;

	public CmdLineAbstractParser( String anApplicationName )
	{
		myApplicationName = anApplicationName;
		myAllowedCommands = new HashMap();

		myAllowedOptions = new ArrayList();
		myAllowedFlags = new ArrayList();
		myOptionParser = new OptionParser();
		
//		this.acceptOption(HELP, "Prints a usage and short help");
		this.acceptFlag(HELP, "Prints a usage and short help");
		
		myCommandOptionSpec = this.acceptOption(COMMAND, "The action to perform")
			.withRequiredArg().ofType( String.class ).describedAs("Command");
	}

	public void parse(RunTimeData aRtData, String... anArgs) throws ParameterException
	{
		OptionSet optionSet = myOptionParser.parse( anArgs );
		String command = (String) optionSet.valueOf( COMMAND );
		if ( command != null && ! myAllowedCommands.containsKey( command ) )
		{
			throw new ParameterException( "Unknown command: " + command );
		}

//		if (optionSet.has( HELP ))
//		{
//			RunTimeVariable optionVar = new RunTimeVariable(HELP, true);
//			aRtData.add(optionVar);
//		}
//		
		for ( String option : myAllowedOptions )
		{
			Object value = optionSet.valueOf(option);
			if ( value != null )
			{
				RunTimeVariable optionVar = new RunTimeVariable(option, value);
				aRtData.add(optionVar);
			}
		}

		for ( String flag : myAllowedFlags )
		{
			if ( optionSet.has(flag) )
			{
				RunTimeVariable flagVar = new RunTimeVariable(flag, true);
				aRtData.add(flagVar);
			}
		}
	}

	public void printHelpOn( PrintStream aStream )
	{
		// TODO Improve format of help
		aStream.println( "Usage:" );
		aStream.println( myApplicationName + " []" );
		aStream.println( "" );
		aStream.println( "Where 'options' is one or more of:" );

		try
		{
			myOptionParser.printHelpOn( aStream );
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public OptionSpecBuilder acceptOption(String anOption)
	{
		myAllowedOptions.add(anOption);
		return myOptionParser.accepts(anOption);
	}

	public OptionSpecBuilder acceptOption(String anOption, String aDescription)
	{
		myAllowedOptions.add(anOption);
		return myOptionParser.accepts(anOption, aDescription);
	}

	public OptionSpecBuilder acceptOptions(ArrayList anOptions)
	{
		myAllowedOptions.addAll(anOptions);
		return myOptionParser.acceptsAll(anOptions);
	}

	public OptionSpecBuilder acceptOptions(ArrayList anOptions, String aDescription)
	{
		myAllowedOptions.addAll(anOptions);
		return myOptionParser.acceptsAll(anOptions, aDescription);
	}

	public OptionSpecBuilder acceptFlag(String aFlag)
	{
		myAllowedFlags.add(aFlag);
		return myOptionParser.accepts(aFlag);
	}

	public OptionSpecBuilder acceptFlag(String aFlag, String aDescription)
	{
		myAllowedFlags.add(aFlag);
		return myOptionParser.accepts(aFlag, aDescription);
	}

	public void acceptCommand(String aCommand)
	{
		myAllowedCommands.put(aCommand, "");		
	}

	public void acceptCommand(String aCommand, String aDescription)
	{
		myAllowedCommands.put(aCommand, aDescription);		
	}
	
	public void setDefaultCommand( String aCommand )
	{
		if ( ! myAllowedCommands.containsKey(aCommand) )
		{
			this.acceptCommand(aCommand);
		}
		this.myCommandOptionSpec.defaultsTo( aCommand );
	}

	public void setDefaultCommand( String aCommand, String aDescription )
	{
		if ( ! myAllowedCommands.containsKey(aCommand) )
		{
			this.acceptCommand(aCommand, aDescription);
		}
		this.myCommandOptionSpec.defaultsTo( aCommand );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy