001 /* 002 * Copyright (C) 2010 eXo Platform SAS. 003 * 004 * This is free software; you can redistribute it and/or modify it 005 * under the terms of the GNU Lesser General Public License as 006 * published by the Free Software Foundation; either version 2.1 of 007 * the License, or (at your option) any later version. 008 * 009 * This software is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * You should have received a copy of the GNU Lesser General Public 015 * License along with this software; if not, write to the Free 016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 018 */ 019 020 package org.crsh.cmdline; 021 022 import org.crsh.cmdline.binding.TypeBinding; 023 import org.crsh.cmdline.matcher.CmdSyntaxException; 024 import org.crsh.cmdline.spi.Completer; 025 026 import java.io.IOException; 027 import java.lang.annotation.Annotation; 028 import java.lang.reflect.Type; 029 import java.util.ArrayList; 030 import java.util.List; 031 032 /** 033 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 034 * @version $Revision$ 035 */ 036 public class ArgumentDescriptor<B extends TypeBinding> extends ParameterDescriptor<B> { 037 038 /** . */ 039 private final String name; 040 041 public ArgumentDescriptor( 042 B binding, 043 String name, 044 Type javaType, 045 Description info, 046 boolean required, 047 boolean password, 048 boolean unquote, 049 Class<? extends Completer> completerType, 050 Annotation annotation) throws IllegalValueTypeException, IllegalParameterException { 051 super( 052 binding, 053 javaType, 054 info, 055 required, 056 password, 057 unquote, 058 completerType, 059 annotation); 060 061 // 062 this.name = name; 063 } 064 065 /** 066 * Returns the argument name, that can be null. This value is used for display capabilities and does not play a role 067 * when a command line is parsed. 068 * 069 * @return the argument name 070 */ 071 public String getName() { 072 return name; 073 } 074 075 @Override 076 public Object parse(List<String> values) throws CmdSyntaxException { 077 if (getMultiplicity() == Multiplicity.SINGLE) { 078 if (values.size() > 1) { 079 throw new CmdSyntaxException("Too many option values " + values); 080 } 081 String value = values.get(0); 082 try { 083 return parse(value); 084 } catch (Exception e) { 085 throw new CmdSyntaxException("Could not parse " + value); 086 } 087 } else { 088 List<Object> v = new ArrayList<Object>(values.size()); 089 for (String value : values) { 090 try { 091 v.add(parse(value)); 092 } catch (Exception e) { 093 throw new CmdSyntaxException("Could not parse " + value); 094 } 095 } 096 return v; 097 } 098 } 099 100 /** 101 * Prints the argument: 102 * 103 * <ul> 104 * <li>Single valued arguments use the "$arg" pattern.</li> 105 * <li>Multi valued arguments use the "... $arg" pattern.</li> 106 * </ul> 107 * 108 * Where $arg is the value "arg" or the argument name when it is not null. 109 * 110 * @param writer the writer to print to 111 * @throws IOException any io exception 112 */ 113 public void printUsage(Appendable writer) throws IOException { 114 if (getMultiplicity() == Multiplicity.MULTI) { 115 writer.append("... "); 116 } 117 writer.append((name == null || name.length() == 0) ? "arg" : name); 118 } 119 120 @Override 121 public String toString() { 122 return "ArgumentDescriptor[" + name + "]"; 123 } 124 }