JOpt Simple
This commit is contained in:
@@ -1,26 +1,78 @@
|
||||
package ru.dmitriymx.corrector1s;
|
||||
|
||||
import joptsimple.OptionParser;
|
||||
import joptsimple.OptionSet;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.dmitriymx.corrector1s.gui.MainApp;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
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 {
|
||||
if (args.length == 0) {
|
||||
MainApp.main(args);
|
||||
} else {
|
||||
File file = new File(args[0]);
|
||||
File file1 = new File(args[1]);
|
||||
OptionParser parser = buildOptionParser();
|
||||
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);
|
||||
List<ExcelReader.ExcelRecord> data = excelReader.getData();
|
||||
excelReader.correctStyle();
|
||||
excelReader.correctData(data);
|
||||
excelReader.insertNewCols();
|
||||
excelReader.save(file1);
|
||||
if (optionSet.has("help")) {
|
||||
System.out.printf("Version: %s\n", VERSION);
|
||||
parser.printHelpOn(System.out);
|
||||
return;
|
||||
} else if (optionSet.has("version")) {
|
||||
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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user