Files

134 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
namespace Sarand.Net.Standard.Base.App
{
//=======================================================================
/// <summary>
/// Aids in parsing the command line at application startup.
/// <para>Ref. CCommandLineInfo Class</para>
/// </summary>
public class SacsCommandLineInfo
{
//-------------------------------------------------------------------
// Definitions.
public class Param
{
internal Param (String name, String value)
{
Name = StringUtil.EmptyOnNull(name);
Value = StringUtil.EmptyOnNull(value);
}
public String Name { get; }
public String Value { get; }
}
public enum ShellCommand
{
/// <summary>Indicates that no file name was found on the command line.</summary>
FileNew,
/// <summary>Indicates that a file name was found on the command line and that none of the following flags were found on the command line: /p, /pt, /dde.</summary>
FileOpen,
/// <summary>Indicates that the /p flag was found on the command line.</summary>
FilePrint,
/// <summary>Indicates that the /pt flag was found on the command line.</summary>
FilePrintTo,
/// <summary>Indicates that the /dde flag was found on the command line.</summary>
FileDDE,
/// <summary>Indicates that the /Register or /Regserver flag was found on the command line and the application was asked to register.</summary>
AppRegister,
/// <summary>Indicates that the /Unregister or /Unregserver application was asked to unregister.</summary>
AppUnregister,
/// <summary>Indicates that the application was restarted by the restart manager.</summary>
RestartByRestartManager,
FileNothing = -1
};
//-------------------------------------------------------------------
// Data members.
private readonly List<String> parameters = new List<string>();
//-------------------------------------------------------------------
// Construction.
public SacsCommandLineInfo (string[] args)
{
// Intercept specific arguments.
foreach (String arg in args)
{
if (arg.EqualsCI("/Automation")) RunAutomated = true;
if (arg.EqualsCI("/Embedding")) RunEmbedded = true;
}
// Store raw parameters.
parameters.AddRange(args);
// Extract process definitions.
foreach (String arg in args)
{
if (arg.StartsWith("-") || arg.StartsWith("/"))
{
String processDfnArg = StringUtil.SubstringSafe(arg,1);
if (SacsProcessDefinition.IsValidProcessDefinition(processDfnArg))
{
try
{
processDefinitions.Add(new SacsProcessDefinition(processDfnArg));
}
catch (Exception exc)
{
SaLogger.Warn($"{GetType().Name}.CTR Invalid process definition: {exc.Message}", true);
}
}
}
}
}
//-------------------------------------------------------------------
// Properties.
/// <summary>Indicates that the /Automation flag was found on the command line.</summary>
public bool RunAutomated { get; private set; } = false;
/// <summary>Indicates that the /Embedding flag was found on the command line.</summary>
public bool RunEmbedded { get; private set; } = false;
/// <summary>Indicates that the splash screen should be displayed.</summary>
public bool ShowSplash { get; private set; } = false;
//-------------------------------------------------------------------
// Printing properties.
/// <summary>Indicates the file name to be opened or printed; empty if the shell command is New or DDE.<para>The value of the first non-flag parameter on the command line.</para></summary>
public String FileName { get; private set; } = "";
/// <summary>Indicates the printer name if the shell command is Print To; otherwise empty.<para>The value of the second non-flag parameter on the command line.</para></summary>
public String PrinterName { get; private set; } = "";
/// <summary>Indicates the driver name if the shell command is Print To; otherwise empty.<para>The value of the third non-flag parameter on the command line.</para></summary>
public String DriverName { get; private set; } = "";
/// <summary>Indicates the port name if the shell command is Print To; otherwise empty.<para>The value of the fourth non-flag parameter on the command line.</para></summary>
public String PortName { get; private set; } = "";
//-------------------------------------------------------------------
// Restart properties.
/// <summary>
/// Indicates the unique restart identifier for the restart manager if the restart manager restarted the application.
/// <para>The restart identifier is unique for each instance of the application.</para>
/// <para>If the restart manager exits the application and is configured to restart it, the restart manager executes the application from the command line with the restart identifier as an optional parameter.</para>
/// <para>When the restart manager uses the restart identifier, the application can reopen the previously open documents and recover autosaved files.</para>
/// </summary>
public String RestartIdentifier { get; private set; } = "";
//-------------------------------------------------------------------
// Application parameters.
public int ParamCount { get { return parameters.Count; } }
public List<String> Params { get { return new List<string>(parameters); } }
// Redirected from.
public String RedirectedFrom { get; private set; } = "";
//-------------------------------------------------------------------
// Process definitions.
public readonly List<SacsProcessDefinition> processDefinitions = new List<SacsProcessDefinition>();
public List<SacsProcessDefinition> ProcessDefinitions { get { return new List<SacsProcessDefinition>(processDefinitions); } }
//-------------------------------------------------------------------
}
//=======================================================================
}