Skip to content

Commit

Permalink
feat: add process builder function
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Apr 1, 2024
1 parent 45adbd3 commit 5d047eb
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/main/java/space/iseki/cmdpipe/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,20 +425,25 @@ private void configureRedirect(ProcessBuilder pb, Stdio stdio, boolean processor

/**
* Set the command to run and its arguments.
* <p>
* And the process builders will be cleared if it was set before.
*
* @param cmds the command and its arguments
* @return this, so that the method can be chained
* @throws NullPointerException if any of the elements in cmds is null
* @throws IndexOutOfBoundsException if cmds is empty
*/
public @NotNull Builder cmdline(@NotNull String... cmds) {
public @NotNull Builder cmdline(@NotNull String @NotNull ... cmds) {
if (cmds.length == 0) throw new IndexOutOfBoundsException("cmds is empty");
this.cmds = nonNullInArray(Arrays.copyOf(cmds, cmds.length));
this.pbs = null;
return this;
}

/**
* Set the command to run and its arguments.
* <p>
* And the process builders will be cleared if it was set before.
*
* @param cmds the command and its arguments
* @return this, so that the method can be chained
Expand All @@ -449,9 +454,46 @@ private void configureRedirect(ProcessBuilder pb, Stdio stdio, boolean processor
var a = cmds.toArray(String[]::new);
if (a.length == 0) throw new IndexOutOfBoundsException("cmds is empty");
this.cmds = nonNullInArray(a);
this.pbs = null;
return this;
}

/**
* Set the process builder for the command.
* <p>
* And the process builders be cleared if it was set before.
*
* @param pbs the process builder for the command
* @return this, so that the method can be chained
* @throws NullPointerException if pbs or any of the elements in pbs is null
* @throws IndexOutOfBoundsException if pbs is empty
*/
public @NotNull Builder useProcessBuilder(@NotNull ProcessBuilder @NotNull ... pbs) {
if (pbs.length == 0) throw new IndexOutOfBoundsException("pbs is empty");
this.pbs = nonNullInArray(pbs);
this.cmds = null;
return this;
}

/**
* Set the process builder for the command.
* <p>
* And the process builders will be cleared if it was set before.
*
* @param pbs the process builder for the command
* @return this, so that the method can be chained
* @throws NullPointerException if pbs or any of the elements in pbs is null
* @throws IndexOutOfBoundsException if pbs is empty
*/
public @NotNull Builder useProcessBuilder(@NotNull Collection<@NotNull ProcessBuilder> pbs) {
var pb = pbs.toArray(ProcessBuilder[]::new);
if (pb.length == 0) throw new IndexOutOfBoundsException("pbs is empty");
this.pbs = nonNullInArray(pb);
this.cmds = null;
return this;
}


/**
* Set the processor for the command's stdout.
*
Expand Down

0 comments on commit 5d047eb

Please sign in to comment.