#region Disclaimer / License // Copyright (C) 2015, The Duplicati Team // http://www.duplicati.com, info@duplicati.com // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library 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 // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion using System; using System.Collections.Generic; using System.Text; namespace Duplicati.Library.Interface { /// /// Primary implementation of the ICommandLineArgument interface. /// [Serializable] public class CommandLineArgument : Duplicati.Library.Interface.ICommandLineArgument { public enum ArgumentType { /// /// Indicates that the argument is a string type /// String, /// /// Indicates that the argument is an integer type /// Integer, /// /// Indicates that the argument is a boolean type /// Boolean, /// /// Indicates that the argument is a timespan type /// Timespan, /// /// Indicates that the argument is a size type /// Size, /// /// Indicates that the argument is an enumeration value /// Enumeration, /// /// Indicates that the argument is a path to a file or directory /// Path, /// /// Indicates that the argument is a password and should be masked /// Password, /// /// Indicates that the argument is an enumeration value supporting a combination of flags /// Flags, /// /// The argument type is unknown /// Unknown } private string m_name; private string[] m_aliases = null; private ArgumentType m_type = ArgumentType.Unknown; private string[] m_validValues = null; private string m_shortDescription = ""; private string m_longDescription = ""; private string m_defaultValue = ""; private bool m_deprecated = false; private string m_deprecationMessage = ""; /// /// The primary name for the argument /// public string Name { get { return m_name; } set { m_name = value; } } /// /// A list of valid aliases, may be null or an empty array /// public string[] Aliases { get { return m_aliases; } set { m_aliases = value; } } /// /// The argument type /// public ArgumentType Type { get { return m_type; } set { m_type = value; } } /// /// A list of valid values, if applicable /// public string[] ValidValues { get { return m_validValues; } set { m_validValues = value; } } /// /// A short description of the argument /// public string ShortDescription { get { return m_shortDescription; } set { m_shortDescription = value; } } /// /// A long description of the argument /// public string LongDescription { get { return m_longDescription; } set { m_longDescription = value; } } /// /// The default value for the parameter /// public string DefaultValue { get { return m_defaultValue; } set { m_defaultValue = value; } } /// /// A value indicating if the option is deprecated /// public bool Deprecated { get { return m_deprecated; } set { m_deprecated = value; } } /// /// A message describing the deprecation reason and possible change suggestions /// public string DeprecationMessage { get { return m_deprecationMessage; } set { m_deprecationMessage = value; } } /// /// Creates a new CommandLineArgument instance /// /// The name of the argument public CommandLineArgument(string name) { m_name = name; } /// /// Creates a new CommandLineArgument instance /// /// The name of the argument /// The argument type public CommandLineArgument(string name, ArgumentType type) : this(name) { m_type = type; } /// /// Creates a new CommandLineArgument instance /// /// The name of the argument /// The argument type /// The arguments short description /// The arguments long description public CommandLineArgument(string name, ArgumentType type, string shortDescription, string longDescription) : this(name, type) { m_shortDescription = shortDescription; m_longDescription = longDescription; } /// /// Creates a new CommandLineArgument instance /// /// The name of the argument /// The argument type /// The arguments short description /// The arguments long description /// The default value of the argument public CommandLineArgument(string name, ArgumentType type, string shortDescription, string longDescription, string defaultValue) : this(name, type, shortDescription, longDescription) { m_defaultValue = defaultValue; } /// /// Creates a new CommandLineArgument instance /// /// The name of the argument /// The argument type /// The arguments short description /// The arguments long description /// The default value of the argument /// A list of aliases for the command public CommandLineArgument(string name, ArgumentType type, string shortDescription, string longDescription, string defaultValue, string[] aliases) : this(name, type, shortDescription, longDescription, defaultValue) { m_aliases = aliases; } /// /// Creates a new CommandLineArgument instance /// /// The name of the argument /// The argument type /// The arguments short description /// The arguments long description /// The default value of the argument /// A list of aliases for the command /// A list of valid values for the command public CommandLineArgument(string name, ArgumentType type, string shortDescription, string longDescription, string defaultValue, string[] aliases, string[] values) : this(name, type, shortDescription, longDescription, defaultValue) { m_aliases = aliases; m_validValues = values; } /// /// Creates a new CommandLineArgument instance /// /// The name of the argument /// The argument type /// The arguments short description /// The arguments long description /// The default value of the argument /// A list of aliases for the command /// A list of valid values for the command /// A message indicating the reason for deprecation and any change suggestions public CommandLineArgument(string name, ArgumentType type, string shortDescription, string longDescription, string defaultValue, string[] aliases, string[] values, string deprecationMessage) : this(name, type, shortDescription, longDescription, defaultValue, aliases, values) { m_deprecated = true; m_deprecationMessage = deprecationMessage; } /// /// Returns a localized string indicating the argument type /// public string Typename { get { switch (this.Type) { case Duplicati.Library.Interface.CommandLineArgument.ArgumentType.Boolean: return Strings.DataTypes.Boolean; case Duplicati.Library.Interface.CommandLineArgument.ArgumentType.Enumeration: return Strings.DataTypes.Enumeration; case Duplicati.Library.Interface.CommandLineArgument.ArgumentType.Flags: return Strings.DataTypes.Flags; case Duplicati.Library.Interface.CommandLineArgument.ArgumentType.Integer: return Strings.DataTypes.Integer; case Duplicati.Library.Interface.CommandLineArgument.ArgumentType.Path: return Strings.DataTypes.Path; case Duplicati.Library.Interface.CommandLineArgument.ArgumentType.Size: return Strings.DataTypes.Size; case Duplicati.Library.Interface.CommandLineArgument.ArgumentType.String: return Strings.DataTypes.String; case Duplicati.Library.Interface.CommandLineArgument.ArgumentType.Timespan: return Strings.DataTypes.Timespan; case Duplicati.Library.Interface.CommandLineArgument.ArgumentType.Unknown: return Strings.DataTypes.Unknown; default: return this.Type.ToString(); } } } public static void PrintArgument(List lines, ICommandLineArgument arg) { PrintArgument(lines, arg, " "); } public static void PrintArgument(List lines, ICommandLineArgument arg, string indent) { lines.Add(indent + "--" + arg.Name + " (" + arg.Typename + "): " + arg.ShortDescription); if (arg.Deprecated) lines.Add(indent + " " + Strings.CommandLineArgument.DeprecationMarker + ": " + arg.DeprecationMessage); lines.Add(indent + " " + arg.LongDescription); if (arg.Aliases != null && arg.Aliases.Length > 0) lines.Add(indent + " * " + Strings.CommandLineArgument.AliasesHeader + ": --" + string.Join(", --", arg.Aliases)); if (arg.ValidValues != null && arg.ValidValues.Length > 0) lines.Add(indent + " * " + Strings.CommandLineArgument.ValuesHeader + ": " + string.Join(", ", arg.ValidValues)); if (!string.IsNullOrEmpty(arg.DefaultValue)) lines.Add(indent + " * " + Strings.CommandLineArgument.DefaultValueHeader + ": " + arg.DefaultValue); } } }