-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1915] improve default provider examples
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...li-examples/src/main/java/picocli/examples/defaultprovider/SimpleDefaultProviderDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package picocli.examples.defaultprovider; | ||
|
||
import picocli.CommandLine; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.IDefaultValueProvider; | ||
import picocli.CommandLine.Model.ArgSpec; | ||
import picocli.CommandLine.Model.OptionSpec; | ||
import picocli.CommandLine.Option; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.Reader; | ||
import java.util.Properties; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Command(name = "demo", mixinStandardHelpOptions = true, version = CommandLine.VERSION, | ||
description = "Demonstrates default provider", | ||
defaultValueProvider = SimpleDefaultProvider.class) | ||
public class SimpleDefaultProviderDemo implements Runnable { // ... | ||
|
||
@Option(names = "-x", description = "Print count. ${DEFAULT-VALUE} by default.") | ||
int x; | ||
|
||
@Option(names = "-d") double d; | ||
@Option(names = { "-u", "--timeUnit"}) TimeUnit unit; | ||
|
||
@Override | ||
public void run() { | ||
for (int i = 0; i < x; i++) { | ||
System.out.printf("You selected %f, %s.%n", d, unit); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
new CommandLine(new SimplePropertyDefaultProviderDemo()).execute(args); | ||
} | ||
} | ||
|
||
class SimpleDefaultProvider implements IDefaultValueProvider { | ||
|
||
@Override | ||
public String defaultValue(ArgSpec argSpec) throws Exception { | ||
|
||
if (argSpec.isOption()) { | ||
OptionSpec option = (OptionSpec) argSpec; | ||
if ("--timeUnit".equals(option.longestName())) { | ||
return TimeUnit.SECONDS.name(); | ||
} | ||
if ("-x".equals(option.longestName())) { | ||
return "3"; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters