-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
123 lines (113 loc) · 3.75 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Arguments;
using System.IO;
using System.Linq;
namespace IDGen
{
internal class Program
{
private static void Main(string[] args)
{
ParserOptions options = new ParserOptions(true, "--", "-", "=");
Parser parser = new Parser(options);
NamedArgument<int> count = parser.CreateNamedArgument<int>("gen-count", "gc", "Gencount", "The count of IDs to generate; default=100.", 100, null);
NamedArgument<string> o = parser.CreateNamedArgument<string>("output", "o", "Ouput", "The file location to output the results of the ID generator; will append to existing file.", default, null);
FlagArgument<bool> overWrite = parser.CreateFlagArgument<bool>("create", "c", "Create", "Create the output file; will overwrite existing file.");
FlagArgument<bool> help = parser.CreateFlagArgument<bool>("help", "h", "Help", "Output parameter options to app.");
parser.AddNamedArgument(count).AddNamedArgument(o).AddFlagArgument(overWrite).AddFlagArgument(help);
FileInfo file = new FileInfo(Environment.GetCommandLineArgs()[0]);
List<string> parms = new List<string>();
parms.Add(file.Name.Replace(file.Extension, ".exe")); //First command is the application itself.
parms.AddRange(args);
try
{
ParsingResults pr = parser.Parse(parms);
if (pr.HasParsedValue(help.Destination) && pr.GetParsedValue<bool>(help.Destination))
{
writeHelpInfo(parser);
return;
}
int gc = pr.GetParsedValue<int>(count.Destination);
//IdGenerator initializer can be improved to include custom IdGeneratorOptions.
IdGenerator idGen = new IdGenerator(0);
IEnumerable<long> ids = idGen.Take(gc);
if (pr.HasParsedValue(o.Destination))
{
writeToFile(ids, pr.GetParsedValue<string>(o.Destination), pr.GetParsedValue<bool>(overWrite.Destination));
}
else
{
foreach (long id in ids) { Console.WriteLine(id); }
}
}
catch (Exception e)
{
Console.WriteLine($"{e.GetType()}: {e.Message}");
writeHelpInfo(parser);
}
}
private static void writeHelpInfo(Parser parser)
{
if (parser.NamedArguments.Any()) { Console.WriteLine("Arguments"); }
foreach (Argument arg in parser.NamedArguments)
{
Console.WriteLine($"\t--{arg.Name} -{arg.Alias}\t\t: {arg.Help}");
}
if (parser.NamedArguments.Any())
{
Console.WriteLine();
Console.WriteLine(@"Example: idGenExe.exe -gc=100 -o=C:\temp\ids.txt -ow");
Console.WriteLine();
}
if (parser.PositionalArguments.Any()) { Console.WriteLine("PositionalArguments"); }
foreach (Argument arg in parser.PositionalArguments)
{
Console.WriteLine($"\t--{arg.Name} -{arg.Alias}\t\t: {arg.Help}");
}
if (parser.FlagArguments.Any()) { Console.WriteLine("Flags"); }
foreach (Argument arg in parser.FlagArguments)
{
Console.WriteLine($"\t--{arg.Name} -{arg.Alias}\t\t: {arg.Help}");
}
if (parser.Commands.Any()) { Console.WriteLine("Commands"); }
foreach (Command cmd in parser.Commands)
{
Console.WriteLine($"\t--{cmd.Name} -{cmd.Alias}");
}
}
private static void writeToFile(IEnumerable<long> ids, string path, bool overwrite = false)
{
FileInfo info = new FileInfo(path);
if (info.Directory.Exists)
{
if (!info.Exists) { File.AppendAllText(path, String.Empty); }
if (overwrite)
{
using (StreamWriter writer = info.CreateText())
{
foreach (long id in ids)
{
writer.WriteLine(id);
}
}
}
else
{
using (StreamWriter writer = info.AppendText())
{
foreach (long id in ids)
{
writer.WriteLine(id);
}
}
}
}
else
{
throw new InvalidOperationException($"The file and path ({path}) do not exist.");
}
}
}
}