Skip to content
Permalink
Browse files
314: Add help command to git-pr
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Mar 19, 2020
1 parent 19f319e commit ac71433a60ad49675e897b75137adfc75f0f5965
Showing 18 changed files with 785 additions and 614 deletions.
@@ -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());
@@ -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("")) {
@@ -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);
@@ -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) {
@@ -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);
}
@@ -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;
}

@@ -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() {
@@ -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")
@@ -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();
@@ -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();

0 comments on commit ac71433

Please sign in to comment.