UNPKG

1.05 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * Expose `Option`.
5 */
6
7module.exports = exports = Option;
8
9/**
10 * Initialize a new `Option` instance.
11 *
12 * @param {String} flags
13 * @param {String} description
14 * @return {Option}
15 * @api public
16 */
17
18function Option(flags, description) {
19 this.flags = flags;
20 this.required = ~flags.indexOf("<");
21 this.optional = ~flags.indexOf("[");
22 this.bool = !~flags.indexOf("-no-");
23 flags = flags.split(/[ ,|]+/);
24 if (flags.length > 1 && !/^[[<]/.test(flags[1])) { this.short = flags.shift(); }
25 this.long = flags.shift();
26 this.description = description || "";
27 return this;
28}
29
30/**
31 * Return option name.
32 *
33 * @return {String}
34 * @api private
35 */
36
37Option.prototype.name = function() {
38 return this.long
39 .replace("--", "")
40 .replace("no-", "");
41};
42
43/**
44 * Check if `arg` matches the short or long flag.
45 *
46 * @param {String} arg
47 * @return {Boolean}
48 * @api private
49 */
50
51Option.prototype.is = function(arg) {
52 return arg === this.short || arg === this.long;
53};