All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.core.env.JOptCommandLinePropertySource Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2002-2014 the original author or authors.
 *
 * 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 org.springframework.core.env;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import joptsimple.OptionSet;
import joptsimple.OptionSpec;

import org.springframework.util.Assert;

/**
 * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}.
 *
 * 

Typical usage

* Configure and execute an {@code OptionParser} against the {@code String[]} of arguments * supplied to the {@code main} method, and create a {@link JOptCommandLinePropertySource} * using the resulting {@code OptionSet} object: *
 * public static void main(String[] args) {
 *     OptionParser parser = new OptionParser();
 *     parser.accepts("option1");
 *     parser.accepts("option2").withRequiredArg();
 *     OptionSet options = parser.parse(args);
 *     PropertySource ps = new JOptCommandLinePropertySource(options);
 *     // ...
 * }
* * See {@link CommandLinePropertySource} for complete general usage examples. * *

Requires JOpt version 4.3 or higher. Tested against JOpt up until 4.6. * * @author Chris Beams * @author Juergen Hoeller * @author Dave Syer * @since 3.1 * @see CommandLinePropertySource * @see joptsimple.OptionParser * @see joptsimple.OptionSet */ public class JOptCommandLinePropertySource extends CommandLinePropertySource { /** * Create a new {@code JOptCommandLinePropertySource} having the default name * and backed by the given {@code OptionSet}. * @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME * @see CommandLinePropertySource#CommandLinePropertySource(Object) */ public JOptCommandLinePropertySource(OptionSet options) { super(options); } /** * Create a new {@code JOptCommandLinePropertySource} having the given name * and backed by the given {@code OptionSet}. */ public JOptCommandLinePropertySource(String name, OptionSet options) { super(name, options); } @Override protected boolean containsOption(String name) { return this.source.has(name); } @Override public String[] getPropertyNames() { List names = new ArrayList(); for (OptionSpec spec : this.source.specs()) { List aliases = new ArrayList(spec.options()); if (!aliases.isEmpty()) { // Only the longest name is used for enumerating names.add(aliases.get(aliases.size() - 1)); } } return names.toArray(new String[names.size()]); } @Override public List getOptionValues(String name) { List argValues = this.source.valuesOf(name); List stringArgValues = new ArrayList(); for (Object argValue : argValues) { stringArgValues.add(argValue instanceof String ? (String) argValue : argValue.toString()); } if (stringArgValues.isEmpty()) { return (this.source.has(name) ? Collections.emptyList() : null); } return Collections.unmodifiableList(stringArgValues); } @Override protected List getNonOptionArgs() { List argValues = this.source.nonOptionArguments(); List stringArgValues = new ArrayList(); for (Object argValue : argValues) { Assert.isInstanceOf(String.class, argValue, "Argument values must be of type String"); stringArgValues.add((String) argValue); } return (stringArgValues.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(stringArgValues)); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy