Skip to content

Commit

Permalink
skara-314
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Helin committed Mar 19, 2020
1 parent d45b816 commit 038c21f
Show file tree
Hide file tree
Showing 18 changed files with 785 additions and 614 deletions.
72 changes: 41 additions & 31 deletions args/src/main/java/org/openjdk/skara/args/ArgumentParser.java
Expand Up @@ -31,6 +31,7 @@ public class ArgumentParser {
private final List<Flag> flags;
private final List<Input> inputs;
private final Map<String, Flag> names = new HashMap<String, Flag>();
private final boolean shouldShowHelp;

public ArgumentParser(String programName, List<Flag> flags) {
this(programName, flags, List.of());
Expand All @@ -41,11 +42,16 @@ public ArgumentParser(String programName, List<Flag> flags, List<Input> inputs)
this.flags = new ArrayList<Flag>(flags);
this.inputs = inputs;

var help = Switch.shortcut("h")
.fullname("help")
.helptext("Show this help text")
.optional();
this.flags.add(help);
if (!flags.stream().anyMatch(f -> f.shortcut().equals("h") && f.fullname().equals("help"))) {
var help = Switch.shortcut("h")
.fullname("help")
.helptext("Show this help text")
.optional();
this.flags.add(help);
shouldShowHelp = true;
} else {
shouldShowHelp = false;
}

for (var flag : this.flags) {
if (!flag.fullname().equals("")) {
Expand Down Expand Up @@ -77,26 +83,49 @@ private Flag lookupShortcut(String name) {
return lookupFlag(name, true);
}

private int longest(Function<Flag, String> getName) {
private static int longest(List<Flag> flags, Function<Flag, String> getName) {
return flags.stream()
.map(getName)
.filter(Objects::nonNull)
.mapToInt(String::length)
.reduce(0, Integer::max);
}

private int longestShortcut() {
return longest(Flag::shortcut);
private static int longestShortcut(List<Flag> flags) {
return longest(flags, Flag::shortcut);
}

private int longestFullname() {
return longest(f -> f.fullname() + " " + f.description());
private static int longestFullname(List<Flag> flags) {
return longest(flags, f -> f.fullname() + " " + f.description());
}

public void showUsage() {
showUsage(System.out);
}

public static void showFlags(PrintStream ps, List<Flag> flags, String prefix) {
var shortcutPad = longestShortcut(flags) + 1 + 2; // +1 for '-' and +2 for ', '
var fullnamePad = longestFullname(flags) + 2 + 2; // +2 for '--' and +2 for ' '

for (var flag : flags) {
ps.print(prefix);
var fmt = "%-" + shortcutPad + "s";
var s = flag.shortcut().equals("") ? " " : "-" + flag.shortcut() + ", ";
ps.print(String.format(fmt, s));

fmt = "%-" + fullnamePad + "s";
var desc = flag.description().equals("") ? "" : " " + flag.description();
s = flag.fullname().equals("") ? " " : "--" + flag.fullname() + desc + " ";
ps.print(String.format(fmt, s));

if (!flag.helptext().equals("")) {
ps.print(flag.helptext());
}

ps.println("");
}
}

public void showUsage(PrintStream ps) {
ps.print("usage: ");
ps.print(programName);
Expand Down Expand Up @@ -126,26 +155,7 @@ public void showUsage(PrintStream ps) {
}
ps.println("");

var shortcutPad = longestShortcut() + 1 + 2; // +1 for '-' and +2 for ', '
var fullnamePad = longestFullname() + 2 + 2; // +2 for '--' and +2 for ' '

for (var flag : flags) {
ps.print("\t");
var fmt = "%-" + shortcutPad + "s";
var s = flag.shortcut().equals("") ? " " : "-" + flag.shortcut() + ", ";
ps.print(String.format(fmt, s));

fmt = "%-" + fullnamePad + "s";
var desc = flag.description().equals("") ? "" : " " + flag.description();
s = flag.fullname().equals("") ? " " : "--" + flag.fullname() + desc + " ";
ps.print(String.format(fmt, s));

if (!flag.helptext().equals("")) {
ps.print(flag.helptext());
}

ps.println("");
}
showFlags(ps, flags, "\t");
}

public Arguments parse(String[] args) {
Expand Down Expand Up @@ -220,7 +230,7 @@ public Arguments parse(String[] args) {
}

var arguments = new Arguments(values, positional);
if (arguments.contains("help")) {
if (arguments.contains("help") && shouldShowHelp) {
showUsage();
System.exit(0);
}
Expand Down
8 changes: 4 additions & 4 deletions args/src/main/java/org/openjdk/skara/args/Flag.java
Expand Up @@ -45,19 +45,19 @@ boolean isSwitch() {
return isSwitch;
}

String fullname() {
public String fullname() {
return fullname;
}

String shortcut() {
public String shortcut() {
return shortcut;
}

String description() {
public String description() {
return description;
}

String helptext() {
public String helptext() {
return helptext;
}

Expand Down
Expand Up @@ -46,7 +46,9 @@ public MultiCommandParser(String programName, List<Command> commands) {
.collect(Collectors.toMap(
Command::name,
Function.identity()));
this.subCommands.put("help", helpCommand());
if (!commands.stream().anyMatch(c -> c.name().equals("help"))) {
this.subCommands.put("help", helpCommand());
}
}

private Command helpCommand() {
Expand Down
5 changes: 4 additions & 1 deletion cli/src/main/java/org/openjdk/skara/cli/GitPr.java
Expand Up @@ -31,7 +31,10 @@
public class GitPr {
public static void main(String[] args) throws Exception {
var commands = List.of(
Default.name("list")
Default.name("help")
.helptext("show help text")
.main(GitPrHelp::main),
Command.name("list")
.helptext("list open pull requests")
.main(GitPrList::main),
Command.name("fetch")
Expand Down
70 changes: 35 additions & 35 deletions cli/src/main/java/org/openjdk/skara/cli/pr/GitPrApply.java
Expand Up @@ -31,43 +31,43 @@
import java.util.List;

public class GitPrApply {
public static void main(String[] args) throws IOException {
var flags = List.of(
Option.shortcut("u")
.fullname("username")
.describe("NAME")
.helptext("Username on host")
.optional(),
Option.shortcut("r")
.fullname("remote")
.describe("NAME")
.helptext("Name of remote, defaults to 'origin'")
.optional(),
Switch.shortcut("")
.fullname("no-token")
.helptext("Do not use a personal access token (PAT)")
.optional(),
Switch.shortcut("")
.fullname("verbose")
.helptext("Turn on verbose output")
.optional(),
Switch.shortcut("")
.fullname("debug")
.helptext("Turn on debugging output")
.optional(),
Switch.shortcut("")
.fullname("version")
.helptext("Print the version of this tool")
.optional()
);
static List<Flag> flags = List.of(
Option.shortcut("u")
.fullname("username")
.describe("NAME")
.helptext("Username on host")
.optional(),
Option.shortcut("r")
.fullname("remote")
.describe("NAME")
.helptext("Name of remote, defaults to 'origin'")
.optional(),
Switch.shortcut("")
.fullname("no-token")
.helptext("Do not use a personal access token (PAT)")
.optional(),
Switch.shortcut("")
.fullname("verbose")
.helptext("Turn on verbose output")
.optional(),
Switch.shortcut("")
.fullname("debug")
.helptext("Turn on debugging output")
.optional(),
Switch.shortcut("")
.fullname("version")
.helptext("Print the version of this tool")
.optional()
);

var inputs = List.of(
Input.position(0)
.describe("ID")
.singular()
.optional()
);
static List<Input> inputs = List.of(
Input.position(0)
.describe("ID")
.singular()
.optional()
);

public static void main(String[] args) throws IOException {
var parser = new ArgumentParser("git-pr", flags, inputs);
var arguments = parse(parser, args);
var repo = getRepo();
Expand Down
63 changes: 32 additions & 31 deletions cli/src/main/java/org/openjdk/skara/cli/pr/GitPrApprove.java
Expand Up @@ -31,38 +31,39 @@
import java.util.List;

public class GitPrApprove {
public static void main(String[] args) throws IOException {
var flags = List.of(
Option.shortcut("u")
.fullname("username")
.describe("NAME")
.helptext("Username on host")
.optional(),
Option.shortcut("r")
.fullname("remote")
.describe("NAME")
.helptext("Name of remote, defaults to 'origin'")
.optional(),
Switch.shortcut("")
.fullname("verbose")
.helptext("Turn on verbose output")
.optional(),
Switch.shortcut("")
.fullname("debug")
.helptext("Turn on debugging output")
.optional(),
Switch.shortcut("")
.fullname("version")
.helptext("Print the version of this tool")
.optional()
);
static final List<Flag> flags = List.of(
Option.shortcut("u")
.fullname("username")
.describe("NAME")
.helptext("Username on host")
.optional(),
Option.shortcut("r")
.fullname("remote")
.describe("NAME")
.helptext("Name of remote, defaults to 'origin'")
.optional(),
Switch.shortcut("")
.fullname("verbose")
.helptext("Turn on verbose output")
.optional(),
Switch.shortcut("")
.fullname("debug")
.helptext("Turn on debugging output")
.optional(),
Switch.shortcut("")
.fullname("version")
.helptext("Print the version of this tool")
.optional()
);

static final List<Input> inputs = List.of(
Input.position(0)
.describe("ID")
.singular()
.optional()
);

var inputs = List.of(
Input.position(0)
.describe("ID")
.singular()
.optional()
);
public static void main(String[] args) throws IOException {
var parser = new ArgumentParser("git-pr", flags, inputs);
var arguments = parse(parser, args);
var repo = getRepo();
Expand Down

0 comments on commit 038c21f

Please sign in to comment.