mirror of
https://github.com/jlengrand/picocli.git
synced 2026-03-10 08:41:17 +00:00
Manual, chapter 17 'Subcommands': add source code tabs in Groovy language
This commit is contained in:
committed by
Remko Popma
parent
0fc72f23a8
commit
27555adb35
117
docs/index.adoc
117
docs/index.adoc
@@ -6851,7 +6851,26 @@ This is the recommended way when you want to use the picocli <<Annotation Proces
|
|||||||
GitRebase.class,
|
GitRebase.class,
|
||||||
GitTag.class
|
GitTag.class
|
||||||
})
|
})
|
||||||
public class Git { ... }
|
public class Git { /* ... */ }
|
||||||
|
----
|
||||||
|
|
||||||
|
.Groovy
|
||||||
|
[source,groovy,role="secondary"]
|
||||||
|
----
|
||||||
|
@Command(subcommands = [
|
||||||
|
GitStatus.class,
|
||||||
|
GitCommit.class,
|
||||||
|
GitAdd.class,
|
||||||
|
GitBranch.class,
|
||||||
|
GitCheckout.class,
|
||||||
|
GitClone.class,
|
||||||
|
GitDiff.class,
|
||||||
|
GitMerge.class,
|
||||||
|
GitPush.class,
|
||||||
|
GitRebase.class,
|
||||||
|
GitTag.class
|
||||||
|
])
|
||||||
|
public class Git { /* ... */ }
|
||||||
----
|
----
|
||||||
|
|
||||||
.Kotlin
|
.Kotlin
|
||||||
@@ -6870,7 +6889,7 @@ public class Git { ... }
|
|||||||
GitRebase::class,
|
GitRebase::class,
|
||||||
GitTag::class]
|
GitTag::class]
|
||||||
)
|
)
|
||||||
public class Git { ... }
|
public class Git { /* ... */ }
|
||||||
----
|
----
|
||||||
|
|
||||||
Subcommands referenced in a `subcommands` attribute _must_ have a `@Command` annotation with a `name` attribute, or an exception is thrown from the `CommandLine` constructor.
|
Subcommands referenced in a `subcommands` attribute _must_ have a `@Command` annotation with a `name` attribute, or an exception is thrown from the `CommandLine` constructor.
|
||||||
@@ -6914,6 +6933,23 @@ CommandLine commandLine = new CommandLine(new Git())
|
|||||||
.addSubcommand("tag", new GitTag());
|
.addSubcommand("tag", new GitTag());
|
||||||
----
|
----
|
||||||
|
|
||||||
|
.Groovy
|
||||||
|
[source,groovy,role="secondary"]
|
||||||
|
----
|
||||||
|
def commandLine = new CommandLine(new Git())
|
||||||
|
.addSubcommand("status", new GitStatus())
|
||||||
|
.addSubcommand("commit", new GitCommit())
|
||||||
|
.addSubcommand("add", new GitAdd())
|
||||||
|
.addSubcommand("branch", new GitBranch())
|
||||||
|
.addSubcommand("checkout", new GitCheckout())
|
||||||
|
.addSubcommand("clone", new GitClone())
|
||||||
|
.addSubcommand("diff", new GitDiff())
|
||||||
|
.addSubcommand("merge", new GitMerge())
|
||||||
|
.addSubcommand("push", new GitPush())
|
||||||
|
.addSubcommand("rebase", new GitRebase())
|
||||||
|
.addSubcommand("tag", new GitTag());
|
||||||
|
----
|
||||||
|
|
||||||
.Kotlin
|
.Kotlin
|
||||||
[source,kotlin,role="secondary"]
|
[source,kotlin,role="secondary"]
|
||||||
----
|
----
|
||||||
@@ -6986,6 +7022,42 @@ class Bar implements Callable<Integer> {
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
.Groovy
|
||||||
|
[source,groovy,role="secondary"]
|
||||||
|
----
|
||||||
|
@Command(name = "foo", subcommands = Bar.class)
|
||||||
|
class Foo implements Callable<Integer> {
|
||||||
|
@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<Integer> {
|
||||||
|
@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
|
.Kotlin
|
||||||
[source,kotlin,role="secondary"]
|
[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
|
.Kotlin
|
||||||
[source,kotlin,role="secondary"]
|
[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
|
.Kotlin
|
||||||
[source,kotlin,role="secondary"]
|
[source,kotlin,role="secondary"]
|
||||||
----
|
----
|
||||||
|
|||||||
Reference in New Issue
Block a user