-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCommandLineParser.cs
88 lines (80 loc) · 3.19 KB
/
CommandLineParser.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
/*
K5TOOL UV-K5 toolkit utility
Copyright (C) 2024 qrp73
https://github.com/qrp73/K5TOOL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
namespace K5TOOL
{
public static class CommandLineParser
{
private static Dictionary<string, Func<string, string[], int>> _commands = new Dictionary<string, Func<string, string[], int>>();
private static Dictionary<string, string> _helps = new Dictionary<string, string>();
private static Dictionary<string, string> _descs = new Dictionary<string, string>();
public static void RegisterCommand(string command, Func<string, string[], int> handler, string help, string desc=null)
{
_commands.Add(command, handler);
_helps.Add(command, help);
if (desc != null)
_descs.Add(command, desc);
}
public static string GetPortName(ref string[] args)
{
if (args.Length < 1) return null;
if (args[0] == "-port")
{
if (args.Length > 1)
{
var name = args[1];
args = args.Skip(2).ToArray();
return name;
}
return string.Empty; // port list request
}
return null;
}
public static int Process(string portName, ref string[] args)
{
if (args.Length > 0)
{
var command = args[0];
var commandArgs = args.Skip(1).ToArray();
if (_commands.ContainsKey(command))
{
return _commands[command](portName, commandArgs);
}
Console.WriteLine("ERROR: unknown command {0}", command);
}
// Print Usage
Console.WriteLine("K5TOOL v{0}", GetVersionString());
Console.WriteLine("UV-K5 radio toolkit (c)2024 qrp73");
Console.WriteLine();
Console.WriteLine("USGAGE: k5tool [-port <portName>] <command> [<args>]");
Console.WriteLine("Commands:");
foreach (var cmd in _commands.Keys)
{
Console.WriteLine(" {0} {1}", cmd, _helps[cmd]);
if (_descs.ContainsKey(cmd))
Console.WriteLine(" {0}", _descs[cmd]);
}
return -1;
}
private static string GetVersionString()
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
}