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 999b937 commit 968506f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@Command(name = "demo", mixinStandardHelpOptions = true, version = "3.9.3",
description = "Demonstrate parsing & type conversion",
defaultValueProvider = PropertyDefaultProvider.class)
defaultValueProvider = SimpleDefaultProvider.class)
public class SimplePropertyDefaultProviderDemo implements Runnable { // ...

@Option(names = "-x", description = "Print count. ${DEFAULT-VALUE} by default.")
Expand Down

0 comments on commit 968506f

Please sign in to comment.