Here is a proposal for a simple Configuration file loading/managing some Key/values with typing, and controlled as a CLI command line with helper to understand usage.
Each item in the ConfigAttribute have :
- a
attrName
defining the configruation attribute name, - a
helpDescription
explaining the nature and usage of this attribute, - a
cliArgName
a shortcute used on CLI to set value on this attribute, - a
configAttrName
the name of this attribute in the configruation file (a standard properties file), - a
defaultValue
a default value fotr this attribute to de set if nothing is done on configuration file or from the CLI, - a
Function<String, Object> parserFunction
a function implmening the parsing of the valie from CLI and config file to convert the string value to a typed value object.
The Configuration
class provide a loadFrom
method to load configuration values from a properties file.
It also provide a helper parseArguments
to parse the famous String[] args
array from the CLI and assign the right configuration value to the right key.
private boolean init(String[] args) {
// load configuration from file.
List<String> lArgs = Arrays.asList(args);
config = new Configuration(ConfigAttribute.values());
if (!lArgs.isEmpty()) {
config.parseArguments(lArgs);
} else {
System.out.println(I18n.getMessage("app.message.execution.no.argument"));
}
String configFilePath = (String) Configuration.get(ConfigAttribute.CONFIG_FILE_PATH);
return config.loadFrom(configFilePath);
}
That's all.
McG.