-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MID-8842 ninja - help command + updated -h behavior (by default only …
…lists commands) (cherry picked from commit b8cfff1)
- Loading branch information
Showing
9 changed files
with
193 additions
and
20 deletions.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
tools/ninja/src/main/java/com/evolveum/midpoint/ninja/action/HelpAction.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,46 @@ | ||
/* | ||
* Copyright (C) 2010-2023 Evolveum and contributors | ||
* | ||
* This work is dual-licensed under the Apache License 2.0 | ||
* and European Union Public License. See LICENSE file for details. | ||
*/ | ||
|
||
package com.evolveum.midpoint.ninja.action; | ||
|
||
import java.util.List; | ||
|
||
import com.beust.jcommander.JCommander; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import com.evolveum.midpoint.ninja.util.NinjaUtils; | ||
|
||
public class HelpAction extends Action<HelpOptions, ActionResult> { | ||
|
||
@Override | ||
public String getOperationName() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public ActionResult execute() throws Exception { | ||
String command = options.getCommand(); | ||
|
||
JCommander jc = NinjaUtils.setupCommandLineParser(); | ||
|
||
if (StringUtils.isNotEmpty(command)) { | ||
JCommander specific = jc.getCommands().get(command); | ||
if (specific == null) { | ||
log.error( | ||
"Unknown command {}, known commands: {}", | ||
command, | ||
StringUtils.join(", ", List.of(jc.getCommands().keySet()).stream().sorted())); | ||
return new ActionResult(null, 1); | ||
} | ||
} | ||
|
||
String help = NinjaUtils.createHelp(jc, command); | ||
context.out.println(help); | ||
|
||
return new ActionResult<>(null, 0); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tools/ninja/src/main/java/com/evolveum/midpoint/ninja/action/HelpOptions.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,29 @@ | ||
/* | ||
* Copyright (C) 2010-2023 Evolveum and contributors | ||
* | ||
* This work is dual-licensed under the Apache License 2.0 | ||
* and European Union Public License. See LICENSE file for details. | ||
*/ | ||
|
||
package com.evolveum.midpoint.ninja.action; | ||
|
||
import com.beust.jcommander.Parameter; | ||
import com.beust.jcommander.Parameters; | ||
|
||
/** | ||
* Created by Viliam Repan (lazyman). | ||
*/ | ||
@Parameters(resourceBundle = "messages", commandDescriptionKey = "help") | ||
public class HelpOptions { | ||
|
||
@Parameter(descriptionKey = "help.command") | ||
private String command; | ||
|
||
public String getCommand() { | ||
return command; | ||
} | ||
|
||
public void setCommand(String command) { | ||
this.command = command; | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
tools/ninja/src/main/java/com/evolveum/midpoint/ninja/impl/NinjaUsageFormatter.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,50 @@ | ||
/* | ||
* Copyright (C) 2010-2023 Evolveum and contributors | ||
* | ||
* This work is dual-licensed under the Apache License 2.0 | ||
* and European Union Public License. See LICENSE file for details. | ||
*/ | ||
|
||
package com.evolveum.midpoint.ninja.impl; | ||
|
||
import java.util.Map; | ||
|
||
import com.beust.jcommander.DefaultUsageFormatter; | ||
import com.beust.jcommander.JCommander; | ||
import com.beust.jcommander.Parameters; | ||
|
||
public class NinjaUsageFormatter extends DefaultUsageFormatter { | ||
|
||
private JCommander commander; | ||
|
||
public NinjaUsageFormatter(JCommander commander) { | ||
super(commander); | ||
|
||
this.commander = commander; | ||
} | ||
|
||
@Override | ||
public void appendCommands(StringBuilder out, int indentCount, int descriptionIndent, String indent) { | ||
out.append(indent + "\n Commands:\n\n"); | ||
|
||
int maxCommandNameLength = commander.getRawCommands().keySet().stream() | ||
.map(pn -> pn.getDisplayName().length()) | ||
.max(Integer::compareTo).orElse(0); | ||
|
||
// The magic value 3 is the number of spaces between the name of the option and its description | ||
for (Map.Entry<JCommander.ProgramName, JCommander> commands : commander.getRawCommands().entrySet()) { | ||
Object arg = commands.getValue().getObjects().get(0); | ||
Parameters p = arg.getClass().getAnnotation(Parameters.class); | ||
|
||
if (p == null || !p.hidden()) { | ||
JCommander.ProgramName progName = commands.getKey(); | ||
String dispName = progName.getDisplayName() + s(maxCommandNameLength - progName.getDisplayName().length() + 3); | ||
|
||
String description = indent + s(4) + dispName + getCommandDescription(progName.getName()); | ||
|
||
wrapDescription(out, indentCount + descriptionIndent, description); | ||
out.append("\n"); | ||
} | ||
} | ||
} | ||
} |
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
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
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
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