Skip to content

Commit f1f9d0e

Browse files
committed
Improve console log formatting and command line help messages. Test that settings file exists before use.
1 parent 5fa8b53 commit f1f9d0e

5 files changed

Lines changed: 103 additions & 35 deletions

File tree

ConformU/AlpacaProtocolTestManager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public async Task<int> TestAlpacaProtocol()
136136

137137
try
138138
{
139-
TL.LogMessage("TestAlpacaProtocol", $"Task started");
139+
// Create a blank line at the start of the console log
140+
Console.WriteLine("");
140141

141142
string clientHostAddress = $"{settings.AlpacaDevice.ServiceType.ToString().ToLowerInvariant()}://{settings.AlpacaDevice.IpAddress}:{settings.AlpacaDevice.IpPort}";
142143

@@ -339,6 +340,9 @@ public async Task<int> TestAlpacaProtocol()
339340
}
340341
}
341342

343+
// Create a blank line in the console log
344+
Console.WriteLine("");
345+
342346
// Set the return code to the number of errors + issues
343347
returnCode = errorMessages.Count + issueMessages.Count;
344348
}

ConformU/ConformLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void SetStatusMessage(string status)
167167
public new void LogMessage(string method, string message)
168168
{
169169
// Write the message to the console
170-
Console.WriteLine($"{method} {message}");
170+
Console.WriteLine($"{method}{(string.IsNullOrEmpty(method)?"":" ")}{message}");
171171

172172
// Write the message to the log file
173173
base.LogMessage(method,message);

ConformU/Pages/CheckAlpacaProtocol.razor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@
148148
// Clear the screen log
149149
screenLog = "";
150150

151+
// Add a blank line to the console log
152+
Console.WriteLine("");
153+
151154
// Create a task to run the conformance test
152155
conformanceTestTask = new Task<int>(() =>
153156
{

ConformU/Program.cs

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ public static async Task<int> Main(string[] args)
6666

6767
// ROOT command
6868
var rootCommand = new RootCommand($"Conform Universal {Update.ConformuVersionDisplayString}\r\nCopyright (c) 2021-{DateTime.Now.Year} Peter Simpson\r\n\r\n" +
69-
$"Use conformu [command] -h for information on options available in each command.");
69+
$"Enter conformu [command] -h for information on options available in each command.\r\n\r\n" +
70+
$"If no command or options are provided Conform Universal will start as a GUI application using default parameters."
71+
);
7072

7173
// CONFORMANCE command
72-
var conformanceCommand = new Command("conformance", "Check the specified device for ASCOM device interface conformance");
74+
var conformanceCommand = new Command("conformance", "Check the specified device for ASCOM device interface conformance with all tests enabled.");
7375

7476
// ALPACA PROTOCOL command
75-
var alpacaProtocolCommand = new Command("alpacaprotocol", "Check the specified device for Alpaca protocol conformance");
77+
var alpacaProtocolCommand = new Command("alpacaprotocol", "Check the specified Alpaca device for Alpaca protocol conformance");
7678

7779
// CONFORMANCE USING SETTINGS command
7880
var conformanceUsingSettingsCommand = new Command("conformance-settings", "Check the device configured in the settings file for ASCOM device interface conformance");
@@ -81,7 +83,7 @@ public static async Task<int> Main(string[] args)
8183
var alpacaUsingSettingsCommand = new Command("alpacaprotocol-settings", "Check the device configured in the settings file for Alpaca protocol conformance");
8284

8385
// START AS GUI command
84-
var startAsGuiCommand = new Command("gui", "Start Conform Universal as an interactive GUI application with options to change the log file, results file and settings file locations");
86+
var startAsGuiCommand = new Command("gui", "Start Conform Universal as a GUI application with options to change the log file, results file and settings file locations");
8587

8688
#endregion
8789

@@ -176,10 +178,10 @@ public static async Task<int> Main(string[] args)
176178
};
177179

178180
// LOG FILE PATH option
179-
Option<string> logFilePathOption = new(
181+
Option<DirectoryInfo> logFilePathOption = new(
180182
aliases: new string[] { "-p", "--logfilepath" },
181-
description: "Fully qualified path to the log file folder.\r\n" +
182-
"Overrides the default log file path used by the GUI application, but is ignored when the --logfilename option is used.")
183+
description: "Relative or fully qualified path to the log file folder.\r\n" +
184+
"Overrides the default GUI log file path, but is ignored when the --logfilename option is present.")
183185
{
184186
ArgumentHelpName = "PATH"
185187
};
@@ -191,16 +193,16 @@ public static async Task<int> Main(string[] args)
191193
if (OperatingSystem.IsWindows())
192194
{
193195
// Get the log file path
194-
string logFilePath = (string)result.GetValueOrDefault();
196+
DirectoryInfo logFilePath = (DirectoryInfo)result.GetValueOrDefault();
195197

196198
// Check each character to see if it matches an invalid character
197199
foreach (char invalidCharacter in Path.GetInvalidPathChars())
198200
{
199-
if (logFilePath.Contains(invalidCharacter)) // Found an invalid character detected
201+
if (logFilePath.FullName.Contains(invalidCharacter)) // Found an invalid character detected
200202
{
201203
// Set the error message
202204
Console.WriteLine($"\r\n{RED_TEXT}Found invalid log file path character: '{invalidCharacter}' ({(int)invalidCharacter:X2}){WHITE_TEXT}");
203-
result.ErrorMessage = $"\r\nLog file path contains invalid characters: {logFilePath}";
205+
result.ErrorMessage = $"\r\nLog file path contains invalid characters: {logFilePath.FullName}";
204206
}
205207
}
206208
}
@@ -209,8 +211,8 @@ public static async Task<int> Main(string[] args)
209211
// LOG FILE NAME option
210212
Option<FileInfo> logFileNameOption = new(
211213
aliases: new string[] { "-n", "--logfilename" },
212-
description: "Filename of the log file (fully qualified or relative to the current directory).\r\n" +
213-
"The default GUI log filename and location will be used if this option is omitted.")
214+
description: "Relative or fully qualified name of the log file.\r\n" +
215+
"The default GUI log file name will be used if this option is omitted.")
214216
{
215217
ArgumentHelpName = "FILENAME"
216218
};
@@ -245,21 +247,87 @@ public static async Task<int> Main(string[] args)
245247
// RESULTS FILE option
246248
Option<FileInfo> resultsFileOption = new(
247249
aliases: new string[] { "-r", "--resultsfile" },
248-
description: "Filename of the machine readable results file (fully qualified or relative to the current directory).\r\n" +
250+
description: "Relative or fully qualified name of the machine readable results file.\r\n" +
249251
"The default GUI filename and location will be used if this option is omitted.")
250252
{
251253
ArgumentHelpName = "FILENAME"
252254
};
253255

