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