[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | package org.apache.hadoop.fs.shell; |
---|
| 19 | |
---|
| 20 | import java.util.ArrayList; |
---|
| 21 | import java.util.HashMap; |
---|
| 22 | import java.util.List; |
---|
| 23 | import java.util.Map; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * Parse the args of a command and check the format of args. |
---|
| 27 | */ |
---|
| 28 | public class CommandFormat { |
---|
| 29 | final String name; |
---|
| 30 | final int minPar, maxPar; |
---|
| 31 | final Map<String, Boolean> options = new HashMap<String, Boolean>(); |
---|
| 32 | |
---|
| 33 | /** constructor */ |
---|
| 34 | public CommandFormat(String n, int min, int max, String ... possibleOpt) { |
---|
| 35 | name = n; |
---|
| 36 | minPar = min; |
---|
| 37 | maxPar = max; |
---|
| 38 | for(String opt : possibleOpt) |
---|
| 39 | options.put(opt, Boolean.FALSE); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | /** Parse parameters starting from the given position |
---|
| 43 | * |
---|
| 44 | * @param args an array of input arguments |
---|
| 45 | * @param pos the position at which starts to parse |
---|
| 46 | * @return a list of parameters |
---|
| 47 | */ |
---|
| 48 | public List<String> parse(String[] args, int pos) { |
---|
| 49 | List<String> parameters = new ArrayList<String>(); |
---|
| 50 | for(; pos < args.length; pos++) { |
---|
| 51 | if (args[pos].charAt(0) == '-' && args[pos].length() > 1) { |
---|
| 52 | String opt = args[pos].substring(1); |
---|
| 53 | if (options.containsKey(opt)) |
---|
| 54 | options.put(opt, Boolean.TRUE); |
---|
| 55 | else |
---|
| 56 | throw new IllegalArgumentException("Illegal option " + args[pos]); |
---|
| 57 | } |
---|
| 58 | else |
---|
| 59 | parameters.add(args[pos]); |
---|
| 60 | } |
---|
| 61 | int psize = parameters.size(); |
---|
| 62 | if (psize < minPar || psize > maxPar) |
---|
| 63 | throw new IllegalArgumentException("Illegal number of arguments"); |
---|
| 64 | return parameters; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** Return if the option is set or not |
---|
| 68 | * |
---|
| 69 | * @param option String representation of an option |
---|
| 70 | * @return true is the option is set; false otherwise |
---|
| 71 | */ |
---|
| 72 | public boolean getOpt(String option) { |
---|
| 73 | return options.get(option); |
---|
| 74 | } |
---|
| 75 | } |
---|