Skip to content

Commit

Permalink
[#1915] improve default provider examples
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Jan 13, 2023
1 parent 968506f commit f0cb251
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import java.util.Properties;
import java.util.concurrent.TimeUnit;

@Command(name = "demo", mixinStandardHelpOptions = true, version = "3.9.3",
description = "Demonstrate parsing & type conversion",
defaultValueProvider = SimpleDefaultProvider.class)
public class SimplePropertyDefaultProviderDemo implements Runnable { // ...
@Command(name = "demo", mixinStandardHelpOptions = true, version = CommandLine.VERSION,
description = "Demonstrate property file based custom default provider",
defaultValueProvider = MyPropertyDefaultProvider.class)
public class MyPropertyDefaultProviderDemo implements Runnable { // ...

@Option(names = "-x", description = "Print count. ${DEFAULT-VALUE} by default.")
int x;
Expand All @@ -32,11 +32,11 @@ public void run() {
}

public static void main(String[] args) {
new CommandLine(new SimplePropertyDefaultProviderDemo()).execute(args);
new CommandLine(new MyPropertyDefaultProviderDemo()).execute(args);
}
}

class PropertyDefaultProvider implements IDefaultValueProvider {
class MyPropertyDefaultProvider implements IDefaultValueProvider {
private Properties properties;

@Override
Expand All @@ -48,8 +48,9 @@ public String defaultValue(ArgSpec argSpec) throws Exception {
properties.load(reader);
}
}
return argSpec.isOption()
? properties.getProperty(((OptionSpec) argSpec).longestName())
: properties.getProperty(argSpec.paramLabel());
String key = argSpec.isOption()
? ((OptionSpec) argSpec).longestName()
: argSpec.paramLabel();
return properties.getProperty(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void run() {
}

public static void main(String[] args) {
new CommandLine(new SimplePropertyDefaultProviderDemo()).execute(args);
new CommandLine(new SimpleDefaultProviderDemo()).execute(args);
}
}

Expand Down

0 comments on commit f0cb251

Please sign in to comment.