Skip to content

Commit

Permalink
Add command line option to print version
Browse files Browse the repository at this point in the history
  • Loading branch information
unkish authored and joelittlejohn committed Jan 5, 2023
1 parent 2d55620 commit 8e77206
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 deletions jsonschema2pojo-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
</includes>
<targetPath>${project.build.directory}</targetPath>
</resource>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import org.jsonschema2pojo.AllFileFilter;
Expand Down Expand Up @@ -249,6 +251,9 @@ public class Arguments implements GenerationConfig {
@Parameter(names = { "--useJakartaValidation" }, description = "Whether to use annotations from jakarta.validation package instead of javax.validation package when adding JSR-303/349 annotations to generated Java types")
private boolean useJakartaValidation = false;

@Parameter(names = { "-v", "--version"}, description = "Print version information", help = true)
private boolean printVersion = false;

private static final int EXIT_OKAY = 0;
private static final int EXIT_ERROR = 1;

Expand All @@ -274,8 +279,13 @@ public Arguments parse(String[] args) {
if (this.showHelp) {
jCommander.usage();
exit(EXIT_OKAY);
} else if (printVersion) {
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("version.properties"));
jCommander.getConsole().println(jCommander.getProgramName() + " version " + properties.getProperty("version"));
exit(EXIT_OKAY);
}
} catch (ParameterException e) {
} catch (IOException | ParameterException e) {
System.err.println(e.getMessage());
jCommander.usage();
exit(EXIT_ERROR);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=${project.version}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

Expand Down Expand Up @@ -127,7 +126,7 @@ public void allOptionalArgsCanBeOmittedAndDefaultsPrevail() {
}

@Test
public void missingArgsCausesHelp() throws IOException {
public void missingArgsCausesHelp() {
ArgsForTest args = (ArgsForTest) new ArgsForTest().parse(new String[] {});

assertThat(args.status, is(1));
Expand All @@ -137,13 +136,21 @@ public void missingArgsCausesHelp() throws IOException {
}

@Test
public void requestingHelpCausesHelp() throws IOException {
public void requestingHelpCausesHelp() {
ArgsForTest args = (ArgsForTest) new ArgsForTest().parse(new String[] { "--help" });

assertThat(args.status, is(notNullValue()));
assertThat(new String(systemOutCapture.toByteArray(), StandardCharsets.UTF_8), is(containsString("Usage: jsonschema2pojo")));
}

@Test
public void requestingVersionCausesVersion() {
ArgsForTest args = (ArgsForTest) new ArgsForTest().parse(new String[] { "--version" });

assertThat(args.didExit(), is(true));
assertThat(new String(systemOutCapture.toByteArray(), StandardCharsets.UTF_8).matches("(?s)jsonschema2pojo version \\d.*"), is(true));
}

private File theFile(String path) {
return new File(path);
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.30</version>
<version>1.82</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down

0 comments on commit 8e77206

Please sign in to comment.