001    /*
002     * Copyright (C) 2012 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    public class ArgumentDescriptor<B extends TypeBinding> extends ParameterDescriptor<B> {
033    
034      /** . */
035      private final String name;
036    
037      public ArgumentDescriptor(
038        B binding,
039        String name,
040        Type javaType,
041        Description info,
042        boolean required,
043        boolean password,
044        boolean unquote,
045        Class<? extends Completer> completerType,
046        Annotation annotation) throws IllegalValueTypeException, IllegalParameterException {
047        super(
048          binding,
049          javaType,
050          info,
051          required,
052          password,
053          unquote,
054          completerType,
055          annotation);
056    
057        //
058        this.name = name;
059      }
060    
061      /**
062       * Returns the argument name, that can be null. This value is used for display capabilities and does not play a role
063       * when a command line is parsed.
064       *
065       * @return the argument name
066       */
067      public String getName() {
068        return name;
069      }
070    
071      @Override
072      public Object parse(List<String> values) throws CmdSyntaxException {
073        if (getMultiplicity() == Multiplicity.SINGLE) {
074          if (values.size() > 1) {
075            throw new CmdSyntaxException("Too many option values " + values);
076          }
077          String value = values.get(0);
078          try {
079            return parse(value);
080          } catch (Exception e) {
081            throw new CmdSyntaxException("Could not parse " + value);
082          }
083        } else {
084          List<Object> v = new ArrayList<Object>(values.size());
085          for (String value : values) {
086            try {
087              v.add(parse(value));
088            } catch (Exception e) {
089              throw new CmdSyntaxException("Could not parse " + value);
090            }
091          }
092          return v;
093        }
094      }
095    
096      /**
097       * Prints the argument:
098       *
099       * <ul>
100       * <li>Single valued arguments use the "$arg" pattern.</li>
101       * <li>Multi valued arguments use the "... $arg" pattern.</li>
102       * </ul>
103       *
104       * Where $arg is the value "arg" or the argument name when it is not null.
105       *
106       * @param writer the writer to print to
107       * @throws IOException any io exception
108       */
109      public void printUsage(Appendable writer) throws IOException {
110        if (getMultiplicity() == Multiplicity.MULTI) {
111          writer.append("... ");
112        }
113        writer.append((name == null || name.length() == 0) ? "arg" : name);
114      }
115    
116      @Override
117      public String toString() {
118        return "ArgumentDescriptor[" + name + "]";
119      }
120    }