256+
// Add a validator for the results file name
257+
resultsFileOption.AddValidator(result =>
258+
{
259+
// Validate Windows file names
260+
if (OperatingSystem.IsWindows())
261+
{
262+
// Get the log file name as a FileInfo
263+
FileInfo resultsFileInfo = (FileInfo)result.GetValueOrDefault();
264+
265+
// Check each character to see if it matches an invalid character
266+
foreach (char invalidCharacter in Path.GetInvalidFileNameChars())
267+
{
268+
//Console.WriteLine($"Invalid log file name character: '{invalidCharacter}' ({(int)invalidCharacter:X2})");
269+
// Ignore colon and backslash, which are marked as invalid in a file name
270+
if ((invalidCharacter != '\\') & (invalidCharacter != ':'))
271+
{
272+
if (resultsFileInfo.FullName.Contains(invalidCharacter)) // Found an invalid character detected
273+
{
274+
// Set the error message
275+
Console.WriteLine($"\r\n{RED_TEXT}Found invalid results file name character: '{invalidCharacter}' ({(int)invalidCharacter:X2}){WHITE_TEXT}");
276+
result.ErrorMessage = $"\r\nResults file name contains invalid characters: {resultsFileInfo.FullName}";
277+
}
278+
}
279+
}
280+
}
281+
});
282+
254283
// SETTINGS FILE option
255284
Option<FileInfo> settingsFileOption = new(
256285
aliases: new string[] { "-s", "--settingsfile" },
257-
description: "Filename of the settings file to use (fully qualified or relative to the current directory).\r\n" +
258-
"The GUI application settings file will be used if this option is omitted.")
286+
description: "Relative or fully qualified name of the settings file.\r\n" +
287+
"The default GUI application settings file will be used if this option is omitted.")
259288
{
260289
ArgumentHelpName = "FILENAME"
261290
};
262291

