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

org.extendj.ast.Options Maven / Gradle / Ivy

There is a newer version: 8.1.2
Show newest version
package org.extendj.ast;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.ArrayList;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.zip.*;
import java.io.*;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import org.jastadd.util.PrettyPrintable;
import org.jastadd.util.PrettyPrinter;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.jastadd.util.*;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import beaver.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
/**
 * @ast class
 * @declaredat /home/jesper/git/extendj/java4/frontend/Options.jadd:45
 */
public class Options extends java.lang.Object {
  public  Collection files() {
    return files;
  }

  public  void initOptions() {
    options.clear();
    optionDescriptions.clear();
    files.clear();
  }

  public  void addKeyOption(String name) {
    if (optionDescriptions.containsKey(name)) {
      throw new Error("Command line definition error: option description for "
          + name + " is multiply declared");
    }
    optionDescriptions.put(name, new Option(name, false, false));
  }

  public  void addKeyValueOption(String name) {
    if (optionDescriptions.containsKey(name)) {
      throw new Error("Command line definition error: option description for "
          + name + " is multiply declared");
    }
    optionDescriptions.put(name, new Option(name, true, false));
  }

  public  void addKeyCollectionOption(String name) {
    if (optionDescriptions.containsKey(name)) {
      throw new Error("Command line definition error: option description for "
          + name + " is multiply declared");
    }
    optionDescriptions.put(name, new Option(name, true, true));
  }

  public  void addOptionDescription(String name, boolean value) {
    if (optionDescriptions.containsKey(name)) {
      throw new Error("Command line definition error: option description for "
          + name + " is multiply declared");
    }
    optionDescriptions.put(name, new Option(name, value, false));
  }

  public  void addOptionDescription(String name, boolean value, boolean isCollection) {
    if (optionDescriptions.containsKey(name)) {
      throw new Error("Command line definition error: option description for "
          + name + " is multiply declared");
    }
    optionDescriptions.put(name, new Option(name, value, isCollection));
  }

  public  void addOptions(String[] args) {
    java.util.List argList = new ArrayList();

    // Expand argument files.
    for (int i = 0; i < args.length; i++) {
      String arg = args[i];
      if (arg.length() > 1 && arg.startsWith("@")) {
        if (arg.startsWith("@@")) {
          // Escape the double at.
          argList.add(arg.substring(1));
        } else {
          String fileName = arg.substring(1);
          try {
            java.io.StreamTokenizer tokenizer = new java.io.StreamTokenizer(
                new java.io.FileReader(fileName));
            tokenizer.resetSyntax();
            tokenizer.whitespaceChars(' ',' ');
            tokenizer.whitespaceChars('\t','\t');
            tokenizer.whitespaceChars('\f','\f');
            tokenizer.whitespaceChars('\n','\n');
            tokenizer.whitespaceChars('\r','\r');
            tokenizer.wordChars(33,255);
            tokenizer.commentChar('#');
            tokenizer.quoteChar('"');
            tokenizer.quoteChar('\'');
            while (tokenizer.nextToken() != tokenizer.TT_EOF) {
              argList.add(tokenizer.sval);
            }
          } catch (java.io.FileNotFoundException e) {
            System.err.println("Argument file not found: " + fileName);
          } catch (java.io.IOException e) {
            System.err.println("Exception: "+e.getMessage());
          }
        }
      } else {
        argList.add(arg);
      }
    }

    Iterator all = argList.iterator();
    while (all.hasNext()) {
      String arg = all.next();
      if (arg.startsWith("-")) {
        if (!optionDescriptions.containsKey(arg)) {
          throw new Error("Command line argument error: option " + arg + " is not defined");
        }
        Option o = (Option) optionDescriptions.get(arg);

        if (!o.isCollection && options.containsKey(arg)) {
          throw new Error("Command line argument error: option " + arg + " is multiply defined");
        }

        if (o.hasValue) {
          String value = null;
          if (!all.hasNext()) {
            throw new Error("Command line argument error: value missing for key " + arg);
          }
          value = all.next();
          if (value.startsWith("-")) {
            throw new Error("Command line argument error: expected value for key " + arg
                + ", but found option " + value);
          }

          if (o.isCollection) {
            Collection c = (Collection) options.get(arg);
            if (c == null) {
              c = new ArrayList();
            }
            c.add(value);
            options.put(arg, c);
          } else {
            options.put(arg, value);
          }
        } else {
          options.put(arg, null);
        }
      } else {
        files.add(arg);
      }
    }
  }

  public  boolean hasOption(String name) {
    return options.containsKey(name);
  }

  public  void setOption(String name) {
    options.put(name, null);
  }

  public  boolean hasValueForOption(String name) {
    return options.containsKey(name) && options.get(name) != null;
  }

  public  String getValueForOption(String name) {
    if (!hasValueForOption(name)) {
      throw new Error("Command line argument error: key " + name + " does not have a value");
    }
    return (String) options.get(name);
  }

  public  void setValueForOption(String value, String option) {
    options.put(option, value);
  }

  public  Collection getValueCollectionForOption(String name) {
    if (!hasValueForOption(name)) {
      throw new Error("Command line argument error: key " + name + " does not have a value");
    }
    return (Collection) options.get(name);
  }

  public  boolean verbose() {
    return hasOption("-verbose");
  }

  static  class Option {
    public String name;

    public boolean hasValue;

    public boolean isCollection;

    public Option(String name, boolean hasValue, boolean isCollection) {
      this.name = name;
      this.hasValue = hasValue;
      this.isCollection = isCollection;
    }
  }

  private final  Map options = new HashMap();

  private final  Map optionDescriptions = new HashMap();

  private final  Set files = new LinkedHashSet();


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy