-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCommandLineOptions.cs
More file actions
96 lines (80 loc) · 3.62 KB
/
Copy pathCommandLineOptions.cs
File metadata and controls
96 lines (80 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
namespace CreateInvoiceFromStructuredData;
internal sealed class CommandLineOptions
{
public string InvoiceJsonPath { get; init; } = Path.Combine("data", "metadata.json");
public string LineItemsCsvPath { get; init; } = Path.Combine("data", "line-items.csv");
public string StyleJsonPath { get; init; } = Path.Combine("data", "style.json");
public string OutputPdfPath { get; init; } = "CreateInvoiceFromStructuredData-out.pdf";
public static bool IsHelpArgument(string arg)
{
return string.Equals(arg, "--help", StringComparison.OrdinalIgnoreCase) ||
string.Equals(arg, "-h", StringComparison.OrdinalIgnoreCase) ||
string.Equals(arg, "/?", StringComparison.OrdinalIgnoreCase);
}
public static CommandLineOptions Parse(string[] args)
{
string invoiceJsonPath = Path.Combine("data", "metadata.json");
string lineItemsCsvPath = Path.Combine("data", "line-items.csv");
string styleJsonPath = Path.Combine("data", "style.json");
string outputPdfPath = "CreateInvoiceFromStructuredData-out.pdf";
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg.ToLowerInvariant())
{
case "--invoice":
case "--metadata":
invoiceJsonPath = RequireValue(args, ref i, arg);
break;
case "--line-items":
lineItemsCsvPath = RequireValue(args, ref i, arg);
break;
case "--style":
styleJsonPath = RequireValue(args, ref i, arg);
break;
case "--output":
outputPdfPath = RequireValue(args, ref i, arg);
break;
default:
throw new CommandLineException($"Unknown option: {arg}");
}
}
return new CommandLineOptions
{
InvoiceJsonPath = invoiceJsonPath,
LineItemsCsvPath = lineItemsCsvPath,
StyleJsonPath = styleJsonPath,
OutputPdfPath = outputPdfPath
};
}
public static void PrintUsage()
{
Console.WriteLine("Usage:");
Console.WriteLine(" CreateInvoiceFromStructuredData.exe [options]");
Console.WriteLine();
Console.WriteLine("With no options, the sample reads:");
Console.WriteLine(" data/metadata.json");
Console.WriteLine(" data/line-items.csv");
Console.WriteLine(" data/style.json");
Console.WriteLine("and writes:");
Console.WriteLine(" CreateInvoiceFromStructuredData-out.pdf");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" --metadata <path> JSON file with seller, customer, and invoice metadata.");
Console.WriteLine(" --invoice <path> Alias for --metadata.");
Console.WriteLine(" --line-items <path> CSV file containing invoice line items.");
Console.WriteLine(" --style <path> JSON style configuration for fonts, colors, and sizing.");
Console.WriteLine(" --output <path> Output PDF path.");
Console.WriteLine(" --self-test Validate parsing and totals without creating a PDF.");
Console.WriteLine(" --help, -h, /? Show this help.");
}
private static string RequireValue(string[] args, ref int index, string optionName)
{
if (index + 1 >= args.Length)
{
throw new CommandLineException($"{optionName} requires a value.");
}
index++;
return args[index];
}
}