-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathComLineProcessor.cs
More file actions
563 lines (482 loc) · 18.4 KB
/
ComLineProcessor.cs
File metadata and controls
563 lines (482 loc) · 18.4 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//---------------------------------------------------------------------
// <copyright file="ComLineProcesser.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.OpenApi;
namespace OoasUtil
{
/// <summary>
/// Command line arguments processer.
/// </summary>
internal class ComLineProcessor
{
private IList<string> _args;
private bool _continue;
public static Version version = new Version(1, 0, 0, 0);
/// <summary>
/// Initializes a new instance of the <see cref="ComLineProcessor"/> class.
/// </summary>
/// <param name="args">The command line arguments.</param>
public ComLineProcessor(string[] args)
{
_args = args;
}
/// <summary>
/// Can continue with the arguments
/// </summary>
public bool CanContinue { get { return _continue; } }
/// <summary>
/// Input FilePath or Url with the CSDL.
/// </summary>
public string Input { get; private set; }
/// <summary>
/// Output format.
/// </summary>
#nullable enable
public string? Format { get; private set; }
#nullable restore
/// <summary>
/// Whether KeyAsSegment is used.
/// </summary>
public bool? KeyAsSegment { get; private set; }
/// <summary>
/// Output OpenApi Specification Version.
/// </summary>
public OpenApiSpecVersion? Version { get; private set; }
/// <summary>
/// Output file.
/// </summary>
public string Output { get; private set; }
/// <summary>
/// Is input Url
/// </summary>
public bool IsLocalFile { get; private set; }
/// <summary>
/// Set the output to produce all derived types in responses.
/// </summary>
public bool? DerivedTypesReferencesForResponses { get; private set; }
/// <summary>
/// Set the output to expect all derived types in request bodies.
/// </summary>
public bool? DerivedTypesReferencesForRequestBody { get; private set; }
/// <summary>
/// Set the output to expose pagination for collections.
/// </summary>
public bool? EnablePagination { get; private set; }
/// <summary>
/// Set the output to use unqualified calls for bound operations.
/// </summary>
public bool? EnableUnqualifiedCall { get; private set; }
/// <summary>
/// Disable examples in the schema.
/// </summary>
public bool? DisableSchemaExamples { get; private set; }
/// <summary>
/// Gets/Sets a value indicating whether or not to require the
/// Validation.DerivedTypeConstraint to be applied to NavigationSources
/// to bind operations of derived types to them.
/// </summary>
public bool? RequireDerivedTypesConstraint { get; private set; }
/// <summary>
/// Use HTTP PUT method for update operations by default instead of PATCH.
/// </summary>
public bool? UseHttpPutForUpdate { get; private set; }
/// <summary>
/// Process the arguments.
/// </summary>
public bool Process()
{
_continue = false;
if (_args.Count == 0)
{
PrintUsage();
return true;
}
try
{
for (int i = 0; i < _args.Count; i++)
{
string arg = _args[i];
switch (arg)
{
case "--version":
case "-v":
PrintVersion();
return true;
case "--help":
case "-h":
PrintUsage();
return true;
case "--input":
case "-i":
if (!ProcessInput(_args[i + 1]))
{
return false;
}
i++;
break;
case "--keyassegment":
case "-k":
if (!ProcessKeyAsSegment(true))
{
return false;
}
break;
case "--yaml":
case "-y":
if (!ProcessTarget("yaml"))
{
return false;
}
break;
case "--json":
case "-j":
if (!ProcessTarget("json"))
{
return false;
}
break;
case "--specversion":
case "-s":
if (!int.TryParse(_args[i + 1], out int version) || !ProcessTarget(version))
{
return false;
}
++i;
break;
case "--output":
case "-o":
if (!ProcessOutput(_args[i + 1]))
{
return false;
}
i++;
break;
case "--derivedtypesreferencesforresponses":
case "-drs":
if (!ProcessDerivedTypesReferencesForResponses(true))
{
return false;
}
break;
case "--derivedtypesreferencesforrequestbody":
case "-drq":
if (!ProcessDerivedTypesReferencesForRequestBody(true))
{
return false;
}
break;
case "--requireDerivedTypesConstraint":
case "-rdt":
if (!ProcessRequireDerivedTypesConstraint(true))
{
return false;
}
break;
case "--enablepagination":
case "-p":
if (!ProcessEnablePagination(true))
{
return false;
}
break;
case "--enableunqualifiedcall":
case "-u":
if (!ProcessEnableUnqualifiedCall(true))
{
return false;
}
break;
case "--disableschemaexamples":
case "-x":
if (!ProcessDisableSchemaExamples(true))
{
return false;
}
break;
case "--useputforupdate":
case "-put":
if (!ProcessUseHttpPutForUpdate(true))
{
return false;
}
break;
default:
PrintUsage();
return false;
}
}
}
catch
{
PrintUsage();
return false;
}
// by default.
if (Format == null)
{
Format = "json";
}
if (Version == null)
{
Version = OpenApiSpecVersion.OpenApi3_1;
}
if (KeyAsSegment == null)
{
KeyAsSegment = false;
}
if (DerivedTypesReferencesForResponses == null)
{
DerivedTypesReferencesForResponses = false;
}
if (DerivedTypesReferencesForRequestBody == null)
{
DerivedTypesReferencesForRequestBody = false;
}
if (RequireDerivedTypesConstraint == null)
{
RequireDerivedTypesConstraint = false;
}
if (EnablePagination == null)
{
EnablePagination = false;
}
if (EnableUnqualifiedCall == null)
{
EnableUnqualifiedCall = false;
}
if (DisableSchemaExamples == null)
{
DisableSchemaExamples = false;
}
if (UseHttpPutForUpdate == null)
{
UseHttpPutForUpdate = false;
}
_continue = ValidateArguments();
return _continue;
}
private bool ProcessInput(string input)
{
if (Input != null)
{
Console.WriteLine("[Error:] Multiple [--input|-i] are not allowed.\n");
PrintUsage();
return false;
}
Input = input;
return true;
}
private bool ProcessOutput(string file)
{
if (Output != null)
{
Console.WriteLine("[Error:] Multiple [--output|-o] are not allowed.\n");
PrintUsage();
return false;
}
Output = file;
return true;
}
private bool ProcessTarget(string format)
{
if (Format != null)
{
Console.WriteLine("[Error:] Multiple [--json|-j|--yaml|-y] are not allowed.\n");
PrintUsage();
return false;
}
Format = format;
return true;
}
private bool ProcessKeyAsSegment(bool keyAsSegment)
{
if (KeyAsSegment != null)
{
Console.WriteLine("[Error:] Multiple [--keyassegment|-k] are not allowed.\n");
PrintUsage();
return false;
}
KeyAsSegment = keyAsSegment;
return true;
}
private bool ProcessDerivedTypesReferencesForResponses(bool derivedTypesReferencesForResponses)
{
if (DerivedTypesReferencesForResponses != null)
{
Console.WriteLine("[Error:] Multiple [--derivedtypesreferencesforresponses|-drs] are not allowed.\n");
PrintUsage();
return false;
}
DerivedTypesReferencesForResponses = derivedTypesReferencesForResponses;
return true;
}
private bool ProcessDerivedTypesReferencesForRequestBody(bool derivedTypesReferencesForRequestBody)
{
if (DerivedTypesReferencesForRequestBody != null)
{
Console.WriteLine("[Error:] Multiple [--derivedtypesreferencesforrequestbody|-drq] are not allowed.\n");
PrintUsage();
return false;
}
DerivedTypesReferencesForRequestBody = derivedTypesReferencesForRequestBody;
return true;
}
private bool ProcessRequireDerivedTypesConstraint(bool requireDerivedTypesConstraint)
{
if (RequireDerivedTypesConstraint != null)
{
Console.WriteLine("[Error:] Multiple [--requireDerivedTypesConstraint|-rdt] are not allowed.\n");
PrintUsage();
return false;
}
RequireDerivedTypesConstraint = requireDerivedTypesConstraint;
return true;
}
private bool ProcessEnablePagination(bool enablePagination)
{
if (EnablePagination != null)
{
Console.WriteLine("[Error:] Multiple [--enablepagination|-p] are not allowed.\n");
PrintUsage();
return false;
}
EnablePagination = enablePagination;
return true;
}
private bool ProcessEnableUnqualifiedCall(bool enableUnqualifiedCall)
{
if (EnableUnqualifiedCall != null)
{
Console.WriteLine("[Error:] Multiple [--enableunqualifiedcall|-u] are not allowed.\n");
PrintUsage();
return false;
}
EnableUnqualifiedCall = enableUnqualifiedCall;
return true;
}
private bool ProcessDisableSchemaExamples(bool disableSchemaExamples)
{
if (DisableSchemaExamples != null)
{
Console.WriteLine("[Error:] Multiple [--disableschemaexamples|-x] are not allowed.\n");
PrintUsage();
return false;
}
DisableSchemaExamples = disableSchemaExamples;
return true;
}
private bool ProcessUseHttpPutForUpdate(bool useHttpPutForUpdate)
{
if (UseHttpPutForUpdate != null)
{
Console.WriteLine("[Error:] Multiple [--useputforupdate|-put] are not allowed.\n");
PrintUsage();
return false;
}
UseHttpPutForUpdate = useHttpPutForUpdate;
return true;
}
private bool ProcessTarget(int version)
{
if (Version != null)
{
Console.WriteLine("[Error:] Multiple [--specversion|-s] are not allowed.\n");
PrintUsage();
return false;
}
else if (version > 3 || version < 2)
{
Console.WriteLine("[Error:] Only OpenApi specification version 2 or 3 are supported.\n");
PrintUsage();
return false;
}
Version = version == 2 ? OpenApiSpecVersion.OpenApi2_0 : OpenApiSpecVersion.OpenApi3_1;
return true;
}
private bool ValidateArguments()
{
if (String.IsNullOrEmpty(Input))
{
Console.WriteLine("[Error:] At least one of [--input|-i] is required.\n");
PrintUsage();
return false;
}
IsLocalFile = IsLocalPath(Input);
if (IsLocalFile)
{
if (!File.Exists(Input))
{
Console.WriteLine("[Error]: File (" + Input + ") is not existed.\n");
return false;
}
}
if (String.IsNullOrEmpty(Output))
{
Console.WriteLine("[Error:] [--output|-o] is required.\n");
PrintUsage();
return false;
}
return true;
}
public static void PrintUsage()
{
StringBuilder sb = new StringBuilder();
sb.Append("Usage: OoasUtil.exe [options]\n");
sb.Append("\nOptions:\n");
sb.Append(" --help|-h\t\t\tDisplay help.\n");
sb.Append(" --version|-v\t\t\tDisplay version.\n");
sb.Append(" --input|-i CsdlFileOrUrl\tSet the CSDL file name or the OData Service Url.\n");
sb.Append(" --output|-o OutputFile\tSet the output file name.\n");
sb.Append(" --keyassegment|-k\t\t\tSet the output to use key-as-segment style URLs.\n");
sb.Append(" --derivedtypesreferencesforresponses|-drs\t\t\tSet the output to produce all derived types in responses.\n");
sb.Append(" --derivedtypesreferencesforrequestbody|-drq\t\t\tSet the output to expect all derived types in request bodies.\n");
sb.Append(" --requireDerivedTypesConstraint|-rdt\t\t\tSet the output to require derived type constraint to bind Operations.\n");
sb.Append(" --enablepagination|-p\t\t\tSet the output to expose pagination for collections.\n");
sb.Append(" --enableunqualifiedcall|-u\t\t\tSet the output to use unqualified calls for bound operations.\n");
sb.Append(" --disableschemaexamples|-x\t\t\tDisable examples in the schema.\n");
sb.Append(" --useputforupdate|-put\t\t\tUse HTTP PUT method for update operations instead of PATCH by default.\n");
sb.Append(" --json|-j\t\t\tSet the output format as JSON.\n");
sb.Append(" --yaml|-y\t\t\tSet the output format as YAML.\n");
sb.Append(" --specversion|-s IntVersion\tSet the OpenApi Specification version of the output. Only 2 or 3 are supported.\n");
sb.Append("\nExamples:\n");
sb.Append(" OoasUtil.exe -y -i http://services.odata.org/TrippinRESTierService -o trip.yaml\n");
sb.Append(" OoasUtil.exe -j -s 2 -i c:\\csdl.xml -o trip.json\n");
Console.WriteLine(sb.ToString());
}
public static string Copyright()
{
return
"Microsoft (R) OData To Open API Utilities, Version " + version.ToString() + " \n" +
"Copyright(C) Microsoft Corporation. All rights reserved.\n\n";
}
public static void PrintVersion()
{
Console.WriteLine(version.ToString());
}
private static bool IsLocalPath(string path)
{
bool ret = true;
try
{
ret = new Uri(path).IsFile;
}
catch
{
if (path.StartsWith("http://") ||
path.StartsWith(@"http:\\") ||
path.StartsWith("https://") ||
path.StartsWith(@"https:\\"))
{
return false;
}
}
return ret;
}
}
}