/
CommandLineArgument.cs
325 lines (296 loc) · 13.2 KB
/
CommandLineArgument.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#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
{
/// <summary>
/// Primary implementation of the <see cref="ICommandLineArgument">ICommandLineArgument</see> interface.
/// </summary>
[Serializable]
public class CommandLineArgument : Duplicati.Library.Interface.ICommandLineArgument
{
public enum ArgumentType
{
/// <summary>
/// Indicates that the argument is a string type
/// </summary>
String,
/// <summary>
/// Indicates that the argument is an integer type
/// </summary>
Integer,
/// <summary>
/// Indicates that the argument is a boolean type
/// </summary>
Boolean,
/// <summary>
/// Indicates that the argument is a timespan type
/// </summary>
Timespan,
/// <summary>
/// Indicates that the argument is a size type
/// </summary>
Size,
/// <summary>
/// Indicates that the argument is an enumeration value
/// </summary>
Enumeration,
/// <summary>
/// Indicates that the argument is a path to a file or directory
/// </summary>
Path,
/// <summary>
/// Indicates that the argument is a password and should be masked
/// </summary>
Password,
/// <summary>
/// Indicates that the argument is an enumeration value supporting a combination of flags
/// </summary>
Flags,
/// <summary>
/// The argument type is unknown
/// </summary>
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 = "";
/// <summary>
/// The primary name for the argument
/// </summary>
public string Name
{
get { return m_name; }
set { m_name = value; }
}
/// <summary>
/// A list of valid aliases, may be null or an empty array
/// </summary>
public string[] Aliases
{
get { return m_aliases; }
set { m_aliases = value; }
}
/// <summary>
/// The argument type
/// </summary>
public ArgumentType Type
{
get { return m_type; }
set { m_type = value; }
}
/// <summary>
/// A list of valid values, if applicable
/// </summary>
public string[] ValidValues
{
get { return m_validValues; }
set { m_validValues = value; }
}
/// <summary>
/// A short description of the argument
/// </summary>
public string ShortDescription
{
get { return m_shortDescription; }
set { m_shortDescription = value; }
}
/// <summary>
/// A long description of the argument
/// </summary>
public string LongDescription
{
get { return m_longDescription; }
set { m_longDescription = value; }
}
/// <summary>
/// The default value for the parameter
/// </summary>
public string DefaultValue
{
get { return m_defaultValue; }
set { m_defaultValue = value; }
}
/// <summary>
/// A value indicating if the option is deprecated
/// </summary>
public bool Deprecated
{
get { return m_deprecated; }
set { m_deprecated = value; }
}
/// <summary>
/// A message describing the deprecation reason and possible change suggestions
/// </summary>
public string DeprecationMessage
{
get { return m_deprecationMessage; }
set { m_deprecationMessage = value; }
}
/// <summary>
/// Creates a new CommandLineArgument instance
/// </summary>
/// <param name="name">The name of the argument</param>
public CommandLineArgument(string name)
{
m_name = name;
}
/// <summary>
/// Creates a new CommandLineArgument instance
/// </summary>
/// <param name="name">The name of the argument</param>
/// <param name="type">The argument type</param>
public CommandLineArgument(string name, ArgumentType type)
: this(name)
{
m_type = type;
}
/// <summary>
/// Creates a new CommandLineArgument instance
/// </summary>
/// <param name="name">The name of the argument</param>
/// <param name="type">The argument type</param>
/// <param name="shortDescription">The arguments short description</param>
/// <param name="longDescription">The arguments long description</param>
public CommandLineArgument(string name, ArgumentType type, string shortDescription, string longDescription)
: this(name, type)
{
m_shortDescription = shortDescription;
m_longDescription = longDescription;
}
/// <summary>
/// Creates a new CommandLineArgument instance
/// </summary>
/// <param name="name">The name of the argument</param>
/// <param name="type">The argument type</param>
/// <param name="shortDescription">The arguments short description</param>
/// <param name="longDescription">The arguments long description</param>
/// <param name="defaultValue">The default value of the argument</param>
public CommandLineArgument(string name, ArgumentType type, string shortDescription, string longDescription, string defaultValue)
: this(name, type, shortDescription, longDescription)
{
m_defaultValue = defaultValue;
}
/// <summary>
/// Creates a new CommandLineArgument instance
/// </summary>
/// <param name="name">The name of the argument</param>
/// <param name="type">The argument type</param>
/// <param name="shortDescription">The arguments short description</param>
/// <param name="longDescription">The arguments long description</param>
/// <param name="defaultValue">The default value of the argument</param>
/// <param name="aliases">A list of aliases for the command</param>
public CommandLineArgument(string name, ArgumentType type, string shortDescription, string longDescription, string defaultValue, string[] aliases)
: this(name, type, shortDescription, longDescription, defaultValue)
{
m_aliases = aliases;
}
/// <summary>
/// Creates a new CommandLineArgument instance
/// </summary>
/// <param name="name">The name of the argument</param>
/// <param name="type">The argument type</param>
/// <param name="shortDescription">The arguments short description</param>
/// <param name="longDescription">The arguments long description</param>
/// <param name="defaultValue">The default value of the argument</param>
/// <param name="aliases">A list of aliases for the command</param>
/// <param name="values">A list of valid values for the command</param>
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;
}
/// <summary>
/// Creates a new CommandLineArgument instance
/// </summary>
/// <param name="name">The name of the argument</param>
/// <param name="type">The argument type</param>
/// <param name="shortDescription">The arguments short description</param>
/// <param name="longDescription">The arguments long description</param>
/// <param name="defaultValue">The default value of the argument</param>
/// <param name="aliases">A list of aliases for the command</param>
/// <param name="values">A list of valid values for the command</param>
/// <param name="deprecationMessage">A message indicating the reason for deprecation and any change suggestions</param>
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;
}
/// <summary>
/// Returns a localized string indicating the argument type
/// </summary>
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<string> lines, ICommandLineArgument arg)
{
PrintArgument(lines, arg, " ");
}
public static void PrintArgument(List<string> 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);
}
}
}