MATSIM
ArgumentParser.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ArgumentParser.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.misc;
22 
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 
56 public class ArgumentParser implements Iterable<String> {
57 
58  private List<String> args = null;
59  private boolean enableShortOptions = true;
60 
67  public ArgumentParser(final String[] args) {
68  this(args, false);
69  }
70 
79  public ArgumentParser(final String[] args, final boolean enableShortOptions) {
80  this.args = new ArrayList<String>();
81  this.enableShortOptions = enableShortOptions;
82  parse(args);
83  }
84 
85 
89  @Override
90  public Iterator<String> iterator() {
91  return this.args.iterator();
92  }
93 
94 
95  private void parse(final String[] args) {
96  if (this.enableShortOptions) {
97  for (String arg : args) {
98  if (arg.startsWith("--")) {
99  parseLongOption(arg.substring(2));
100  } else if (arg.startsWith("-")) {
101  parseShortOption(arg.substring(1));
102  } else {
103  this.args.add(arg);
104  }
105  }
106  } else {
107  for (String arg : args) {
108  if (arg.startsWith("-")) {
109  parseOption(arg);
110  } else {
111  this.args.add(arg);
112  }
113  }
114  }
115  }
116 
117  private void parseShortOption(final String arg) {
118  if (arg.length() == 0) {
119  this.args.add("-");
120  return;
121  }
122  for (int i = 0; i < arg.length(); i++) {
123  char ch = arg.charAt(i);
124  if (ch == '=') {
125  if ((i == 0) && (arg.length() == 1)) {
126  // '=' is the only char
127  this.args.add("-=");
128  return;
129  } else if (i == 0) {
130  // arg is a string in the form of '-=*', for me this makes only sense as a string-param, so just add it
131  this.args.add('-' + arg);
132  return;
133  } else {
134  this.args.add(arg.substring(i+1));
135  return;
136  }
137  }
138  // else...
139  this.args.add("-" + ch);
140  }
141  }
142 
143  private void parseLongOption(final String arg) {
144  StringBuilder argname = new StringBuilder("--");
145  for (int i = 0; i < arg.length(); i++) {
146  char ch = arg.charAt(i);
147  if (ch == '=') {
148  if ((i == 0) && (arg.length() == 1)) {
149  // '=' is the only char
150  this.args.add("--=");
151  return;
152  } else if (i == 0) {
153  // arg is a string in the form of '--=*', interpret it as a string argument
154  this.args.add("--" + arg);
155  return;
156  } else {
157  this.args.add(argname.toString());
158  this.args.add(arg.substring(i + 1));
159  return;
160  }
161  }
162  // else...
163  argname.append(ch);
164  }
165  this.args.add(argname.toString());
166  }
167 
168  private void parseOption(final String arg) {
169  StringBuilder argname = new StringBuilder();
170  argname.append('-'); // every option starts with '-'
171  for (int i = 1; i < arg.length(); i++) { // options always start with '-', so we can start at char 1
172  char ch = arg.charAt(i);
173  if (ch == '=') {
174  if (argname.toString().equals("-")) {
175  // arg is a string in the form of '-=*', interpret it as a strig argument
176  this.args.add(arg);
177  return;
178  }
179  if (argname.toString().equals("--")) {
180  // arg is a string in the form of '--=*', interpret it as a strig argument
181  this.args.add(arg);
182  return;
183  }
184  // it seems there are already some real characters before '=' that make a correct argument
185  this.args.add(argname.toString());
186  this.args.add(arg.substring(i + 1));
187  return;
188  }
189  // else...
190  argname.append(ch);
191  }
192  this.args.add(argname.toString());
193  }
194 
195 }
ArgumentParser(final String[] args, final boolean enableShortOptions)