john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

PropertiesReaderCLI

// 2012-11-29 johnpfeiffer

package net.kittyandbear;

import java.io.IOException;
import java.io.PrintStream;

public class PropertiesReaderCLI {
    public static final String CORRECTUSAGE = "usage: java -jar PropertiesReaderGetValueCLI-" + PropertiesReader.CLASSVERSION
            + " pathAndFilename.properties , OR FOR A SINGLE KEY-VALUE: java -jar PropertiesReaderGetValueCLI-"
            + PropertiesReader.CLASSVERSION + " pathAndFilename.properties KEY";

    static public void main( String args[] ) {
        if (argsAreInvalid( args )) {
            System.out.println( "ERROR: " + args.length + " Incorrect number of parameters, " + CORRECTUSAGE );
            System.exit( 1 );
        }
        if (args.length == 1) {
            displayKeyValueMapping( args[0] , System.out );
        }
        else if (args.length == 2) {
            displayKeyValue( args[0], args[1] , System.out );
        }
        else {
            System.out.println( "ERROR: Incorrect number of parameters, " + CORRECTUSAGE );
            System.exit( 1 );
        }
    }

    private static boolean argsAreInvalid( String args[] ) {
        boolean areInvalid = true;
        if (args != null && args.length > 0 & args.length < 3) {
            areInvalid = false;
        }
        return ( areInvalid );
    }

    private static void displayKeyValue( String pathAndFilename, String key , PrintStream out ) {
        try {
            PropertiesReader r = new PropertiesReader( pathAndFilename );
            String value = r.getPropertyValue( key );
            if (value == null) {
                out.println( "ERROR: key " + key + " not found in " + pathAndFilename );
            }
            else {
                out.println( r.getPropertyValue( key ) );
            }
        }
        catch( IllegalArgumentException e ) {
            out.println( "ERROR: IllegalArgumentException " + e.getMessage() );
        }
        catch( IOException e ) {
            out.println( "ERROR: IOException " + e.getMessage() );
        }
    }

    private static void displayKeyValueMapping( String pathAndFilename, PrintStream out ) {
        try {
            PropertiesReader r = new PropertiesReader( pathAndFilename );
            out.println( r.getKeysAndValuesSortedByKeysString() );
        }
        catch( IOException e ) {
            out.println( "ERROR: IOException " + e.getMessage() );
        }
    }

} // end class

  • « PropertiesReaderTest
  • PropertiesReaderCLI pom.xml »

Published

Dec 4, 2012

Category

java-classes

~172 words

Tags

  • classes 92
  • java 252
  • propertiesreadercli 2