292+
// Add a validator for the settings file name
293+
settingsFileOption.AddValidator(result =>
294+
{
295+
// Get the log file name as a FileInfo
296+
FileInfo settingsFileInfo = (FileInfo)result.GetValueOrDefault();
297+
298+
// Validate Windows file names
299+
if (OperatingSystem.IsWindows())
300+
{
301+
bool fileNameOk = true;
302+
303+
// Check each character to see if it matches an invalid character
304+
foreach (char invalidCharacter in Path.GetInvalidFileNameChars())
305+
{
306+
//Console.WriteLine($"Invalid log file name character: '{invalidCharacter}' ({(int)invalidCharacter:X2})");
307+
// Ignore colon and backslash, which are marked as invalid in a file name
308+
if ((invalidCharacter != '\\') & (invalidCharacter != ':'))
309+
{
310+
if (settingsFileInfo.FullName.Contains(invalidCharacter)) // Found an invalid character detected
311+
{
312+
// Set the error message
313+
Console.WriteLine($"\r\n{RED_TEXT}Found invalid settings file name character: '{invalidCharacter}' ({(int)invalidCharacter:X2}){WHITE_TEXT}");
314+
result.ErrorMessage = $"\r\nSettings file name contains invalid characters: {settingsFileInfo.FullName}";
315+
fileNameOk = false;
316+
}
317+
}
318+
}
319+
if (!fileNameOk)
320+
return;
321+
}
322+
323+
// Validate that the file exists
324+
if (!File.Exists(settingsFileInfo.FullName))
325+
{
326+
//Console.WriteLine($"\r\n{RED_TEXT}Settings file '{settingsFileInfo.FullName}' does not exist.{WHITE_TEXT}");
327+
result.ErrorMessage = $"\r\nSettings file '{settingsFileInfo.FullName}' does not exist.";
328+
}
329+
});
330+
263331
#endregion
264332

265333
#region Associate arguments and options with commands
@@ -274,45 +342,45 @@ public static async Task<int> Main(string[] args)
274342

275343
// CONFORMANCE COMMAND - add arguments and options
276344
conformanceCommand.AddArgument(deviceArgument);
277-
conformanceCommand.AddOption(settingsFileOption);
278345
conformanceCommand.AddOption(logFileNameOption);
279346
conformanceCommand.AddOption(logFilePathOption);
280347
conformanceCommand.AddOption(resultsFileOption);
348+
conformanceCommand.AddOption(settingsFileOption);
281349
conformanceCommand.AddOption(debugDiscoveryOption);
282350
conformanceCommand.AddOption(debugStartUpOption);
283351

284352
// ALPCA COMMAND - add arguments and options
285353
alpacaProtocolCommand.AddArgument(alpacaDeviceArgument);
286-
alpacaProtocolCommand.AddOption(settingsFileOption);
287354
alpacaProtocolCommand.AddOption(logFileNameOption);
288355
alpacaProtocolCommand.AddOption(logFilePathOption);
289356
alpacaProtocolCommand.AddOption(resultsFileOption);
357+
alpacaProtocolCommand.AddOption(settingsFileOption);
290358
alpacaProtocolCommand.AddOption(debugDiscoveryOption);
291359
alpacaProtocolCommand.AddOption(debugStartUpOption);
292360

293361
// ALPCA USING SETTINGS COMMAND - add options
294-
alpacaUsingSettingsCommand.AddOption(settingsFileOption);
295362
alpacaUsingSettingsCommand.AddOption(logFileNameOption);
296363
alpacaUsingSettingsCommand.AddOption(logFilePathOption);
297364
alpacaUsingSettingsCommand.AddOption(resultsFileOption);
365+
alpacaUsingSettingsCommand.AddOption(settingsFileOption);
298366
alpacaUsingSettingsCommand.AddOption(debugDiscoveryOption);
299367
alpacaUsingSettingsCommand.AddOption(debugStartUpOption);
300368

301369
// CONFORMANCE USING SETTINGS COMMAND - add options
302-
conformanceUsingSettingsCommand.AddOption(settingsFileOption);
303370
conformanceUsingSettingsCommand.AddOption(logFileNameOption);
304371
conformanceUsingSettingsCommand.AddOption(logFilePathOption);
305372
conformanceUsingSettingsCommand.AddOption(resultsFileOption);
373+
conformanceUsingSettingsCommand.AddOption(settingsFileOption);
306374
conformanceUsingSettingsCommand.AddOption(debugDiscoveryOption);
307375
conformanceUsingSettingsCommand.AddOption(debugStartUpOption);
308376

