org.springframework.core.env.SimpleCommandLinePropertySource Maven / Gradle / Ivy
/*
* Copyright 2002-2021 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
*
* https://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.List;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
* {@link CommandLinePropertySource} implementation backed by a simple String array.
*
* Purpose
* This {@code CommandLinePropertySource} implementation aims to provide the simplest
* possible approach to parsing command line arguments. As with all {@code
* CommandLinePropertySource} implementations, command line arguments are broken into two
* distinct groups: option arguments and non-option arguments, as
* described below (some sections copied from Javadoc for
* {@link SimpleCommandLineArgsParser}):
*
*
Working with option arguments
* Option arguments must adhere to the exact syntax:
*
*
--optName[=optValue]
*
* That is, options must be prefixed with "{@code --}" and may or may not
* specify a value. If a value is specified, the name and value must be separated
* without spaces by an equals sign ("="). The value may optionally be
* an empty string.
*
*
Valid examples of option arguments
*
* --foo
* --foo=
* --foo=""
* --foo=bar
* --foo="bar then baz"
* --foo=bar,baz,biz
*
* Invalid examples of option arguments
*
* -foo
* --foo bar
* --foo = bar
* --foo=bar --foo=baz --foo=biz
*
* Working with non-option arguments
* Any and all arguments specified at the command line without the "{@code --}"
* option prefix will be considered as "non-option arguments" and made available
* through the {@link CommandLineArgs#getNonOptionArgs()} method.
*
*
Typical usage
*
* public static void main(String[] args) {
* PropertySource<?> ps = new SimpleCommandLinePropertySource(args);
* // ...
* }
*
* See {@link CommandLinePropertySource} for complete general usage examples.
*
* Beyond the basics
*
* When more fully-featured command line parsing is necessary, consider using
* the provided {@link JOptCommandLinePropertySource}, or implement your own
* {@code CommandLinePropertySource} against the command line parsing library of your
* choice.
*
* @author Chris Beams
* @since 3.1
* @see CommandLinePropertySource
* @see JOptCommandLinePropertySource
*/
public class SimpleCommandLinePropertySource extends CommandLinePropertySource {
/**
* Create a new {@code SimpleCommandLinePropertySource} having the default name
* and backed by the given {@code String[]} of command line arguments.
* @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME
* @see CommandLinePropertySource#CommandLinePropertySource(Object)
*/
public SimpleCommandLinePropertySource(String... args) {
super(new SimpleCommandLineArgsParser().parse(args));
}
/**
* Create a new {@code SimpleCommandLinePropertySource} having the given name
* and backed by the given {@code String[]} of command line arguments.
*/
public SimpleCommandLinePropertySource(String name, String[] args) {
super(name, new SimpleCommandLineArgsParser().parse(args));
}
/**
* Get the property names for the option arguments.
*/
@Override
public String[] getPropertyNames() {
return StringUtils.toStringArray(this.source.getOptionNames());
}
@Override
protected boolean containsOption(String name) {
return this.source.containsOption(name);
}
@Override
@Nullable
protected List getOptionValues(String name) {
return this.source.getOptionValues(name);
}
@Override
protected List getNonOptionArgs() {
return this.source.getNonOptionArgs();
}
}