001 /*
002 * Java Genetic Algorithm Library (jenetics-3.5.0).
003 * Copyright (c) 2007-2016 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019 */
020 package org.jenetics.internal.util;
021
022 import static java.util.Objects.requireNonNull;
023
024 import java.util.Optional;
025 import java.util.function.Function;
026
027 import org.jenetics.util.ISeq;
028
029 /**
030 * Helper class for parsing command line arguments.
031 *
032 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
033 * @version 3.4
034 * @since 3.4
035 */
036 public class Args {
037
038 private final ISeq<String> _args;
039
040 private Args(final String[] args) {
041 _args = ISeq.of(requireNonNull(args));
042 }
043
044 /**
045 * Return the parameter with the given name.
046 *
047 * @param name the parameter name
048 * @return the parameter with the given name, if any
049 */
050 public Optional<String> arg(final String name) {
051 int index = _args.indexOf("--" + name);
052 if (index == -1) index = _args.indexOf("-" + name);
053
054 return index >= 0 && index < _args.length() - 1
055 ? Optional.of(_args.get(index + 1))
056 : Optional.empty();
057 }
058
059 /**
060 * Return the int-argument with the given name.
061 *
062 * @param name the argument name
063 * @return the int argument value, if any
064 */
065 public Optional<Integer> intArg(final String name) {
066 return arg(name)
067 .flatMap(s -> parse(s, Integer::new));
068 }
069
070 /**
071 * Return the long-argument with the given name.
072 *
073 * @param name the argument name
074 * @return the long argument value, if any
075 */
076 public Optional<Long> longArg(final String name) {
077 return arg(name)
078 .flatMap(s -> parse(s, Long::new));
079 }
080
081 /**
082 * Return the double-argument with the given name.
083 *
084 * @param name the argument name
085 * @return the double argument value, if any
086 */
087 public Optional<Double> doubleArg(final String name) {
088 return arg(name)
089 .flatMap(s -> parse(s, Double::new));
090 }
091
092 private static <T> Optional<T> parse(
093 final String string,
094 final Function<String, T> parser
095 ) {
096 Optional<T> value = Optional.empty();
097 try {
098 value = Optional.of(parser.apply(string));
099 } catch (Exception ignore) {
100 }
101
102 return value;
103 }
104
105 @Override
106 public String toString() {
107 return _args.toString();
108 }
109
110 /**
111 * Wraps the given argument array into an {@code Args} object.
112 *
113 * @param args the underlying command line arguments
114 * @return the wrapped argument object
115 */
116 public static Args of(final String[] args) {
117 return new Args(args);
118 }
119
120 }
|