-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptions.cs
More file actions
140 lines (114 loc) · 4.27 KB
/
Options.cs
File metadata and controls
140 lines (114 loc) · 4.27 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This file is part of bugreport.
// Copyright (c) 2006-2009 The bugreport Developers.
// See AUTHORS.txt for details.
// Licensed under the GNU General Public License, Version 3 (GPLv3).
// See LICENSE.txt for details.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
namespace bugreport
{
public static class Options
{
private static FileResolver fileResolver = new FileResolver();
public static String FunctionToAnalyze { get; private set; }
public static ReadOnlyCollection<String> Filenames { get; private set; }
public static Boolean IsTracing { get; private set; }
public static Boolean IsDebugging { get; private set; }
internal static FileResolver FileResolver
{
set { fileResolver = value; }
}
public static void ParseArgumentsFrom(String[] commandLine)
{
FunctionToAnalyze = GetFunctionToAnalyzeFrom(commandLine);
Filenames = GetFilenamesFrom(commandLine);
IsTracing = GetIsTracingFrom(commandLine);
IsDebugging = GetIsDebuggingFrom(commandLine);
if (IsDebugging)
{
IsTracing = true;
}
}
private static String GetFunctionToAnalyzeFrom(String[] arguments)
{
foreach (var argument in arguments)
{
if (!argument.StartsWith("--function"))
{
continue;
}
var indexOfEquals = argument.IndexOf('=');
if (indexOfEquals == -1)
{
throw new ArgumentException("--function malformed, should be in the form of --function=name");
}
return argument.Substring(indexOfEquals + 1);
}
return "_start";
}
private static ReadOnlyCollection<String> GetFilenamesFrom(String[] arguments)
{
var fileArgument = arguments[arguments.Length - 1];
if (fileArgument.Contains("*"))
{
String path;
if (fileArgument.Contains(Path.DirectorySeparatorChar.ToString()) ||
fileArgument.Contains(Path.AltDirectorySeparatorChar.ToString()))
{
var directorySeparatorIndex = fileArgument.LastIndexOf(Path.DirectorySeparatorChar);
if (-1 == directorySeparatorIndex)
{
directorySeparatorIndex = fileArgument.LastIndexOf(Path.AltDirectorySeparatorChar);
}
path = fileArgument.Substring(0, directorySeparatorIndex);
}
else
{
path = Environment.CurrentDirectory;
}
var fileName = Path.GetFileName(fileArgument);
return fileResolver.GetFilesFromDirectory(path, fileName);
}
var fileNames = new List<String>();
foreach (var argument in arguments)
{
if (!argument.StartsWith("--"))
{
fileNames.Add(argument);
}
}
return fileNames.AsReadOnly();
}
private static Boolean GetIsTracingFrom(IEnumerable<string> arguments)
{
foreach (var argument in arguments)
{
if (argument == "--trace")
{
return true;
}
}
return false;
}
private static Boolean GetIsDebuggingFrom(IEnumerable<string> arguments)
{
foreach (var argument in arguments)
{
if (argument == "--debug")
{
return true;
}
}
return false;
}
}
internal class FileResolver
{
public virtual ReadOnlyCollection<String> GetFilesFromDirectory(String path, String fileName)
{
return new ReadOnlyCollection<String>(Directory.GetFiles(path, fileName));
}
}
}