1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const help_1 = require("./help");
|
4 | class ArgsParser {
|
5 | constructor() {
|
6 | this.nameDefinition = { name: 'name', alias: 'n', defaultOption: true };
|
7 | this.jsonDefinition = { name: 'json', type: Boolean };
|
8 | this.helpDefinition = { name: 'help', alias: 'h', type: Boolean };
|
9 | }
|
10 | parse() {
|
11 | const commandLineArgs = require('command-line-args');
|
12 | let optionDefs = [];
|
13 | const mainDefinitions = [
|
14 | { name: 'command', defaultOption: true },
|
15 | { name: 'help', alias: 'h', type: Boolean }
|
16 | ];
|
17 | const mainOptions = commandLineArgs(mainDefinitions, { stopAtFirstUnknown: true });
|
18 | const argv = mainOptions._unknown || [];
|
19 | if (mainOptions['command'] === undefined) {
|
20 | this.help();
|
21 | process.exit(0);
|
22 | }
|
23 | let displayHelp = false;
|
24 | if (mainOptions['help'] === true) {
|
25 | displayHelp = true;
|
26 | }
|
27 | let options = {};
|
28 | if (displayHelp) {
|
29 | options = { help: true };
|
30 | }
|
31 | switch (mainOptions.command) {
|
32 | case 'export':
|
33 | optionDefs = this.exportOptions(optionDefs);
|
34 | break;
|
35 | case 'init':
|
36 | optionDefs = this.initOptions(optionDefs);
|
37 | break;
|
38 | case 'list':
|
39 | optionDefs = this.listOptions(optionDefs);
|
40 | break;
|
41 | case 'parse':
|
42 | optionDefs = this.parseOptions(optionDefs);
|
43 | break;
|
44 | case 'remove':
|
45 | optionDefs = this.removeOptions(optionDefs);
|
46 | break;
|
47 | case 'repos':
|
48 | optionDefs = this.reposOptions(optionDefs);
|
49 | break;
|
50 | case 'search':
|
51 | optionDefs = this.searchOptions(optionDefs);
|
52 | break;
|
53 | case 'show':
|
54 | optionDefs = this.showOptions(optionDefs);
|
55 | break;
|
56 | case 'switch':
|
57 | optionDefs = this.switchOptions(optionDefs);
|
58 | break;
|
59 | }
|
60 | options = Object.assign(options, commandLineArgs(optionDefs, { argv }));
|
61 | return {
|
62 | command: mainOptions.command,
|
63 | options: options
|
64 | };
|
65 | }
|
66 | exportOptions(optionDefs) {
|
67 | optionDefs.push(this.nameDefinition);
|
68 | optionDefs.push({ name: 'outfile', alias: 'o' });
|
69 | optionDefs.push({ name: 'includeempty', alias: 'i', type: Boolean });
|
70 | return optionDefs;
|
71 | }
|
72 | initOptions(optionDefs) {
|
73 | optionDefs.push(this.nameDefinition);
|
74 | optionDefs.push({ name: 'dir', alias: 'd' });
|
75 | optionDefs.push({ name: 'srcdir', alias: 's' });
|
76 | return optionDefs;
|
77 | }
|
78 | listOptions(optionDefs) {
|
79 | optionDefs.push({ name: 'tables', alias: 't', type: Boolean });
|
80 | optionDefs.push({ name: 'dbprefix', alias: 'p', type: Boolean });
|
81 | optionDefs.push({ name: 'dbs', alias: 'd', type: Boolean });
|
82 | optionDefs.push(this.jsonDefinition);
|
83 | return optionDefs;
|
84 | }
|
85 | parseOptions(optionDefs) {
|
86 | optionDefs.push(this.nameDefinition);
|
87 | return optionDefs;
|
88 | }
|
89 | removeOptions(optionDefs) {
|
90 | optionDefs.push(this.nameDefinition);
|
91 | return optionDefs;
|
92 | }
|
93 | reposOptions(optionDefs) {
|
94 | optionDefs.push(this.helpDefinition);
|
95 | optionDefs.push(this.jsonDefinition);
|
96 | optionDefs.push({ name: 'verbose', alias: 'v', type: Boolean });
|
97 | return optionDefs;
|
98 | }
|
99 | searchOptions(optionDefs) {
|
100 | optionDefs.push(this.helpDefinition);
|
101 | optionDefs.push(this.nameDefinition);
|
102 | optionDefs.push({ name: 'field', alias: 'f' });
|
103 | optionDefs.push({ name: 'table', alias: 't' });
|
104 | optionDefs.push({ name: 'db' });
|
105 | optionDefs.push({ name: 'create', alias: 'c' });
|
106 | optionDefs.push({ name: 'update', alias: 'u' });
|
107 | optionDefs.push({ name: 'delete', alias: 'd' });
|
108 | optionDefs.push(this.jsonDefinition);
|
109 | optionDefs.push({ name: 'batch', alias: 'b', type: Boolean });
|
110 | return optionDefs;
|
111 | }
|
112 | showOptions(optionDefs) {
|
113 | optionDefs.push({ name: 'source', alias: 's', defaultOption: true });
|
114 | optionDefs.push({ name: 'tables', alias: 't', type: Boolean });
|
115 | optionDefs.push(this.jsonDefinition);
|
116 | optionDefs.push({ name: 'xref', alias: 'x', type: Boolean });
|
117 | return optionDefs;
|
118 | }
|
119 | switchOptions(optionDefs) {
|
120 | optionDefs.push(this.nameDefinition);
|
121 | return optionDefs;
|
122 | }
|
123 | help() {
|
124 | const help = new help_1.Help();
|
125 | help.main();
|
126 | process.exit(0);
|
127 | }
|
128 | }
|
129 | exports.ArgsParser = ArgsParser;
|
130 |
|
\ | No newline at end of file |