org.jcodec.common.tools.MainUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jcodec Show documentation
Show all versions of jcodec Show documentation
Pure Java implementation of video/audio codecs and formats
package org.jcodec.common.tools;
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jcodec.common.StringUtils;
import org.jcodec.platform.Platform;
/**
* This class is part of JCodec ( www.jcodec.org ) This software is distributed
* under FreeBSD License
*
* @author The JCodec project
*
*/
public class MainUtils {
private static final String JCODEC_LOG_SINK_COLOR = "jcodec.colorPrint";
public static boolean isColorSupported = System.console() != null
|| Boolean.parseBoolean(System.getProperty(JCODEC_LOG_SINK_COLOR));
public static enum FlagType {
VOID, STRING, INT, LONG, DOUBLE, MULT, ENUM, ANY
}
public static class Flag {
private String longName;
private String shortName;
private String description;
private FlagType type;
public Flag(String longName, String description) {
this(longName, null, description);
}
public Flag(String longName, String shortName, String description) {
this(longName, shortName, description, FlagType.ANY);
}
public Flag(String longName, String shortName, String description, FlagType type) {
this.longName = longName;
this.shortName = shortName;
this.description = description;
this.type = type;
}
public String getLongName() {
return longName;
}
public String getDescription() {
return description;
}
public String getShortName() {
return shortName;
}
public FlagType getType() {
return type;
}
}
public static class Cmd {
public Map longFlags;
public Map shortFlags;
public String[] args;
private Map[] longArgFlags;
private Map[] shortArgFlags;
public Cmd(Map longFlags, Map shortFlags, String[] args,
Map[] longArgFlags, Map[] shortArgFlags) {
this.args = args;
this.longFlags = longFlags;
this.shortFlags = shortFlags;
this.longArgFlags = longArgFlags;
this.shortArgFlags = shortArgFlags;
}
private Long getLongFlagInternal(Map longFlags, Map shortFlags, Flag flag,
Long defaultValue) {
return longFlags.containsKey(flag.getLongName()) ? new Long(longFlags.get(flag.getLongName()))
: (shortFlags.containsKey(flag.getShortName()) ? new Long(shortFlags.get(flag.getShortName()))
: defaultValue);
}
private Integer getIntegerFlagInternal(Map longFlags, Map shortFlags, Flag flag,
Integer defaultValue) {
return longFlags.containsKey(flag.getLongName()) ? new Integer(longFlags.get(flag.getLongName()))
: (shortFlags.containsKey(flag.getShortName()) ? new Integer(shortFlags.get(flag.getShortName()))
: defaultValue);
}
private Boolean getBooleanFlagInternal(Map longFlags, Map shortFlags, Flag flag,
Boolean defaultValue) {
return longFlags.containsKey(flag.getLongName())
? !"false".equalsIgnoreCase(longFlags.get(flag.getLongName()))
: (shortFlags.containsKey(flag.getShortName())
? !"false".equalsIgnoreCase(shortFlags.get(flag.getShortName())) : defaultValue);
}
private Double getDoubleFlagInternal(Map longFlags, Map shortFlags, Flag flag,
Double defaultValue) {
return longFlags.containsKey(flag.getLongName()) ? new Double(longFlags.get(flag.getLongName()))
: (shortFlags.containsKey(flag.getShortName()) ? new Double(shortFlags.get(flag.getShortName()))
: defaultValue);
}
private String getStringFlagInternal(Map longFlags, Map shortFlags, Flag flag,
String defaultValue) {
return longFlags.containsKey(flag.getLongName()) ? longFlags.get(flag.getLongName())
: (shortFlags.containsKey(flag.getShortName()) ? shortFlags.get(flag.getShortName())
: defaultValue);
}
private int[] getMultiIntegerFlagInternal(Map longFlags, Map shortFlags,
Flag flag, int[] defaultValue) {
String flagValue;
if (longFlags.containsKey(flag.getLongName()))
flagValue = longFlags.get(flag.getLongName());
else if (shortFlags.containsKey(flag.getShortName()))
flagValue = shortFlags.get(flag.getShortName());
else
return defaultValue;
String[] split = StringUtils.splitS(flagValue, ",");
int[] result = new int[split.length];
for (int i = 0; i < split.length; i++)
result[i] = Integer.parseInt(split[i]);
return result;
}
private > T getEnumFlagInternal(Map longFlags, Map shortFlags,
Flag flag, T defaultValue, Class class1) {
String flagValue;
if (longFlags.containsKey(flag.getLongName()))
flagValue = longFlags.get(flag.getLongName());
else if (shortFlags.containsKey(flag.getShortName()))
flagValue = shortFlags.get(flag.getShortName());
else
return defaultValue;
String strVal = flagValue.toLowerCase();
EnumSet allOf = EnumSet.allOf(class1);
for (T val : allOf) {
if (val.name().toLowerCase().equals(strVal))
return val;
}
return null;
}
public Long getLongFlagD(Flag flagName, Long defaultValue) {
return this.getLongFlagInternal(longFlags, shortFlags, flagName, defaultValue);
}
public Long getLongFlag(Flag flagName) {
return this.getLongFlagInternal(longFlags, shortFlags, flagName, null);
}
public Long getLongFlagID(int arg, Flag flagName, Long defaultValue) {
return this.getLongFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, defaultValue);
}
public Long getLongFlagI(int arg, Flag flagName) {
return this.getLongFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, null);
}
public Integer getIntegerFlagD(Flag flagName, Integer defaultValue) {
return getIntegerFlagInternal(longFlags, shortFlags, flagName, defaultValue);
}
public Integer getIntegerFlag(Flag flagName) {
return getIntegerFlagInternal(longFlags, shortFlags, flagName, null);
}
public Integer getIntegerFlagID(int arg, Flag flagName, Integer defaultValue) {
return getIntegerFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, defaultValue);
}
public Integer getIntegerFlagI(int arg, Flag flagName) {
return getIntegerFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, null);
}
public Boolean getBooleanFlagD(Flag flagName, Boolean defaultValue) {
return getBooleanFlagInternal(longFlags, shortFlags, flagName, defaultValue);
}
public Boolean getBooleanFlag(Flag flagName) {
return getBooleanFlagInternal(longFlags, shortFlags, flagName, false);
}
public Boolean getBooleanFlagID(int arg, Flag flagName, Boolean defaultValue) {
return getBooleanFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, defaultValue);
}
public Boolean getBooleanFlagI(int arg, Flag flagName) {
return getBooleanFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, false);
}
public Double getDoubleFlagD(Flag flagName, Double defaultValue) {
return getDoubleFlagInternal(longFlags, shortFlags, flagName, defaultValue);
}
public Double getDoubleFlag(Flag flagName) {
return getDoubleFlagInternal(longFlags, shortFlags, flagName, null);
}
public Double getDoubleFlagID(int arg, Flag flagName, Double defaultValue) {
return getDoubleFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, defaultValue);
}
public Double getDoubleFlagI(int arg, Flag flagName) {
return getDoubleFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, null);
}
public String getStringFlagD(Flag flagName, String defaultValue) {
return getStringFlagInternal(longFlags, shortFlags, flagName, defaultValue);
}
public String getStringFlag(Flag flagName) {
return getStringFlagInternal(longFlags, shortFlags, flagName, null);
}
public String getStringFlagID(int arg, Flag flagName, String defaultValue) {
return getStringFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, defaultValue);
}
public String getStringFlagI(int arg, Flag flagName) {
return getStringFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, null);
}
public int[] getMultiIntegerFlagD(Flag flagName, int[] defaultValue) {
return getMultiIntegerFlagInternal(longFlags, shortFlags, flagName, defaultValue);
}
public int[] getMultiIntegerFlag(Flag flagName) {
return getMultiIntegerFlagInternal(longFlags, shortFlags, flagName, new int[0]);
}
public int[] getMultiIntegerFlagID(int arg, Flag flagName, int[] defaultValue) {
return getMultiIntegerFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, defaultValue);
}
public int[] getMultiIntegerFlagI(int arg, Flag flagName) {
return getMultiIntegerFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, new int[0]);
}
public > T getEnumFlagD(Flag flagName, T defaultValue, Class class1) {
return getEnumFlagInternal(longFlags, shortFlags, flagName, defaultValue, class1);
}
public > T getEnumFlag(Flag flagName, Class class1) {
return getEnumFlagInternal(longFlags, shortFlags, flagName, null, class1);
}
public > T getEnumFlagID(int arg, Flag flagName, T defaultValue, Class class1) {
return getEnumFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, defaultValue, class1);
}
public > T getEnumFlagI(int arg, Flag flagName, Class class1) {
return getEnumFlagInternal(longArgFlags[arg], shortArgFlags[arg], flagName, null, class1);
}
public String getArg(int i) {
return i < args.length ? args[i] : null;
}
public int argsLength() {
return args.length;
}
public void popArg() {
args = Platform.copyOfRangeO(args, 1, args.length);
}
}
private static Pattern flagPattern = Pattern.compile("^--([^=]+)=(.*)$");
public static Cmd parseArguments(String[] args, Flag[] flags) {
Map longFlags = new HashMap();
Map shortFlags = new HashMap();
Map allLongFlags = new HashMap();
Map allShortFlags = new HashMap();
List outArgs = new ArrayList();
List