Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.compatibility.common.util;
     18 
     19 import java.util.HashMap;
     20 
     21 /**
     22  * Parses an array of arguments into a HashMap.
     23  *
     24  * This class assumed the arguments are in the form "-<key> <value> ..."
     25  */
     26 public class KeyValueArgsParser {
     27 
     28     private KeyValueArgsParser() {}
     29 
     30     public static HashMap<String, String> parse(String[] args) {
     31         final HashMap<String, String> map = new HashMap<String, String>();
     32         String key = null;
     33         for (String s : args) {
     34             if (key == null) {
     35                 if (!s.startsWith("-")) {
     36                     throw new RuntimeException("Invalid Key: " + s);
     37                 }
     38                 key = s;
     39             } else {
     40                 map.put(key, s);
     41                 key = null;
     42             }
     43         }
     44         if (key != null) {
     45             throw new RuntimeException("Left over key");
     46         }
     47         return map;
     48     }
     49 }
     50