Archived
0

JOpt Simple

This commit is contained in:
2018-06-03 16:03:40 +03:00
parent 54b2dd900e
commit a8c2769857
2 changed files with 65 additions and 8 deletions

View File

@@ -54,6 +54,11 @@
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>3.17</version> <version>3.17</version>
</dependency> </dependency>
<dependency>
<groupId>net.sf.jopt-simple</groupId>
<artifactId>jopt-simple</artifactId>
<version>6.0-alpha-2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -1,26 +1,78 @@
package ru.dmitriymx.corrector1s; package ru.dmitriymx.corrector1s;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import ru.dmitriymx.corrector1s.gui.MainApp; import ru.dmitriymx.corrector1s.gui.MainApp;
import java.io.File; import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List; import java.util.List;
@Slf4j @Slf4j
public class Main { public class Main {
public static final String VERSION = "1.0";
private static OptionParser buildOptionParser() {
final OptionParser optionParser = new OptionParser();
optionParser.acceptsAll(Arrays.asList("s", "source"), "Source Excel file")
.withRequiredArg()
.ofType(File.class)
.required();
optionParser.acceptsAll(Arrays.asList("t", "target"), "Target Excel file (default: target = source)")
.withRequiredArg()
.ofType(File.class);
optionParser.acceptsAll(Arrays.asList("f", "fee"), "Honorarium (in percentages)")
.withRequiredArg()
.ofType(Double.class)
.defaultsTo(9.0d);
optionParser.acceptsAll(Arrays.asList("h", "help"), "Help page. Display this message")
.forHelp();
optionParser.acceptsAll(Arrays.asList("v", "version"), "Version")
.forHelp();
return optionParser;
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
if (args.length == 0) { if (args.length == 0) {
MainApp.main(args); MainApp.main(args);
} else { } else {
File file = new File(args[0]); OptionParser parser = buildOptionParser();
File file1 = new File(args[1]); OptionSet optionSet;
try {
optionSet = parser.parse(args);
} catch (joptsimple.OptionException e) {
if (e.getMessage().contains("Missing")) {
log.error(e.getMessage());
} else {
log.error("", e);
}
return;
}
ExcelReader excelReader = new ExcelReader(file); if (optionSet.has("help")) {
List<ExcelReader.ExcelRecord> data = excelReader.getData(); System.out.printf("Version: %s\n", VERSION);
excelReader.correctStyle(); parser.printHelpOn(System.out);
excelReader.correctData(data); return;
excelReader.insertNewCols(); } else if (optionSet.has("version")) {
excelReader.save(file1); System.out.printf("Version: %s\n", VERSION);
return;
}
File sourceFile, targetFile;
sourceFile = (File) optionSet.valueOf("source");
if (optionSet.has("target")) {
targetFile = (File) optionSet.valueOf("target");
} else {
targetFile = sourceFile;
}
log.debug("Source file: {} (exists: {})", sourceFile.getAbsolutePath(), Files.exists(sourceFile.toPath()));
log.debug("Target file: {}", targetFile.getAbsolutePath());
log.debug("Honorarium: {}%", optionSet.valueOf("fee"));
} }
} }
} }