NHMLFixup v10
[jpcrr.git] / org / jpc / ArgProcessor.java
blobd0502750a43b30caf8763ef9010b717ea93584cd
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009 H. Ilari Liusvaara
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Based on JPC x86 PC Hardware emulator,
22 A project from the Physics Dept, The University of Oxford
24 Details about original JPC can be found at:
26 www-jpc.physics.ox.ac.uk
30 package org.jpc;
32 /**
33 * Provides simple command line parsing for the various frontends to the
34 * emulator.
35 * @author Rhys Newman
37 public class ArgProcessor
39 /**
40 * Finds the value of the variable <code>key</code>. Searches the given
41 * commandline for <code>-key value</code>
42 * @param args array of strings to search
43 * @param key key to search for
44 * @param defaultValue value returned on failure
45 * @return result, or <code>defaultValue</code> on failure
47 public static String findVariable(String[] args, String key, String defaultValue)
49 int keyIndex = findKey(args, key);
50 if(keyIndex < 0)
51 return defaultValue;
53 if((keyIndex + 1) < args.length)
54 return args[keyIndex + 1];
55 else
56 return defaultValue;
59 /**
60 * Searches for the presence of the given flag on the command line as
61 * <code>-flag</code>
62 * @param args array of strings to search
63 * @param flag parameter to search for
64 * @return true if flag is found
66 public static boolean findFlag(String[] args, String flag)
68 return findKey(args, flag) >= 0;
71 private static int findKey(String[] args, String key)
73 if(key.startsWith("-"))
74 key = key.substring(1);
76 for(int i=0; i<args.length; i++) {
77 if(!args[i].startsWith("-"))
78 continue;
80 if(args[i].substring(1).equalsIgnoreCase(key))
81 return i;
84 return -1;
87 private ArgProcessor()