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