-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
125 lines (92 loc) · 3 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
using System.Xml;
using System.CommandLine;
using System;
namespace xmlnodes;
/// <summary>
/// Application Entry class
/// </summary>
public class Program
{
/// <summary>
///
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//create our file option
var fileArgument = new Argument<FileInfo?>(
name: "file",
description: "The xml file to read and display on the console.");
//create url option
var urlArgument = new Argument<string>(
name: "url",
description: "The xml URL to read and display on the console.");
//setup File Command
var fileCommand = new Command("file", "Parse xml in a local file");
fileCommand.AddArgument(fileArgument);
//setup URL command
var urlCommand = new Command("url", "Parse xml from the web");
urlCommand.AddArgument(urlArgument);
//create our root command and add the option
var rootCommand = new RootCommand("App to parse XML nodes");
rootCommand.AddCommand(fileCommand);
rootCommand.AddCommand(urlCommand);
//clear approach https://intellitect.com/blog/demystified-system-commandline/
fileCommand.SetHandler((FileInfo file) =>
{
ReadFile(file);
}, fileArgument);
urlCommand.SetHandler((string url) =>
{
ReadUrl(url);
}, urlArgument);
//invoke the commandline
rootCommand.Invoke(args);
}
/// <summary>
///
/// </summary>
/// <param name="url"></param>
static void ReadUrl(string url)
{
try
{
//create instand of the xml parser class
XmlNodes xml = new();
//call the parser entry point and pass the file to parse
var errorText = xml.Process(url);
//if the process returned a string write it to console
if (!string.IsNullOrEmpty(errorText))
{
Console.WriteLine($"Error: {errorText}");
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Method responsible for calling the XML parser
/// </summary>
/// <param name="file"></param>
static void ReadFile(FileInfo file)
{
try
{
//create instand of the xml parser class
XmlNodes xml = new();
//call the parser entry point and pass the file to parse
var errorText = xml.Process(file);
//if the process returned a string write it to console
if (!string.IsNullOrEmpty(errorText))
{
Console.WriteLine($"Error: {errorText}");
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}