@Override
	public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
		if (event.getArgs().length == 0) {
			// If there are no arguments, create cmdLineArgs property source here. This would at least hold the default
			// command line options.
			if (event.getEnvironment().getPropertySources().get(COMMAND_LINE_PROPERTY_SOURCE_NAME) == null) {
				event.getEnvironment().getPropertySources().addFirst(new SimpleCommandLinePropertySource());
			}
		}
		CmdLineParser parser = null;
		try {
			environmentHolder.set(event.getEnvironment());
			parser = new CmdLineParser(options);
			parser.parseArgument(event.getArgs());
			if (TRUE.equals(options.isShowHelp())) {
				System.err.println("Usage:");
				parser.printUsage(System.err);
				System.exit(0);
			}
		}
		catch (CmdLineException e) {
			System.err.println(e.getMessage());
			System.err.println();
			System.err.println("Usage:");
			parser.printUsage(System.err);
			System.exit(1);
		}
		finally {
			environmentHolder.set(null);
		}

		final EnumerablePropertySource<T> ps = new BeanPropertiesPropertySource<T>(COMMAND_LINE_PROPERTY_SOURCE_NAME,
				options);

		// Convert all properties to their String representation
		// Also, don't advertise property names for which value is null
		Map<String, Object> map = new HashMap<String, Object>();
		for (String key : ps.getPropertyNames()) {
			Object raw = ps.getProperty(key);
			if (raw != null) {
				map.put(key, raw.toString());
			}
		}

		event.getEnvironment().getPropertySources().replace(COMMAND_LINE_PROPERTY_SOURCE_NAME,
				new MapPropertySource(COMMAND_LINE_PROPERTY_SOURCE_NAME, map));

	}
An {@code ApplicationListener} that will parse command line options and also replace the default boot commandline
{@link PropertySource} with those values. This turns out to be the most elegant solution if we want to keep the
{@link SpringApplicationBuilder} code clean.

@author Eric Bottard
@author Luke Taylor
spring-xd-dirt/src/main/java/org/springframework/xd/dirt/server/options/CommandLinePropertySourceOverridingListener.java