Introduce flag for output tag file
This commit is contained in:
@@ -2,8 +2,8 @@ package xyz.naofal.jtags;
|
||||
|
||||
import static xyz.naofal.jtags.JtagsLogger.logger;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -13,6 +13,7 @@ public class Jtags {
|
||||
static class Options {
|
||||
List<String> sources = new ArrayList<>();
|
||||
boolean absolutePaths = false;
|
||||
Path output = Paths.get("tags");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -27,11 +28,30 @@ public class Jtags {
|
||||
String argument;
|
||||
while ((argument = arguments.poll()) != null) {
|
||||
switch (argument) {
|
||||
case "-h", "help", "-help", "--help":
|
||||
printUsage();
|
||||
System.exit(0);
|
||||
break;
|
||||
|
||||
case "-absolute":
|
||||
logger.config("Using absolute paths");
|
||||
options.absolutePaths = true;
|
||||
break;
|
||||
|
||||
case "-o", "-output":
|
||||
options.output =
|
||||
switch (arguments.poll()) {
|
||||
case null -> {
|
||||
logger.severe("Expected argument after " + argument);
|
||||
printUsage();
|
||||
System.exit(1);
|
||||
yield null;
|
||||
}
|
||||
case String output -> Paths.get(output);
|
||||
};
|
||||
logger.config("Writing tags to " + options.output.toString());
|
||||
break;
|
||||
|
||||
default:
|
||||
options.sources.add(argument);
|
||||
break;
|
||||
@@ -45,14 +65,7 @@ public class Jtags {
|
||||
var tags = TagCollector.collectTags(options);
|
||||
|
||||
var tagsWriter = new TagsWriter(options);
|
||||
try {
|
||||
tagsWriter.writeTagsFile(tags, new FileOutputStream("tags"));
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.severe(ex.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return tagsWriter.writeTagsFile(tags);
|
||||
}
|
||||
|
||||
static void printUsage() {
|
||||
@@ -60,7 +73,9 @@ public class Jtags {
|
||||
"""
|
||||
Usage: jtags [options] <sources...>
|
||||
Options:
|
||||
-absolute Use absolute paths for tag locations
|
||||
-absolute Use absolute paths for tag locations
|
||||
-o, -output <file> Write tags to specified <file>
|
||||
-h, -help Show this message
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ package xyz.naofal.jtags;
|
||||
|
||||
import static xyz.naofal.jtags.JtagsLogger.logger;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.PriorityQueue;
|
||||
@@ -12,9 +12,11 @@ import xyz.naofal.jtags.Jtags.Options;
|
||||
public record TagsWriter(Options options) {
|
||||
private static final int MAX_PATTERN_LENGTH = 96;
|
||||
|
||||
public void writeTagsFile(PriorityQueue<Tag> tags, OutputStream outputStream) {
|
||||
var writer = new OutputStreamWriter(outputStream);
|
||||
try {
|
||||
public boolean writeTagsFile(PriorityQueue<Tag> tags) {
|
||||
|
||||
try (var outputStream = new FileOutputStream(options().output.toFile());
|
||||
var writer = new OutputStreamWriter(outputStream); ) {
|
||||
|
||||
writer.write(
|
||||
"""
|
||||
!_TAG_FILE_ENCODING\tutf-8\t
|
||||
@@ -29,7 +31,10 @@ public record TagsWriter(Options options) {
|
||||
writer.flush();
|
||||
} catch (IOException ex) {
|
||||
logger.severe(ex.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void writeTag(Writer writer, Tag tag) throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user