diff --git a/docs/index.adoc b/docs/index.adoc index 5d14b77a..4c01b86f 100644 --- a/docs/index.adoc +++ b/docs/index.adoc @@ -6851,7 +6851,26 @@ This is the recommended way when you want to use the picocli < { } ---- +.Groovy +[source,groovy,role="secondary"] +---- +@Command(name = "foo", subcommands = Bar.class) +class Foo implements Callable { + @Option(names = "-x") def x + + @Override public Integer call() { + println "hi from foo, x=$x" + def ok = true; + return ok ? 0 : 1 // exit code + } + + public static void main(String... args) { + def exitCode = new CommandLine(new Foo()).execute(args) + System.exit(exitCode) + } +} + +@Command(name = "bar", description = "I'm a subcommand of `foo`") +class Bar implements Callable { + @Option(names = "-y") int y + + @Override public Integer call() { + println "hi from bar, y=$y" + return 23 + } + + @Command(name = "baz", description = "I'm a subcommand of `bar`") + int baz(@Option(names = "-z") int z) { + println "hi from baz, z=$z" + return 45 + } +} +---- + .Kotlin [source,kotlin,role="secondary"] ---- @@ -7101,6 +7173,35 @@ class MyApp implements Runnable { } ---- +.Groovy +[source,groovy,role="secondary"] +---- +@Command(subcommands = [Sub1.class, Sub2.class, Sub3.class]) +class MyApp implements Runnable { + + // A reference to this method can be used as a custom execution strategy + // that first calls the init() method, + // and then delegates to the default execution strategy. + def int executionStrategy(ParseResult parseResult) { + init(); // custom initialization to be done before executing any command or subcommand + return new CommandLine.RunLast().execute(parseResult); // default execution strategy + } + + def void init() { + // ... + } + + public static void main(String[] args) { + def app = new MyApp(); + new CommandLine(app) + .setExecutionStrategy(app.&executionStrategy) + .execute(args); + } + + // ... +} +---- + .Kotlin [source,kotlin,role="secondary"] ---- @@ -7159,6 +7260,18 @@ class FileUtils { } ---- +.Groovy +[source,groovy,role="secondary"] +---- +@Command(name = "fileutils", subcommands = [ListFiles.class]) +class FileUtils { + + @Option(names = ["-d", "--directory"], + description = "this option applies to all subcommands") + def baseDirectory; +} +---- + .Kotlin [source,kotlin,role="secondary"] ----