Skip to content

Commit 928b7f4

Browse files
Added priority order to command line / param file for ArgUtils
1 parent 7dec474 commit 928b7f4

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

fj-core/src/main/java/org/fugerit/java/core/cli/ArgUtils.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.io.File;
2424
import java.io.FileInputStream;
25+
import java.util.Enumeration;
2526
import java.util.Properties;
2627

2728
/**
@@ -68,6 +69,18 @@ public static String getArgString( String argName ) {
6869
* @return the arguments parsed
6970
*/
7071
public static Properties getArgs( String[] args, boolean checkParamFile ) {
72+
return getArgs( args, checkParamFile, true );
73+
}
74+
75+
/**
76+
* <p>Parse an argument list as a property object.</p>
77+
*
78+
* @param args command line arguments
79+
* @param checkParamFile true if param-file should be checked if the argument is set.
80+
* @param priorityToParamFile true if param-file arguments should override command line arguments
81+
* @return the arguments parsed
82+
*/
83+
public static Properties getArgs( String[] args, boolean checkParamFile, boolean priorityToParamFile ) {
7184
Properties props = new Properties();
7285
if ( args != null ) {
7386
String currentkey = null;
@@ -94,7 +107,19 @@ public static Properties getArgs( String[] args, boolean checkParamFile ) {
94107
File f = new File( paramFile );
95108
try {
96109
FileInputStream fis = new FileInputStream( f );
97-
props.load( fis );
110+
if ( priorityToParamFile ) {
111+
props.load( fis );
112+
} else {
113+
Properties toLoad = new Properties();
114+
toLoad.load( fis );
115+
Enumeration<Object> keys = toLoad.keys();
116+
while ( keys.hasMoreElements() ) {
117+
String currentKey = keys.nextElement().toString();
118+
if ( !props.contains( currentKey ) ) {
119+
props.setProperty( currentKey , toLoad.getProperty( currentKey ) );
120+
}
121+
}
122+
}
98123
fis.close();
99124
} catch (Exception e) {
100125
throw new RuntimeException( e );

0 commit comments

Comments
 (0)