Skip to content

Commit 59851fc

Browse files
committed
Merge pull request #8 from veraPDF/extended-cli-options
Extended cli options
2 parents 5c6cdc5 + a04a83c commit 59851fc

File tree

10 files changed

+1001
-645
lines changed

10 files changed

+1001
-645
lines changed

vera-cli/pom.xml

+62-54
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,63 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2-
<modelVersion>4.0.0</modelVersion>
3-
<parent>
4-
<groupId>org.verapdf.pdfbox</groupId>
5-
<artifactId>pdfbox-rest</artifactId>
6-
<version>0.0.1-SNAPSHOT</version>
7-
</parent>
8-
<groupId>org.verapdf</groupId>
9-
<artifactId>vera-cli</artifactId>
10-
<name>VeraPDF CLI</name>
11-
<description>Command line invocation of the Vera PDF validator.</description>
12-
<build>
13-
<plugins>
14-
<plugin>
15-
<groupId>org.apache.maven.plugins</groupId>
16-
<artifactId>maven-assembly-plugin</artifactId>
17-
<configuration>
18-
<descriptorRefs>
19-
<descriptorRef>jar-with-dependencies</descriptorRef>
20-
</descriptorRefs>
21-
<archive>
22-
<manifest>
23-
<addClasspath>true</addClasspath>
24-
<mainClass>org.verapdf.cli.VeraPdfCli</mainClass>
25-
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
26-
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
27-
</manifest>
28-
</archive>
29-
</configuration>
30-
<executions>
31-
<execution>
32-
<id>make-assembly</id> <!-- this is used for inheritance merges -->
33-
<phase>package</phase> <!-- bind to the packaging phase -->
34-
<goals>
35-
<goal>single</goal>
36-
</goals>
37-
</execution>
38-
</executions>
39-
</plugin>
40-
</plugins>
41-
</build>
42-
43-
<dependencies>
44-
<dependency>
45-
<groupId>org.verapdf.pdfbox</groupId>
46-
<artifactId>verapdf-core</artifactId>
47-
<version>0.0.1-SNAPSHOT</version>
48-
</dependency>
49-
<dependency>
50-
<groupId>commons-cli</groupId>
51-
<artifactId>commons-cli</artifactId>
52-
<version>1.2</version>
53-
</dependency>
54-
</dependencies>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.verapdf.pdfbox</groupId>
6+
<artifactId>pdfbox-rest</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
</parent>
9+
<groupId>org.verapdf</groupId>
10+
<artifactId>vera-cli</artifactId>
11+
<name>VeraPDF CLI</name>
12+
<description>Command line invocation of the Vera PDF validator.</description>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-assembly-plugin</artifactId>
18+
<configuration>
19+
<descriptorRefs>
20+
<descriptorRef>jar-with-dependencies</descriptorRef>
21+
</descriptorRefs>
22+
<archive>
23+
<manifest>
24+
<addClasspath>true</addClasspath>
25+
<mainClass>org.verapdf.cli.VeraPdfCli</mainClass>
26+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
27+
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
28+
</manifest>
29+
</archive>
30+
</configuration>
31+
<executions>
32+
<execution>
33+
<id>make-assembly</id>
34+
<!-- this is used for inheritance merges -->
35+
<phase>package</phase>
36+
<!-- bind to the packaging phase -->
37+
<goals>
38+
<goal>single</goal>
39+
</goals>
40+
</execution>
41+
</executions>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
<dependencies>
47+
<dependency>
48+
<groupId>org.verapdf.pdfbox</groupId>
49+
<artifactId>verapdf-core</artifactId>
50+
<version>0.0.1-SNAPSHOT</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>commons-cli</groupId>
54+
<artifactId>commons-cli</artifactId>
55+
<version>1.2</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.beust</groupId>
59+
<artifactId>jcommander</artifactId>
60+
<version>1.47</version>
61+
</dependency>
62+
</dependencies>
5563
</project>
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,73 @@
1-
/**
2-
*
3-
*/
4-
package org.verapdf.cli;
5-
6-
import org.apache.commons.cli.CommandLine;
7-
import org.apache.commons.cli.CommandLineParser;
8-
import org.apache.commons.cli.GnuParser;
9-
import org.apache.commons.cli.HelpFormatter;
10-
import org.apache.commons.cli.Option;
11-
import org.apache.commons.cli.OptionBuilder;
12-
import org.apache.commons.cli.Options;
13-
import org.apache.commons.cli.ParseException;
14-
import org.verapdf.VeraPdfTaskConfig;
15-
import org.verapdf.pdfa.spec.PdfaFlavour;
16-
import org.verapdf.pdfa.spec.PdfaSpecifications;
17-
18-
/**
19-
* @author cfw
20-
*
21-
*/
22-
public class VeraPdfCli {
23-
/**
24-
* The application name, used to invoke the CLI application
25-
*/
26-
public static final String APP_NAME = "veraPdf"; //$NON-NLS-1$
27-
28-
// String constants for CLI Options
29-
private static final String VALIDATE_OPT = "validate"; //$NON-NLS-1$
30-
private static final String VALIDATE_OPT_DESC = "Request PDF/A validation";
31-
private static final String FILE_OPT = "file"; //$NON-NLS-1$
32-
private static final String FILE_OPT_ARG = "filepath";
33-
private static final String FILE_OPT_DESC = "Absolute path of file to validate.";
34-
private static final String FIXMETADATA_OPT = "fixmetadata"; //$NON-NLS-1$
35-
private static final String FIXMETADATA_OPT_DESC = "Request automatic fix of metadata.";
36-
private static final String URL_OPT = "url"; //$NON-NLS-1$
37-
private static final String URL_OPT_ARG = "URL";
38-
private static final String URL_OPT_DESC = "URI encoded URL of file to validate.";
39-
private static final String PDFA_OPT = "pdfa"; //$NON-NLS-1$
40-
private static final String PDFA_OPT_ARG = "PDF/A flavour";
41-
private static final String PDFA_OPT_DESC = "PDF/A flavour to use for validation, can be (none|1a|1b|2a|2b|3a|3b|3u).";
42-
private static final String STOPERRORS_OPT = "stoperrors"; //$NON-NLS-1$
43-
private static final String STOPERRORS_OPT_ARG = "number";
44-
private static final String STOPERRORS_OPT_DESC = "The number of errors after which validation is interupted, must be an integer greater than 0. Default is to not stop on any error.";
45-
private static final String VERBOSITY_OPT = "verbosity"; //$NON-NLS-1$
46-
private static final String VERBOSITY_OPT_ARG = "number";
47-
private static final String VERBOSITY_OPT_DESC = "Control reporting verbosity, a number between 0 and 9 (inclusive), defaults to 3.";
48-
private static final String HELP_OPT = "help"; //$NON-NLS-1$
49-
private static final String HELP_OPT_DESC = "Print this message.";
50-
51-
// Create the options object
52-
private static final Options OPTIONS = new Options();
53-
static {
54-
Option help = new Option(HELP_OPT, HELP_OPT_DESC);
55-
Option validate = new Option(VALIDATE_OPT, VALIDATE_OPT_DESC);
56-
Option fixMetadata = new Option(FIXMETADATA_OPT, FIXMETADATA_OPT_DESC);
57-
@SuppressWarnings("static-access")
58-
Option file = OptionBuilder.withArgName(FILE_OPT_ARG).hasArg()
59-
.withDescription(FILE_OPT_DESC).create(FILE_OPT);
60-
@SuppressWarnings("static-access")
61-
Option url = OptionBuilder.withArgName(URL_OPT_ARG).hasArg()
62-
.withDescription(URL_OPT_DESC).create(URL_OPT);
63-
@SuppressWarnings("static-access")
64-
Option pdfa = OptionBuilder.withArgName(PDFA_OPT_ARG).hasArg()
65-
.withDescription(PDFA_OPT_DESC).create(PDFA_OPT);
66-
@SuppressWarnings("static-access")
67-
Option stopErrors = OptionBuilder.withArgName(STOPERRORS_OPT_ARG)
68-
.hasArg().withDescription(STOPERRORS_OPT_DESC)
69-
.create(STOPERRORS_OPT);
70-
@SuppressWarnings("static-access")
71-
Option verbosity = OptionBuilder.withArgName(VERBOSITY_OPT_ARG)
72-
.hasArg().withDescription(VERBOSITY_OPT_DESC)
73-
.create(VERBOSITY_OPT);
74-
75-
OPTIONS.addOption(help);
76-
OPTIONS.addOption(validate);
77-
OPTIONS.addOption(fixMetadata);
78-
OPTIONS.addOption(file);
79-
OPTIONS.addOption(url);
80-
OPTIONS.addOption(pdfa);
81-
OPTIONS.addOption(verbosity);
82-
OPTIONS.addOption(stopErrors);
83-
}
84-
85-
/**
86-
* Main CLI entry point, process the command line arguments
87-
*
88-
* @param args
89-
* Java.lang.String array of command line args, to be processed
90-
* using Apache commons CLI.
91-
*/
92-
public static void main(String[] args) {
93-
CommandLineParser gnuParser = new GnuParser();
94-
VeraPdfTaskConfig taskConfig = VeraPdfTaskConfig.defaultInstance();
95-
try {
96-
CommandLine cmdLine = gnuParser.parse(OPTIONS, args);
97-
98-
// If help requested then output help message and terminate
99-
if (cmdLine.hasOption(HELP_OPT) || args.length == 0) {
100-
outputHelpAndTerminate(0);
101-
}
102-
103-
taskConfig = createConfigFromCliOptions(cmdLine);
104-
} catch (ParseException excep) {
105-
System.err.println("Command line parsing failed, exception message: " + excep.getLocalizedMessage());
106-
excep.printStackTrace();
107-
outputHelpAndTerminate(1);
108-
}
109-
110-
}
111-
112-
private final static void outputHelpAndTerminate(final int exitCode) {
113-
HelpFormatter formatter = new HelpFormatter();
114-
formatter.printHelp(APP_NAME, OPTIONS);
115-
System.exit(exitCode);
116-
}
117-
118-
private final static VeraPdfTaskConfig createConfigFromCliOptions(final CommandLine cmdLine) {
119-
boolean validate = cmdLine.hasOption(VALIDATE_OPT);
120-
boolean fixMetadata = cmdLine.hasOption(FIXMETADATA_OPT);
121-
PdfaFlavour flavour = PdfaFlavour.NONE;
122-
if (cmdLine.hasOption(PDFA_OPT)) {
123-
String flavourString = cmdLine.getOptionValue(PDFA_OPT);
124-
flavour = PdfaSpecifications.byFlavourString(flavourString);
125-
}
126-
int verbosity = VeraPdfTaskConfig.VERBOSITY_DEFAULT;
127-
if (cmdLine.hasOption(VERBOSITY_OPT)) {
128-
verbosity = Integer.valueOf(cmdLine.getOptionValue(VERBOSITY_OPT)).intValue();
129-
}
130-
int stopErrors = VeraPdfTaskConfig.STOPERRORS_DEFAULT;
131-
if (cmdLine.hasOption(STOPERRORS_OPT)) {
132-
stopErrors = Integer.valueOf(cmdLine.getOptionValue(STOPERRORS_OPT)).intValue();
133-
}
134-
return VeraPdfTaskConfig.fromValues(flavour, validate, fixMetadata, verbosity, stopErrors);
135-
}
1+
/**
2+
*
3+
*/
4+
package org.verapdf.cli;
5+
6+
import com.beust.jcommander.JCommander;
7+
import org.verapdf.VeraPdfTaskConfig;
8+
import org.verapdf.cli.commands.CommandVeraPDF;
9+
10+
/**
11+
* @author cfw
12+
*
13+
*/
14+
public class VeraPdfCli {
15+
16+
/**
17+
* The application name, used to invoke the CLI application
18+
*/
19+
public static final String APP_NAME = "veraPdf"; //$NON-NLS-1$
20+
21+
private static final CommandVeraPDF commandVeraPDF;
22+
23+
static {
24+
commandVeraPDF = new CommandVeraPDF();
25+
}
26+
27+
/**
28+
* Main CLI entry point, process the command line arguments
29+
*
30+
* @param args
31+
* Java.lang.String array of command line args, to be processed
32+
* using Apache commons CLI.
33+
*/
34+
public static void main(String[] args) {
35+
JCommander jCommander = new JCommander();
36+
jCommander.addCommand(commandVeraPDF);
37+
VeraPdfTaskConfig taskConfig = VeraPdfTaskConfig.defaultInstance();
38+
39+
jCommander.parse(args);
40+
taskConfig = createConfigFromCliOptions(commandVeraPDF);
41+
}
42+
43+
/**
44+
* Creates instance of VeraPdfTaskConfig from parsed cli options
45+
* @param commandVeraPDF options used by VeraPDF software
46+
* @return an immutable VeraPdfTaskConfig object populated from the parsed options
47+
*/
48+
private final static VeraPdfTaskConfig createConfigFromCliOptions(final CommandVeraPDF commandVeraPDF) {
49+
//TODO: if no logic will be placed there than I prefer not to use intermediate variables
50+
boolean validate = commandVeraPDF.isValidate();
51+
String inputPath = commandVeraPDF.getInputPath();
52+
boolean inputPathURL = commandVeraPDF.isInputPathURL();
53+
String flavour = commandVeraPDF.getFlavour();
54+
String profile = commandVeraPDF.getProfile();
55+
boolean fixMetadata = commandVeraPDF.isFixMetadata();
56+
Integer verbosity = commandVeraPDF.getVerbosity();
57+
boolean progress = commandVeraPDF.isProgress();
58+
boolean progressToStdout = commandVeraPDF.isProgressToStdout();
59+
String progressPath = commandVeraPDF.getProgressPath();
60+
boolean progressPathURL = commandVeraPDF.isProgressPathURL();
61+
Integer stopErrors = commandVeraPDF.getStopErrors();
62+
String tempDir = commandVeraPDF.getTempDir();
63+
boolean output = commandVeraPDF.isOutput();
64+
boolean outputToInput = commandVeraPDF.isOutputToInput();
65+
String outputPath = commandVeraPDF.getOutputPath();
66+
boolean outputPathURL = commandVeraPDF.isOutputPathURL();
67+
String report = commandVeraPDF.getReport();
68+
return VeraPdfTaskConfig.fromValues(validate, inputPath, inputPathURL, flavour, profile,
69+
fixMetadata, verbosity, progress, progressToStdout, progressPath,
70+
progressPathURL, stopErrors, tempDir, output, outputToInput,
71+
outputPath, outputPathURL, report);
72+
}
13673
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.verapdf.cli.commands;
2+
3+
/**
4+
* All classes describing cli commands must inherit from this class.
5+
* @author Timur Kamalov
6+
*/
7+
public abstract class Command {
8+
9+
}

0 commit comments

Comments
 (0)