| 1 | /* |
| 2 | Copyright (C) 2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License version 2 as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | |
| 17 | */ |
| 18 | package com.mysql.management.util; |
| 19 | |
| 20 | import java.io.File; |
| 21 | import java.util.Arrays; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import com.mysql.management.MysqldResourceI; |
| 27 | |
| 28 | /** |
| 29 | * This class is final simply as a hint to the compiler, it may be un-finalized |
| 30 | * safely. |
| 31 | * |
| 32 | * @author Eric Herman <eric@mysql.com> |
| 33 | * @version $Id: CommandLineOptionsParser.java,v 1.1 2005/02/17 21:20:45 eherman |
| 34 | * Exp $ |
| 35 | */ |
| 36 | public final class CommandLineOptionsParser { |
| 37 | |
| 38 | private Map params; |
| 39 | |
| 40 | private Files fileUtil; |
| 41 | |
| 42 | public CommandLineOptionsParser(String[] args) { |
| 43 | this(Arrays.asList(args)); |
| 44 | } |
| 45 | |
| 46 | public CommandLineOptionsParser(List args) { |
| 47 | this.params = new HashMap(); |
| 48 | this.fileUtil = new Files(); |
| 49 | |
| 50 | for (int i = 0; i < args.size(); i++) { |
| 51 | String arg = (String) args.get(i); |
| 52 | if (arg.startsWith("--")) { |
| 53 | arg = arg.substring(2); |
| 54 | } |
| 55 | int equalsPos = arg.indexOf("="); |
| 56 | if (equalsPos == -1) { |
| 57 | equalsPos = arg.length(); |
| 58 | } |
| 59 | String key = arg.substring(0, equalsPos).trim(); |
| 60 | String value = null; |
| 61 | if (arg.length() > equalsPos) { |
| 62 | value = arg.substring(equalsPos + 1, arg.length()).trim(); |
| 63 | } |
| 64 | |
| 65 | params.put(key, value); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public boolean containsKey(String key) { |
| 70 | return asMap().containsKey(key); |
| 71 | } |
| 72 | |
| 73 | public Object remove(String key) { |
| 74 | return asMap().remove(key); |
| 75 | } |
| 76 | |
| 77 | public Map asMap() { |
| 78 | return params; |
| 79 | } |
| 80 | |
| 81 | public File getBaseDir() { |
| 82 | return newFile(MysqldResourceI.BASEDIR); |
| 83 | } |
| 84 | |
| 85 | public File getDataDir() { |
| 86 | return newFile(MysqldResourceI.DATADIR); |
| 87 | } |
| 88 | |
| 89 | private File newFile(String key) { |
| 90 | if (!params.containsKey(key)) |
| 91 | return fileUtil.nullFile(); |
| 92 | return fileUtil.newFile(params.get(key)); |
| 93 | } |
| 94 | |
| 95 | public String getVersion() { |
| 96 | return (String) params.get(MysqldResourceI.MYSQLD_VERSION); |
| 97 | } |
| 98 | |
| 99 | public boolean isShutdown() { |
| 100 | return containsKey("shutdown"); |
| 101 | } |
| 102 | } |