-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdarg.cs
107 lines (98 loc) · 3.39 KB
/
cmdarg.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
using System;
using System.Collections.Generic;
using System.Text;
namespace xayrga
{
public static class cmdarg
{
public static string[] cmdargs;
public static string assertArg(int argn, string assert)
{
if (cmdargs.Length <= argn)
{
Console.WriteLine("Missing required argument #{0} for '{1}'", argn, assert);
Environment.Exit(0);
}
return cmdargs[argn];
}
public static string findDynamicStringArgument(string name, string def)
{
for (int i = 0; i < cmdargs.Length; i++)
if (cmdargs[i] == name || cmdargs[i] == "-" + name)
{
if (cmdargs.Length >= i + 1)
{
return cmdargs[i + 1];
}
break;
}
return def;
}
public static int findDynamicNumberArgument(string name, int def)
{
for (int i = 0; i < cmdargs.Length; i++)
if (cmdargs[i] == name || cmdargs[i] == "-" + name)
{
if (cmdargs.Length < i + 1)
{
int v = 0;
var ok = Int32.TryParse(cmdargs[i + 1], out v);
if (!ok)
{
Console.WriteLine($"Invalid parameter for '{cmdargs[i]}' (Number expected, couldn't parse '{cmdargs[i + 1]}' as a number.)");
Environment.Exit(0);
}
}
else
Console.WriteLine($"Number argument for '{cmdargs[i]}' expected.");
break;
}
return def;
}
public static bool findDynamicFlagArgument(string name)
{
for (int i = 0; i < cmdargs.Length; i++)
if (cmdargs[i] == name || cmdargs[i] == "-" + name )
return true;
return false;
}
public static int assertArgNum(int argn, string assert)
{
if (cmdargs.Length <= argn)
{
Console.WriteLine("Missing required argument #{0} for '{1}'", argn, assert);
Environment.Exit(0);
}
int b = 1;
var w = Int32.TryParse(cmdargs[argn], out b);
if (w == false)
{
Console.WriteLine("Cannot parse argument #{0} for '{1}' (expected number, got {2}) ", argn, assert, cmdargs[argn]);
Environment.Exit(0);
}
return b;
}
public static string tryArg(int argn, string assert)
{
if (cmdargs.Length <= argn)
{
if (assert != null)
Console.WriteLine("No argument #{0} specified {1}.", argn, assert);
return null;
}
return cmdargs[argn];
}
public static void assert(string text, params object[] wtf)
{
Console.WriteLine(text, wtf);
Environment.Exit(0);
}
public static void assert(bool cond, string text, params object[] wtf)
{
if (cond == false)
return;
Console.WriteLine(text, wtf);
Environment.Exit(0);
}
}
}