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