-
Notifications
You must be signed in to change notification settings - Fork 7
/
options.js
111 lines (87 loc) · 3.3 KB
/
options.js
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
"use strict";
var argv = require ("../lib").createParser ({ once: true })
.body ()
//Positive flag because it doesn't define the "argument" property
.option ({ short: "a", long: "aaaa" })
//Negative flag because it doesn't define the "argument" property
.option ({ long: "b", negate: true })
//Option with a mandatory value and null as default value
.option ({ short: "c", metavar: "C" })
//Option with a Number type and 0 as default value
//By default the options are strings
.option ({ short: "d", metavar: "D", type: Number })
//Option with an Array type and [] as default value
//Array-type values must be comma-separated values, eg: --a 1,a,true
//Each element is converted automatically to the type it represents,
//the previous example is converted to { a: [1, "a", true] }
.option ({ short: "e", metavar: "E", type: Array })
//Option with a Boolean type and false as default value
//This type is not very useful because you can just use a flag, eg:
//--a true is converted to { a: true }
.option ({ short: "f", metavar: "F", type: Boolean })
//Option with a mandatory value and "gggg" as default value
.option ({ long: "g", metavar: "G", default: "gggg" })
//Option with an optional value and null as default value
.option ({ short: "i", metavar: "I", optional: true })
//Option with an optional value and "jjjj" as default value
.option ({ long: "j", metavar: "J", default: "jjjj",
optional: true })
//Option with a description and reviver
//The reviver receives the string value and returns the new value,
//it's like the json reviver function
.option ({ short: "k", long: "kkkk", metavar: "K", description: "kkkk",
reviver: function (value){
return value + "foo";
}})
//Hidden option, it isn't printed in the --help and --usage messages
.option ({ short: "l", hidden: true })
//Option with an alias
.option ({ short: "m", long: "mm", aliases: ["x"] })
//Option with choices
.option ({ short: "n", metavar: "NUM", type: Number,
choices: [1, 10, 100] })
.help ()
.usage ()
.argv ();
console.log (argv);
/*
$ node options.js
{
aaaa: false,
b: true,
c: null,
d: 0,
e: [],
f: false,
g: "gggg",
i: null,
j: "jjjj",
kkkk: null,
l: false,
mm: false,
n: 0,
help: false,
usage: false
}
--------------------------------------------------------------------------------
$ node options.js --help
Usage: options [options]
-a, --aaaa
--no-b
-c C
-d D
-e E
-f F
--g=G
-i[I]
--j[=J]
-k, --kkkk=K kkkk
-m, --mm, --x
-n NUM
-h, --help Display this help message and exit
--usage Display a short usage message and exit
--------------------------------------------------------------------------------
$ node options.js --usage
Usage: options [-c C] [-d D] [-e E] [-f F] [-i[I]] [-n NUM] [-a|--aaaa] [--b]
[--g=G] [--j[=J]] [-k|--kkkk=K] [-m|--mm|--x] [-h|--help] [--usage]
*/