309377
// START AS GUI COMMAND - add options
310378
startAsGuiCommand.AddOption(logFileNameOption);
311379
startAsGuiCommand.AddOption(logFilePathOption);
312-
startAsGuiCommand.AddOption(debugDiscoveryOption);
313-
startAsGuiCommand.AddOption(debugStartUpOption);
314380
startAsGuiCommand.AddOption(resultsFileOption);
315381
startAsGuiCommand.AddOption(settingsFileOption);
382+
startAsGuiCommand.AddOption(debugDiscoveryOption);
383+
startAsGuiCommand.AddOption(debugStartUpOption);
316384

317385
#endregion
318386

@@ -559,7 +627,7 @@ static int RootCommandHandler(bool version)
559627
/// <param name="debugDiscovery"></param>
560628
/// <param name="resultsFile"></param>
561629
/// <param name="settingsFile"></param>
562-
static int StartGuiHandler(FileInfo file, string path, bool debugStartUp, bool debugDiscovery, FileInfo resultsFile, FileInfo settingsFile)
630+
static int StartGuiHandler(FileInfo file, DirectoryInfo path, bool debugStartUp, bool debugDiscovery, FileInfo resultsFile, FileInfo settingsFile)
563631
{
564632
// Initialise required variables required by several commands
565633
InitialiseVariables(file, path, debugStartUp, debugDiscovery, resultsFile, settingsFile);
@@ -745,7 +813,7 @@ static int RunAlpacaProtocolTest()
745813
/// <param name="resultsFileInfo"></param>
746814
/// <param name="settingsFileInfo"></param>
747815

748-
private static void InitialiseVariables(FileInfo logFileInfo, string logFilePath, bool debugStartUp, bool debugDiscovery, FileInfo resultsFileInfo, FileInfo settingsFileInfo)
816+
private static void InitialiseVariables(FileInfo logFileInfo, DirectoryInfo logPathInfo, bool debugStartUp, bool debugDiscovery, FileInfo resultsFileInfo, FileInfo settingsFileInfo)
749817
{
750818
argList = new();
751819

@@ -763,8 +831,7 @@ private static void InitialiseVariables(FileInfo logFileInfo, string logFilePath
763831
}
764832

765833
string logFileName = logFileInfo?.FullName ?? "";
766-
if (string.IsNullOrEmpty(logFilePath))
767-
logFilePath = "";
834+
string logFilePath = logPathInfo?.FullName ?? "";
768835

769836
// Use fully qualified file name if present, otherwise use log file path and relative file name
770837
if (Path.IsPathFullyQualified(logFileName)) // Full file name and path provided so split into path and filename and ignore any supplied log file path

publish.cmd

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ cd
88
cd J:\ConformU
99

1010
echo *** Build application
11-
MSBuild "J:\ConformU\ConformU.sln" /p:Configuration=Debug /p:Platform="Any CPU" /t:Restore
11+
MSBuild ConformU.sln /p:Configuration=Debug /p:Platform="Any CPU" /t:Restore
1212
cd
13-
MSBuild "J:\ConformU\ConformU.sln" /p:Configuration=Debug /p:Platform="Any CPU" /t:Rebuild
13+
MSBuild ConformU.sln /p:Configuration=Debug /p:Platform="Any CPU" /t:Rebuild
1414
echo *** Completed Build
1515

1616
echo *** Publishing MacOS Intel silicon
@@ -38,12 +38,6 @@ dotnet publish -c Debug /p:Platform="Any CPU" -r linux-x64 --framework net7.0 --
3838
bsdtar -cJf publish/conformu.linux-x64.needsexec.tar.xz -C ConformU\bin\Debug\net7.0\linux-x64\publish\ *
3939
echo *** Completed Linux X64
4040

41-
echo *** Build application
42-
MSBuild "ConformU.sln" /p:Configuration=Debug /p:Platform="Any CPU" /t:Restore
43-
cd
44-
MSBuild "ConformU.sln" /p:Configuration=Debug /p:Platform="Any CPU" /t:Rebuild
45-
echo *** Completed Build
46-
4741
echo *** Publishing Windows 64bit
4842
dotnet publish ConformU/ConformU.csproj -c Debug /p:Platform="Any CPU" -r win-x64 --framework net7.0-windows --self-contained true /p:PublishTrimmed=false /p:PublishSingleFile=true -o ./publish/ConformU64/
4943
echo *** Completed 64bit publish

0 commit comments

Comments
 (0)