mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-08-20 08:25:09 +08:00
passing -r will repeat parsing the set n times, -f will run a single file
This commit is contained in:
parent
52c0635273
commit
3131dae49e
@ -30,6 +30,9 @@ namespace UglyToad.PdfPig.ConsoleRunner
|
|||||||
public required string SuppliedDirectoryPath { get; init; }
|
public required string SuppliedDirectoryPath { get; init; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private const string FileSymbol = "f";
|
||||||
|
private const string RepeatSymbol = "r";
|
||||||
|
|
||||||
private static IReadOnlyList<OptionalArg> GetSupportedArgs() =>
|
private static IReadOnlyList<OptionalArg> GetSupportedArgs() =>
|
||||||
[
|
[
|
||||||
new OptionalArg
|
new OptionalArg
|
||||||
@ -49,6 +52,18 @@ namespace UglyToad.PdfPig.ConsoleRunner
|
|||||||
SupportsValue = true,
|
SupportsValue = true,
|
||||||
ShortSymbol = "l",
|
ShortSymbol = "l",
|
||||||
Symbol = "limit"
|
Symbol = "limit"
|
||||||
|
},
|
||||||
|
new OptionalArg
|
||||||
|
{
|
||||||
|
SupportsValue = true,
|
||||||
|
ShortSymbol = "f",
|
||||||
|
Symbol = "file"
|
||||||
|
},
|
||||||
|
new OptionalArg
|
||||||
|
{
|
||||||
|
SupportsValue = true,
|
||||||
|
ShortSymbol = "r",
|
||||||
|
Symbol = "repeats"
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -151,6 +166,8 @@ namespace UglyToad.PdfPig.ConsoleRunner
|
|||||||
var noRecursionMode = parsed.SuppliedArgs.Any(x => x.ShortSymbol == "nr");
|
var noRecursionMode = parsed.SuppliedArgs.Any(x => x.ShortSymbol == "nr");
|
||||||
var outputOpt = parsed.SuppliedArgs.SingleOrDefault(x => x.ShortSymbol == "o" && x.Value != null);
|
var outputOpt = parsed.SuppliedArgs.SingleOrDefault(x => x.ShortSymbol == "o" && x.Value != null);
|
||||||
|
|
||||||
|
var fileOpt = parsed.SuppliedArgs.SingleOrDefault(x => x.ShortSymbol == FileSymbol && x.Value != null);
|
||||||
|
|
||||||
var hasError = false;
|
var hasError = false;
|
||||||
var errorBuilder = new StringBuilder();
|
var errorBuilder = new StringBuilder();
|
||||||
var fileList = Directory.GetFiles(
|
var fileList = Directory.GetFiles(
|
||||||
@ -160,6 +177,18 @@ namespace UglyToad.PdfPig.ConsoleRunner
|
|||||||
.OrderBy(x => x).ToList();
|
.OrderBy(x => x).ToList();
|
||||||
var runningCount = 0;
|
var runningCount = 0;
|
||||||
|
|
||||||
|
if (fileOpt?.Value != null)
|
||||||
|
{
|
||||||
|
fileList = fileList.Where(x => x.EndsWith(fileOpt.Value, StringComparison.OrdinalIgnoreCase)).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
var repeatOpt = parsed.SuppliedArgs.SingleOrDefault(x => x.ShortSymbol == RepeatSymbol);
|
||||||
|
|
||||||
|
var repeats = 1;
|
||||||
|
if (repeatOpt?.Value != null && int.TryParse(repeatOpt.Value, CultureInfo.InvariantCulture, out repeats))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
Console.WriteLine($"Found {fileList.Count} files.");
|
Console.WriteLine($"Found {fileList.Count} files.");
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
|
||||||
@ -168,82 +197,85 @@ namespace UglyToad.PdfPig.ConsoleRunner
|
|||||||
var dataList = new List<DataRecord>();
|
var dataList = new List<DataRecord>();
|
||||||
|
|
||||||
var sw = new Stopwatch();
|
var sw = new Stopwatch();
|
||||||
foreach (var file in fileList)
|
for (int i = 0; i < repeats; i++)
|
||||||
{
|
{
|
||||||
if (maxCount.HasValue && runningCount >= maxCount)
|
foreach (var file in fileList)
|
||||||
{
|
{
|
||||||
break;
|
if (maxCount.HasValue && runningCount >= maxCount)
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var numWords = 0;
|
|
||||||
var numPages = 0;
|
|
||||||
long openMicros;
|
|
||||||
long totalPageMicros;
|
|
||||||
|
|
||||||
sw.Reset();
|
|
||||||
sw.Start();
|
|
||||||
|
|
||||||
using (var pdfDocument = PdfDocument.Open(file))
|
|
||||||
{
|
{
|
||||||
sw.Stop();
|
break;
|
||||||
|
|
||||||
openMicros = sw.Elapsed.Microseconds;
|
|
||||||
|
|
||||||
sw.Start();
|
|
||||||
|
|
||||||
foreach (var page in pdfDocument.GetPages())
|
|
||||||
{
|
|
||||||
numPages++;
|
|
||||||
foreach (var word in page.GetWords())
|
|
||||||
{
|
|
||||||
if (word != null)
|
|
||||||
{
|
|
||||||
numWords++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sw.Stop();
|
|
||||||
totalPageMicros = sw.Elapsed.Microseconds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var filename = Path.GetFileName(file);
|
try
|
||||||
|
|
||||||
var size = new FileInfo(file);
|
|
||||||
|
|
||||||
var item = new DataRecord
|
|
||||||
{
|
{
|
||||||
FileName = filename,
|
var numWords = 0;
|
||||||
OpenCostMicros = openMicros,
|
var numPages = 0;
|
||||||
Pages = numPages,
|
long openMicros;
|
||||||
Size = size.Length,
|
long totalPageMicros;
|
||||||
Words = numWords,
|
|
||||||
TotalCostMicros = totalPageMicros + openMicros,
|
|
||||||
PerPageMicros = Math.Round(totalPageMicros / (double)Math.Max(numPages, 1), 2)
|
|
||||||
};
|
|
||||||
|
|
||||||
dataList.Add(item);
|
sw.Reset();
|
||||||
|
sw.Start();
|
||||||
|
|
||||||
PrintTableColumns(
|
using (var pdfDocument = PdfDocument.Open(file))
|
||||||
item.FileName,
|
{
|
||||||
item.Size,
|
sw.Stop();
|
||||||
item.Words,
|
|
||||||
item.Pages,
|
openMicros = sw.Elapsed.Microseconds;
|
||||||
item.OpenCostMicros,
|
|
||||||
item.TotalCostMicros,
|
sw.Start();
|
||||||
item.PerPageMicros);
|
|
||||||
|
foreach (var page in pdfDocument.GetPages())
|
||||||
|
{
|
||||||
|
numPages++;
|
||||||
|
foreach (var word in page.GetWords())
|
||||||
|
{
|
||||||
|
if (word != null)
|
||||||
|
{
|
||||||
|
numWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sw.Stop();
|
||||||
|
totalPageMicros = sw.Elapsed.Microseconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
var filename = Path.GetFileName(file);
|
||||||
|
|
||||||
|
var size = new FileInfo(file);
|
||||||
|
|
||||||
|
var item = new DataRecord
|
||||||
|
{
|
||||||
|
FileName = filename,
|
||||||
|
OpenCostMicros = openMicros,
|
||||||
|
Pages = numPages,
|
||||||
|
Size = size.Length,
|
||||||
|
Words = numWords,
|
||||||
|
TotalCostMicros = totalPageMicros + openMicros,
|
||||||
|
PerPageMicros = Math.Round(totalPageMicros / (double)Math.Max(numPages, 1), 2)
|
||||||
|
};
|
||||||
|
|
||||||
|
dataList.Add(item);
|
||||||
|
|
||||||
|
PrintTableColumns(
|
||||||
|
item.FileName,
|
||||||
|
item.Size,
|
||||||
|
item.Words,
|
||||||
|
item.Pages,
|
||||||
|
item.OpenCostMicros,
|
||||||
|
item.TotalCostMicros,
|
||||||
|
item.PerPageMicros);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
hasError = true;
|
||||||
|
errorBuilder.AppendLine($"Parsing document {file} failed due to an error.")
|
||||||
|
.Append(ex)
|
||||||
|
.AppendLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
runningCount++;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
hasError = true;
|
|
||||||
errorBuilder.AppendLine($"Parsing document {file} failed due to an error.")
|
|
||||||
.Append(ex)
|
|
||||||
.AppendLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
runningCount++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasError)
|
if (hasError)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user