Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo using picocli #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,39 @@ You can try this extension after building it using `rd` version 1.2 or later:
scripting2 - demonstrates typed data output for yaml/json scripting

Use "rd ext demo [command] help" to get help on any command.

# Picocli

**requires rd version 1.2.5 or later**

The Pico command demonstrates using Picocli to handle command argument and subcommand parsing

The build uses the 'shadow' gradle plugin to bundle Picocli into the extension jar so that it can be used standalone.

```
$ RD_EXT_DIR=$PWD/build/libs rd ext
A command was expected: rd ext [command]

ext: a test subcommand

Available commands:

demo - subcommand demo
pico - Demo use of picocli.

Use "rd ext [command] help" to get help on any command.
$ RD_EXT_DIR=$PWD/build/libs rd ext ext pico
What is your name? use --name <yourname>. Or use wave to just wave at things
$ RD_EXT_DIR=$PWD/build/libs rd ext pico -n bob
Name is bob
$ RD_EXT_DIR=$PWD/build/libs rd ext pico wave
Missing required option '--thingies=<thingies>'
Usage: pico wave -t=<thingies> [-t=<thingies>]...
wazzup
-t, --thingies=<thingies>

$ RD_EXT_DIR=$PWD/build/libs rd ext pico wave -t dirt -t sky
*waves at*
* dirt
* sky
```
24 changes: 15 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ plugins {
id 'java-library'
id 'groovy'
id 'idea'
id 'com.github.johnrengelman.shadow' version '5.2.0'
id "maven-publish"
id "nebula.maven-shadow-publish" version "17.2.1"
}

group 'org.rundeck.cli.demo.ext'
Expand All @@ -15,23 +18,26 @@ repositories {
mavenCentral()
}

ext.versions=[
toolbelt:'0.2.28',
rd:'1.3.9'
ext.versions = [
toolbelt: '0.2.28',
rd : '1.3.9',
picocli : '4.2.0'
]
assemble.dependsOn(shadowJar)

dependencies {

compileOnly "org.projectlombok:lombok:1.18.12"
annotationProcessor "org.projectlombok:lombok:1.18.12"

api "org.rundeck.cli:rd-cli-lib:${versions.rd}"
api "org.rundeck.cli-toolbelt:toolbelt-jewelcli:$versions.toolbelt"
implementation "org.rundeck.api:rd-api-client:${versions.rd}"
shadow "org.rundeck.cli:rd-cli-lib:${versions.rd}"
shadow "org.rundeck.cli-toolbelt:toolbelt-jewelcli:${versions.toolbelt}"
shadow "org.rundeck.api:rd-api-client:${versions.rd}"

implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation 'com.squareup.retrofit2:converter-jackson:2.7.1'
implementation 'com.squareup.retrofit2:converter-simplexml:2.7.1'
implementation "info.picocli:picocli:${versions.picocli}"
shadow 'com.squareup.retrofit2:retrofit:2.7.1'
shadow 'com.squareup.retrofit2:converter-jackson:2.7.1'
shadow 'com.squareup.retrofit2:converter-simplexml:2.7.1'


testImplementation "org.rundeck.cli:rd-cli-lib:${versions.rd}"
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/org/rundeck/cli/demo/ext/Pico.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.rundeck.cli.demo.ext;

import lombok.Setter;
import org.rundeck.client.tool.extension.RdCommandExtension;
import org.rundeck.client.tool.extension.RdTool;
import org.rundeck.toolbelt.SubCommand;
import org.rundeck.toolbelt.ToolBelt;
import picocli.CommandLine;

import java.util.List;
import java.util.concurrent.Callable;

// using picocli @Command to define picocli interface
@CommandLine.Command(
name = "pico",
mixinStandardHelpOptions = true,
version = "0.1-SNAPSHOT",
description = "@|bold,underline Demo|@ use of @|fg(red) picocli|@.",
subcommands = {
Pico.Wave.class
}
)
@SubCommand(path = {"ext"})
public class Pico
implements RdCommandExtension, ToolBelt.CommandInvoker, Callable<Integer>
{
@Setter RdTool rdTool;

@CommandLine.Option(names = {"-n", "--name"})
String name;

@Override
public Integer call() throws Exception {
if (name != null) {
System.out.println("Name is " + name);
} else {
System.out.println("What is your name? use --name <yourname>. Or use wave to just wave at things");
}
return 0;
}

/**
* Handle the argument list by invoking Picocli
*
* @param args
*/
@Override
public boolean run(final String[] args) {
return new CommandLine(this).execute(args) == 0;
}

@Override
public String getDescription() {
return new CommandLine(this).getHelp().description();
}

@Override
public void getHelp() {
new CommandLine(this).usage(new CommandLine(this).getOut());
}

@CommandLine.Command(name = "wave", description = "wazzup")
static class Wave
implements Callable<Integer>

{
@CommandLine.Option(names = {"--thingies", "-t"}, required = true)
List<String> thingies;

@Override
public Integer call() throws Exception {
System.out.println("*waves at*");
if (null != thingies) {
thingies.forEach(a -> System.out.printf("* %s%n", a));
}
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.rundeck.cli.demo.ext.MyCommand
org.rundeck.cli.demo.ext.Pico