-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathCommandSettings.cs
202 lines (170 loc) · 7.68 KB
/
CommandSettings.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// Revit Batch Processor
//
// Copyright (c) 2020 Daniel Rumery, BVN
//
// 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 <http://www.gnu.org/licenses/>.
//
//
using System;
using System.Collections.Generic;
using System.Linq;
namespace BatchRvtUtil;
public static class CommandSettings
{
public const string SETTINGS_FILE_PATH_OPTION = "settings_file";
public const string LOG_FOLDER_PATH_OPTION = "log_folder";
public const string SESSION_ID_OPTION = "session_id";
public const string TASK_DATA_OPTION = "task_data";
public const string TEST_MODE_FOLDER_PATH_OPTION = "test_mode_folder_path";
public const string REVIT_FILE_LIST_OPTION = "file_list";
public const string REVIT_VERSION_OPTION = "revit_version";
public const string TASK_SCRIPT_FILE_PATH_OPTION = "task_script";
public const string DETACH_OPTION = "detach";
public const string CREATE_NEW_LOCAL_OPTION = "create_new_local";
public const string WORKSETS_OPTION = "worksets";
public const string CLOSE_ALL_WORKSETS_OPTION_VALUE = "close_all";
public const string OPEN_ALL_WORKSETS_OPTION_VALUE = "open_all";
public const string OPEN_LAST_VIEWED_WORKSETS_OPTION_VALUE = "last_viewed";
public const string AUDIT_ON_OPENING_OPTION = "audit";
public const string PER_FILE_PROCESSING_TIMEOUT_OPTION = "per_file_timeout";
public const string HELP_OPTION = "help";
private static readonly string[] ALL_VALID_OPTONS =
{
SETTINGS_FILE_PATH_OPTION,
LOG_FOLDER_PATH_OPTION,
SESSION_ID_OPTION,
TASK_DATA_OPTION,
TEST_MODE_FOLDER_PATH_OPTION,
REVIT_FILE_LIST_OPTION,
REVIT_VERSION_OPTION,
TASK_SCRIPT_FILE_PATH_OPTION,
DETACH_OPTION,
CREATE_NEW_LOCAL_OPTION,
WORKSETS_OPTION,
AUDIT_ON_OPENING_OPTION,
PER_FILE_PROCESSING_TIMEOUT_OPTION,
HELP_OPTION
};
private static readonly Dictionary<string, Func<string, object>> OPTION_PARSERS =
new()
{
{ SETTINGS_FILE_PATH_OPTION, ParseExistingFilePathOptionValue },
{ LOG_FOLDER_PATH_OPTION, ParseExistingFolderPathOptionValue },
{ SESSION_ID_OPTION, ParseTextOptionValue },
{ TASK_DATA_OPTION, ParseTextOptionValue },
{ TEST_MODE_FOLDER_PATH_OPTION, ParseTextOptionValue },
{ REVIT_VERSION_OPTION, optionValue => ParseRevitVersionOptionValue(optionValue) },
{ REVIT_FILE_LIST_OPTION, ParseExistingFilePathOptionValue },
{ TASK_SCRIPT_FILE_PATH_OPTION, ParseExistingFilePathOptionValue },
{ DETACH_OPTION, null },
{ CREATE_NEW_LOCAL_OPTION, null },
{ WORKSETS_OPTION, optionValue => ParseWorksetsOptionValue(optionValue) },
{ AUDIT_ON_OPENING_OPTION, null },
{ PER_FILE_PROCESSING_TIMEOUT_OPTION, optionValue => ParsePositiveIntegerOptionValue(optionValue) },
{ HELP_OPTION, null }
};
public static string ParseTextOptionValue(string textOptionValue)
{
string parsedValue = null;
if (!string.IsNullOrWhiteSpace(textOptionValue)) parsedValue = textOptionValue.Trim();
return parsedValue;
}
public static int? ParsePositiveIntegerOptionValue(string integerOptionValue)
{
var parsed = int.TryParse(integerOptionValue, out var parsedValue);
return parsed && parsedValue > 0 ? parsedValue : null;
}
public static string ParseExistingFilePathOptionValue(string filePathOptionValue)
{
string parsedValue = null;
if (string.IsNullOrWhiteSpace(filePathOptionValue)) return null;
var fullFilePath = PathUtil.GetFullPath(filePathOptionValue);
if (PathUtil.FileExists(fullFilePath)) parsedValue = fullFilePath;
return parsedValue;
}
public static string ParseExistingFolderPathOptionValue(string folderPathOptionValue)
{
string parsedValue = null;
if (string.IsNullOrWhiteSpace(folderPathOptionValue)) return null;
var fullFolderPath = PathUtil.GetFullPath(folderPathOptionValue);
if (PathUtil.DirectoryExists(fullFolderPath)) parsedValue = fullFolderPath;
return parsedValue;
}
public static bool? ParseBooleanOptionValue(string booleanOptionValue)
{
bool? parsedValue = null;
if (string.IsNullOrWhiteSpace(booleanOptionValue)) return null;
if (new[] { "TRUE", "YES" }.Any(s =>
string.Equals(s, booleanOptionValue, StringComparison.CurrentCultureIgnoreCase)))
parsedValue = true;
else if (new[] { "FALSE", "NO" }.Any(s =>
string.Equals(
s, booleanOptionValue,
StringComparison.CurrentCultureIgnoreCase)))
parsedValue = false;
return parsedValue;
}
private static RevitVersion.SupportedRevitVersion? ParseRevitVersionOptionValue(string revitVersionOptionValue)
{
RevitVersion.SupportedRevitVersion? revitVersion = null;
if (string.IsNullOrWhiteSpace(revitVersionOptionValue)) return null;
if (RevitVersion.IsSupportedRevitVersionNumber(revitVersionOptionValue))
revitVersion = RevitVersion.GetSupportedRevitVersion(revitVersionOptionValue);
return revitVersion;
}
private static BatchRvt.WorksetConfigurationOption? ParseWorksetsOptionValue(string worksetsOptionValue)
{
var parsedTextOptionValue = ParseTextOptionValue(worksetsOptionValue);
return parsedTextOptionValue switch
{
OPEN_ALL_WORKSETS_OPTION_VALUE => BatchRvt.WorksetConfigurationOption.OpenAllWorksets,
CLOSE_ALL_WORKSETS_OPTION_VALUE => BatchRvt.WorksetConfigurationOption.CloseAllWorksets,
OPEN_LAST_VIEWED_WORKSETS_OPTION_VALUE => BatchRvt.WorksetConfigurationOption.OpenLastViewed,
_ => null
};
}
public static Dictionary<string, object> GetCommandLineOptions()
{
return OPTION_PARSERS
.Select(
kv => new { Option = kv.Key, Parser = kv.Value }
).ToDictionary(
optionAndValue => optionAndValue.Option,
optionAndValue => optionAndValue.Parser != null
? optionAndValue.Parser(CommandLineUtil.GetCommandLineOption(optionAndValue.Option))
: CommandLineUtil.HasCommandLineOption(optionAndValue.Option, false)
);
}
public static IEnumerable<string> GetInvalidOptions()
{
return CommandLineUtil.GetAllCommandLineOptionSwitches()
.Where(optionSwitch => !ALL_VALID_OPTONS.Contains(optionSwitch))
.ToList();
}
public class Data
{
// BatchRvt itself will set this to the path of the generated log file.
public string GeneratedLogFilePath = null;
public string LogFolderPath = null;
public IEnumerable<string> RevitFileList = null;
public string SessionId = null;
// Programmatic options
public BatchRvtSettings Settings = null;
// Command line options
public string SettingsFilePath = null;
public string TaskData = null;
public string TestModeFolderPath = null;
}
}