001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.env; 018 019import java.util.List; 020 021import org.springframework.util.StringUtils; 022 023/** 024 * {@link CommandLinePropertySource} implementation backed by a simple String array. 025 * 026 * <h3>Purpose</h3> 027 * This {@code CommandLinePropertySource} implementation aims to provide the simplest 028 * possible approach to parsing command line arguments. As with all {@code 029 * CommandLinePropertySource} implementations, command line arguments are broken into two 030 * distinct groups: <em>option arguments</em> and <em>non-option arguments</em>, as 031 * described below <em>(some sections copied from Javadoc for {@link SimpleCommandLineArgsParser})</em>: 032 * 033 * <h3>Working with option arguments</h3> 034 * Option arguments must adhere to the exact syntax: 035 * <pre class="code">--optName[=optValue]</pre> 036 * That is, options must be prefixed with "{@code --}", and may or may not specify a value. 037 * If a value is specified, the name and value must be separated <em>without spaces</em> 038 * by an equals sign ("="). 039 * 040 * <h4>Valid examples of option arguments</h4> 041 * <pre class="code"> 042 * --foo 043 * --foo=bar 044 * --foo="bar then baz" 045 * --foo=bar,baz,biz</pre> 046 * 047 * <h4>Invalid examples of option arguments</h4> 048 * <pre class="code"> 049 * -foo 050 * --foo bar 051 * --foo = bar 052 * --foo=bar --foo=baz --foo=biz</pre> 053 * 054 * <h3>Working with non-option arguments</h3> 055 * Any and all arguments specified at the command line without the "{@code --}" option 056 * prefix will be considered as "non-option arguments" and made available through the 057 * {@link #getNonOptionArgs()} method. 058 * 059 * <h2>Typical usage</h2> 060 * <pre class="code"> 061 * public static void main(String[] args) { 062 * PropertySource<?> ps = new SimpleCommandLinePropertySource(args); 063 * // ... 064 * }</pre> 065 * 066 * See {@link CommandLinePropertySource} for complete general usage examples. 067 * 068 * <h3>Beyond the basics</h3> 069 * 070 * <p>When more fully-featured command line parsing is necessary, consider using 071 * the provided {@link JOptCommandLinePropertySource}, or implement your own 072 * {@code CommandLinePropertySource} against the command line parsing library of your 073 * choice! 074 * 075 * @author Chris Beams 076 * @since 3.1 077 * @see CommandLinePropertySource 078 * @see JOptCommandLinePropertySource 079 */ 080public class SimpleCommandLinePropertySource extends CommandLinePropertySource<CommandLineArgs> { 081 082 /** 083 * Create a new {@code SimpleCommandLinePropertySource} having the default name 084 * and backed by the given {@code String[]} of command line arguments. 085 * @see CommandLinePropertySource#COMMAND_LINE_PROPERTY_SOURCE_NAME 086 * @see CommandLinePropertySource#CommandLinePropertySource(Object) 087 */ 088 public SimpleCommandLinePropertySource(String... args) { 089 super(new SimpleCommandLineArgsParser().parse(args)); 090 } 091 092 /** 093 * Create a new {@code SimpleCommandLinePropertySource} having the given name 094 * and backed by the given {@code String[]} of command line arguments. 095 */ 096 public SimpleCommandLinePropertySource(String name, String[] args) { 097 super(name, new SimpleCommandLineArgsParser().parse(args)); 098 } 099 100 /** 101 * Get the property names for the option arguments. 102 */ 103 @Override 104 public String[] getPropertyNames() { 105 return StringUtils.toStringArray(this.source.getOptionNames()); 106 } 107 108 @Override 109 protected boolean containsOption(String name) { 110 return this.source.containsOption(name); 111 } 112 113 @Override 114 protected List<String> getOptionValues(String name) { 115 return this.source.getOptionValues(name); 116 } 117 118 @Override 119 protected List<String> getNonOptionArgs() { 120 return this.source.getNonOptionArgs(); 121 } 122 123}