You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api.md
+32-1Lines changed: 32 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -945,7 +945,7 @@ To submit a new translation for yargs:
945
945
946
946
*The [Microsoft Terminology Search](http://www.microsoft.com/Language/en-US/Search.aspx) can be useful for finding the correct terminology in your locale.*
Define global middleware functions to be called first, in list order, for all cli command.
@@ -969,6 +969,37 @@ I'm another middleware function
969
969
Running myCommand!
970
970
```
971
971
972
+
Middleware can be applied before validation by setting the second parameter to `true`. This will execute the middleware prior to validation checks, but after parsing.
973
+
974
+
Each callback is passed a reference to argv. The argv can be modified to affect the behavior of the validation and command execution.
975
+
976
+
For example, an environment variable could potentially populate a required option:
977
+
978
+
```js
979
+
var argv =require('yargs')
980
+
.middleware(function (argv) {
981
+
argv.username=process.env.USERNAME
982
+
argv.password=process.env.PASSWORD
983
+
}, true)
984
+
.command('do-something-logged-in', 'You must be logged in to perform this task'
985
+
{
986
+
'username': {
987
+
'demand':true,
988
+
'string':true
989
+
},
990
+
'password': {
991
+
'demand':true,
992
+
'string':true
993
+
}
994
+
},
995
+
function(argv) {
996
+
console.log('do something with the user login and password', argv.username, argv.password)
it('runs the before-validation middlware before reaching the handler',function(done){
53
+
yargs(['mw'])
54
+
.middleware(function(argv){
55
+
argv.mw='mw'
56
+
},true)
57
+
.command(
58
+
'mw',
59
+
'adds func to middleware',
60
+
{
61
+
'mw': {
62
+
'demand': true,
63
+
'string': true
64
+
}
65
+
},
66
+
function(argv){
67
+
// we should get the argv filled with data from the middleware
68
+
argv.mw.should.equal('mw')
69
+
returndone()
70
+
}
71
+
)
72
+
.exitProcess(false)// defaults to true.
73
+
.parse()
74
+
})
75
+
76
+
it('runs the before-validation middleware and ensures theres a context object with commands and availableOptions',function(done){
77
+
yargs(['mw'])
78
+
.middleware(function(argv){
79
+
argv.mw='foobar'
80
+
argv.other=true
81
+
},true)
82
+
.command(
83
+
'mw',
84
+
'adds func to middleware',
85
+
{
86
+
'mw': {
87
+
'demand': true,
88
+
'string': true
89
+
}
90
+
},
91
+
function(argv){
92
+
// we should get the argv filled with data from the middleware
93
+
argv.mw.should.equal('foobar')
94
+
argv.other.should.equal(true)
95
+
returndone()
96
+
}
97
+
)
98
+
.exitProcess(false)// defaults to true.
99
+
.parse()
100
+
})
101
+
102
+
it('runs the before-validation middlware with an array passed in and ensures theres a context object with commands and availableOptions',function(done){
103
+
yargs(['mw'])
104
+
.middleware([function(argv){
105
+
argv.mw='mw'
106
+
argv.other=true
107
+
}],true)
108
+
.command(
109
+
'mw',
110
+
'adds func to middleware',
111
+
{
112
+
'mw': {
113
+
'demand': true,
114
+
'string': true
115
+
}
116
+
},
117
+
function(argv){
118
+
// we should get the argv filled with data from the middleware
119
+
argv.mw.should.equal('mw')
120
+
argv.other.should.equal(true)
121
+
returndone()
122
+
}
123
+
)
124
+
.exitProcess(false)// defaults to true.
125
+
.parse()
126
+
})
127
+
128
+
it('runs the before-validation middlware ensures if an async function is ran it throws an error',function(done){
129
+
try{
130
+
yargs(['mw'])
131
+
.middleware([asyncfunction(argv){
132
+
argv.mw='mw'
133
+
argv.other=true
134
+
}],true)
135
+
.command(
136
+
'mw',
137
+
'adds func to middleware',
138
+
{
139
+
'mw': {
140
+
'demand': true,
141
+
'string': true
142
+
}
143
+
},
144
+
function(argv){
145
+
// we should get the argv filled with data from the middleware
146
+
argv.mw.should.equal('mw')
147
+
argv.other.should.equal(true)
148
+
returndone(newError('This should not have reached this point.'))
149
+
}
150
+
)
151
+
.exitProcess(false)// defaults to true.
152
+
.parse()
153
+
}catch(err){
154
+
expect(err.message).to.equal('The passed in middleware with applyBeforeValidation set to true may not be used with async functions.')
155
+
done()
156
+
}
157
+
})
158
+
159
+
it('Ensure middleware does not run non-before-validation middleware, and vice versa',function(done){
160
+
letexecPreOnce=false
161
+
letexecPostOnce=false
162
+
yargs(['mw'])
163
+
.middleware([function(argv){
164
+
expect(execPreOnce).to.equal(false)
165
+
execPreOnce=true
166
+
expect(argv).to.be.an('object')
167
+
argv.mw='mw'
168
+
argv.other=true
169
+
}],true)
170
+
.middleware([function(argv){
171
+
expect(execPostOnce).to.equal(false)
172
+
execPostOnce=true
173
+
expect(argv).to.be.an('object')
174
+
}])
175
+
.command(
176
+
'mw',
177
+
'adds func to middleware',
178
+
{
179
+
'mw': {
180
+
'demand': true,
181
+
'string': true
182
+
}
183
+
},
184
+
function(argv){
185
+
// we should get the argv filled with data from the middleware
186
+
argv.mw.should.equal('mw')
187
+
argv.other.should.equal(true)
188
+
returndone()
189
+
}
190
+
)
191
+
.exitProcess(false)// defaults to true.
192
+
.parse()
193
+
})
194
+
52
195
it('runs all middleware before reaching the handler',function(done){
0 commit comments