diff options
Diffstat (limited to 'src/main/java/com/beust/jcommander/converters')
5 files changed, 154 insertions, 3 deletions
diff --git a/src/main/java/com/beust/jcommander/converters/BaseConverter.java b/src/main/java/com/beust/jcommander/converters/BaseConverter.java index 4287163..02e94b8 100644 --- a/src/main/java/com/beust/jcommander/converters/BaseConverter.java +++ b/src/main/java/com/beust/jcommander/converters/BaseConverter.java @@ -27,14 +27,14 @@ import com.beust.jcommander.IStringConverter; */ abstract public class BaseConverter<T> implements IStringConverter<T> { - private String m_optionName; + private String optionName; public BaseConverter(String optionName) { - m_optionName = optionName; + this.optionName = optionName; } public String getOptionName() { - return m_optionName; + return optionName; } protected String getErrorString(String value, String to) { diff --git a/src/main/java/com/beust/jcommander/converters/CharArrayConverter.java b/src/main/java/com/beust/jcommander/converters/CharArrayConverter.java new file mode 100644 index 0000000..5252f63 --- /dev/null +++ b/src/main/java/com/beust/jcommander/converters/CharArrayConverter.java @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2010 the original author or authors. + * See the notice.md file distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.beust.jcommander.converters; + +import com.beust.jcommander.IStringConverter; + +/** + * Converts a String to a char[]. + * + * @author Gary Gregory + */ +public class CharArrayConverter implements IStringConverter<char[]> { + + public char[] convert(final String value) { + return value.toCharArray(); + } +}
\ No newline at end of file diff --git a/src/main/java/com/beust/jcommander/converters/DefaultListConverter.java b/src/main/java/com/beust/jcommander/converters/DefaultListConverter.java new file mode 100644 index 0000000..00bf9ac --- /dev/null +++ b/src/main/java/com/beust/jcommander/converters/DefaultListConverter.java @@ -0,0 +1,36 @@ +package com.beust.jcommander.converters; + +import com.beust.jcommander.IStringConverter; +import com.beust.jcommander.internal.Lists; + +import java.util.List; + +/** + * A converter to obtain a list of elements. + * @param <T> the element type + * @author simon04 + */ +public class DefaultListConverter<T> implements IStringConverter<List<T>> { + + private final IParameterSplitter splitter; + private final IStringConverter<T> converter; + + /** + * Constructs a new converter. + * @param splitter to split value into list of arguments + * @param converter to convert list of arguments to target element type + */ + public DefaultListConverter(IParameterSplitter splitter, IStringConverter<T> converter) { + this.splitter = splitter; + this.converter = converter; + } + + @Override + public List<T> convert(String value) { + List<T> result = Lists.newArrayList(); + for (String param : splitter.split(value)) { + result.add(converter.convert(param)); + } + return result; + } +} diff --git a/src/main/java/com/beust/jcommander/converters/EnumConverter.java b/src/main/java/com/beust/jcommander/converters/EnumConverter.java new file mode 100644 index 0000000..3e850bb --- /dev/null +++ b/src/main/java/com/beust/jcommander/converters/EnumConverter.java @@ -0,0 +1,42 @@ +package com.beust.jcommander.converters; + +import com.beust.jcommander.IStringConverter; +import com.beust.jcommander.ParameterException; + +import java.util.EnumSet; + +/** + * A converter to parse enums + * @param <T> the enum type + * @author simon04 + */ +public class EnumConverter<T extends Enum<T>> implements IStringConverter<T> { + + private final String optionName; + private final Class<T> clazz; + + /** + * Constructs a new converter. + * @param optionName the option name for error reporting + * @param clazz the enum class + */ + public EnumConverter(String optionName, Class<T> clazz) { + this.optionName = optionName; + this.clazz = clazz; + } + + @Override + public T convert(String value) { + try { + try { + return Enum.valueOf(clazz, value); + } catch (IllegalArgumentException e) { + return Enum.valueOf(clazz, value.toUpperCase()); + } + } catch (Exception e) { + throw new ParameterException("Invalid value for " + optionName + " parameter. Allowed values:" + + EnumSet.allOf(clazz)); + + } + } +} diff --git a/src/main/java/com/beust/jcommander/converters/InetAddressConverter.java b/src/main/java/com/beust/jcommander/converters/InetAddressConverter.java new file mode 100644 index 0000000..b6f391a --- /dev/null +++ b/src/main/java/com/beust/jcommander/converters/InetAddressConverter.java @@ -0,0 +1,40 @@ +/** + * Copyright (C) 2010 the original author or authors. + * See the notice.md file distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.beust.jcommander.converters; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +import com.beust.jcommander.IStringConverter; + +/** + * Converts {@code String}s to {@code InetAddress}'. + */ +public class InetAddressConverter implements IStringConverter<InetAddress> { + + @Override + public InetAddress convert(String host) { + try { + return InetAddress.getByName(host); + } catch (UnknownHostException e) { + throw new IllegalArgumentException(host, e); + } + } + +}
\ No newline at end of file |