1 | "use strict";
|
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 | exports.parse = function ( commandLineArgs, def ) {
|
47 | const options = {};
|
48 |
|
49 | commandLineArgs.shift();
|
50 | commandLineArgs.shift();
|
51 |
|
52 | let currentCommand = null;
|
53 |
|
54 | while ( commandLineArgs.length > 0 ) {
|
55 | const arg = commandLineArgs.shift();
|
56 | if ( isOption( arg ) ) {
|
57 |
|
58 |
|
59 | const opt = arg.substr( 1 );
|
60 | checkIfCommandHasThisOption( currentCommand, opt, def );
|
61 | options[ currentCommand ][ opt ] = parseOptionArguments( commandLineArgs, currentCommand, opt, def );
|
62 | } else {
|
63 |
|
64 | const cmd = arg;
|
65 | checkIfCommandExists( cmd, def );
|
66 | options[ cmd ] = {};
|
67 | currentCommand = cmd;
|
68 | }
|
69 | }
|
70 |
|
71 | applyDefaultValuesForOptionsArguments( options, def );
|
72 | return options;
|
73 | };
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | exports.usage = function ( def ) {
|
80 | let output = "Accepted arguments:\n";
|
81 | const
|
82 | commandNames = Object.keys( def ),
|
83 | commandMaxLength = commandNames.reduce( ( acc, val ) => Math.max( acc, val.length ), 0 );
|
84 |
|
85 | commandNames.forEach( function ( commandName ) {
|
86 | output += ` ${commandName.yellow.bold}:`;
|
87 | output += spc( 1 + commandMaxLength - commandName.length );
|
88 | const cmd = def[ commandName ];
|
89 | if ( typeof cmd.desc === 'string' ) output += cmd.desc;
|
90 | output += "\n";
|
91 | if ( !cmd.opts ) return;
|
92 | const optionNames = Object.keys( cmd.opts );
|
93 | optionNames.forEach( function ( optName ) {
|
94 | output += ` -${optName.yellow}:`.yellow;
|
95 | output += spc( 1 + commandMaxLength - commandName.length );
|
96 | const opt = cmd.opts[ optName ];
|
97 | if ( typeof opt.desc === 'string' ) output += opt.desc;
|
98 | output += "\n";
|
99 | } );
|
100 |
|
101 | } );
|
102 |
|
103 | return `${output}\n`;
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function spc( nbSpaces ) {
|
111 | let
|
112 | output = "",
|
113 | counter = typeof nbSpaces !== "number" ? nbSpaces : 0;
|
114 | while ( counter-- > 0 ) output += " ";
|
115 | return output;
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function applyDefaultValuesForOptionsArguments( options, def ) {
|
131 | Object.keys( options ).forEach( function ( cmdName ) {
|
132 | const
|
133 | cmdOpts = options[ cmdName ],
|
134 | defOpts = def[ cmdName ].opts;
|
135 | if ( defOpts ) {
|
136 | Object.keys( defOpts ).forEach( function ( defOptName ) {
|
137 | if ( cmdOpts[ defOptName ] ) return;
|
138 | const defOptValue = defOpts[ defOptName ];
|
139 | if ( countArgs( defOptValue ) > 0 ) {
|
140 | cmdOpts[ defOptName ] = defOptValue.args;
|
141 | }
|
142 | } );
|
143 | }
|
144 | } );
|
145 | }
|
146 |
|
147 |
|
148 | function isOption( name ) {
|
149 | return name.charAt( 0 ) === '-';
|
150 | }
|
151 |
|
152 |
|
153 | function checkIfCommandExists( cmd, def ) {
|
154 | if ( !def[ cmd ] )
|
155 | throw `Unknown command '${cmd}'!`;
|
156 | }
|
157 |
|
158 |
|
159 | function checkIfCommandHasThisOption( cmd, opt, def ) {
|
160 | if ( !cmd )
|
161 | throw "Expected a command, but found '" + opt + "'!";
|
162 | var availableOptions = def[ cmd ].opts;
|
163 | if ( !availableOptions )
|
164 | throw "Command '" + cmd + "' does not accept any option, but you provided '" + opt + "'!";
|
165 | if ( !availableOptions[ opt ] )
|
166 | throw "Command '" + cmd + "' does not accept the option you provided: '" + opt + "'!";
|
167 | }
|
168 |
|
169 |
|
170 | function parseOptionArguments( commandLineArgs, currentCommand, opt, def ) {
|
171 | var args = [];
|
172 | var count = countArgs( def[ currentCommand ].opts[ opt ] );
|
173 | while ( count-- > 0 ) {
|
174 | if ( commandLineArgs.length === 0 )
|
175 | throw `Missing mandatory argument for option '${opt}' of command '${currentCommand}'!`;
|
176 | args.push( commandLineArgs.shift() );
|
177 | }
|
178 | return args;
|
179 | }
|
180 |
|
181 |
|
182 | function countArgs( opt ) {
|
183 | if ( !opt.args ) return 0;
|
184 | if ( !Array.isArray( opt.args ) ) opt.args = [ opt.args ];
|
185 | return opt.args.length;
|
186 | } |
\ | No newline at end of file |