-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
91 lines (74 loc) · 2.9 KB
/
Program.cs
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
namespace PicoArgs_dotnet;
internal static class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Demo of PicoArgs.cs class, eg:");
Console.WriteLine("PicoArgs-dotnet.exe --raw -i file1.txt -i file2.txt --file file3.txt --exclude something");
Console.WriteLine("PicoArgs-dotnet.exe --raw -i=file1.txt -i=\"file 2.txt\" --file file3.txt -i=\"-something=else\" --exclude=\"something else\"");
try {
//var pico = new PicoArgs("--raw -f file1.txt -f file2.txt --file file3.txt --exclude something");
var pico = new PicoArgs(args);
var help = pico.Contains("-h", "-?", "--help");
// if we want help, just bail here
if (help) {
Console.WriteLine(HelpMessage);
return;
}
// parse the rest of the command line
var raw = pico.Contains("-r", "--raw");
var fast = pico.Contains("-f", "--fast");
var slow = pico.Contains("-s", "--slow");
var files = pico.GetMultipleParams("-i", "--file");
var exclude = pico.GetParamOpt("-e", "--exclude") ?? "example-exclude";
// we have finished, make sure there are no unused arguments
pico.Finished();
// show the results
if (files.Count == 0) {
Console.WriteLine(HelpMessage);
Console.WriteLine("\r\nNo files specified");
return;
}
var filesString = string.Join(", ", files);
Console.WriteLine($"raw: {raw}");
Console.WriteLine($"fast: {fast}");
Console.WriteLine($"slow: {slow}");
Console.WriteLine($"files: {filesString}");
Console.WriteLine($"exclude: {exclude}");
}
catch (PicoArgsException ex) {
Console.WriteLine($"PICOARGS ERROR: {ex.Message}");
}
catch (Exception ex) {
Console.WriteLine($"ERROR: {ex.Message}");
}
}
#pragma warning disable IDE0051 // Remove unused private members
/// <summary>
/// Alternative usage to the example in Main() above.
/// This wraps the parsing of the command line, and returns the results as a tuple.
/// </summary>
private static (bool help, DirectoryInfo folder, string pattern, bool verbose) ParseCommandLineWrapper(string[] args)
{
// this will throw when it leaves scope if there are any unused arguments
using var pico = new PicoArgsDisposable(args);
var help = pico.Contains("-h", "-?", "--help");
// if we want help, just bail here, don't bother parsing the rest
if (help) {
return (true, null!, null!, false);
}
// parse the rest of the command line
var folder = new DirectoryInfo(pico.GetParamOpt("-f", "--folder") ?? ".");
var pattern = pico.GetParamOpt("-p", "--pattern") ?? "*.txt";
var verbose = pico.Contains("-v", "--verbose");
return (false, folder, pattern, verbose);
}
private const string HelpMessage = """
Usage: PicoArgs-dotnet.exe [options]
Options:
-f, --file <filename> File(s) to search (required)
-e, --exclude <pattern> Exclude pattern (default 'example-exclude')
-r, --raw Raw output
-h, --help, -? Help information
""";
}