X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Fj2ee%2FPosixArgs.java;fp=src%2Fdolda%2Fjsvc%2Fj2ee%2FPosixArgs.java;h=4a23941f2c97c56153912c8924ec37be5e4fab54;hb=f179d8ba3acb55d1419a9de851d0fcf996798cc4;hp=0000000000000000000000000000000000000000;hpb=78f5d1201f8d3aecb660b7877b08d9bfbe650911;p=jsvc.git diff --git a/src/dolda/jsvc/j2ee/PosixArgs.java b/src/dolda/jsvc/j2ee/PosixArgs.java new file mode 100644 index 0000000..4a23941 --- /dev/null +++ b/src/dolda/jsvc/j2ee/PosixArgs.java @@ -0,0 +1,100 @@ +package dolda.jsvc.j2ee; + +import java.util.*; + +public class PosixArgs { + private List parsed; + public String[] rest; + public String arg = null; + + private static class Arg { + private char ch; + private String arg; + + private Arg(char ch, String arg) { + this.ch = ch; + this.arg = arg; + } + } + + private PosixArgs() { + parsed = new ArrayList(); + } + + public static PosixArgs getopt(String[] argv, int start, String desc) { + PosixArgs ret = new PosixArgs(); + List fl = new ArrayList(), fla = new ArrayList(); + List rest = new ArrayList(); + for(int i = 0; i < desc.length();) { + char ch = desc.charAt(i++); + if((i < desc.length()) && (desc.charAt(i) == ':')) { + i++; + fla.add(ch); + } else { + fl.add(ch); + } + } + boolean acc = true; + for(int i = start; i < argv.length;) { + String arg = argv[i++]; + if(acc && arg.equals("--")) { + acc = false; + } if(acc && (arg.charAt(0) == '-')) { + for(int o = 1; o < arg.length();) { + char ch = arg.charAt(o++); + if(fl.contains(ch)) { + ret.parsed.add(new Arg(ch, null)); + } else if(fla.contains(ch)) { + if(o < arg.length()) { + ret.parsed.add(new Arg(ch, arg.substring(o))); + break; + } else if(i < argv.length) { + ret.parsed.add(new Arg(ch, argv[i++])); + break; + } else { + System.err.println("option requires an argument -- '" + ch + "'"); + return(null); + } + } else { + System.err.println("invalid option -- '" + ch + "'"); + return(null); + } + } + } else { + rest.add(arg); + } + } + ret.rest = rest.toArray(new String[0]); + return(ret); + } + + public static PosixArgs getopt(String[] argv, String desc) { + return(getopt(argv, 0, desc)); + } + + public Iterable parsed() { + return(new Iterable() { + public Iterator iterator() { + return(new Iterator() { + private int i = 0; + + public boolean hasNext() { + return(i < parsed.size()); + } + + public Character next() { + if(i >= parsed.size()) + throw(new NoSuchElementException()); + Arg a = parsed.get(i++); + arg = a.arg; + return(a.ch); + } + + public void remove() { + throw(new UnsupportedOperationException()); + } + }); + } + }); + } +}