-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdeprecated.md
209 lines (148 loc) · 5.72 KB
/
deprecated.md
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Deprecated
These features are deprecated, which means they may go away in a future major version of Commander.
They are currently still available for backwards compatibility, but should not be used in new code.
- [Deprecated](#deprecated)
- [RegExp .option() parameter](#regexp-option-parameter)
- [noHelp](#nohelp)
- [Default import of global Command object](#default-import-of-global-command-object)
- [Callback to .help() and .outputHelp()](#callback-to-help-and-outputhelp)
- [.on('--help')](#on--help)
- [.on('command:\*')](#oncommand)
- [.command('\*')](#command)
- [cmd.description(cmdDescription, argDescriptions)](#cmddescriptioncmddescription-argdescriptions)
- [InvalidOptionArgumentError](#invalidoptionargumenterror)
- [Short option flag longer than a single character](#short-option-flag-longer-than-a-single-character)
- [Import from `commander/esm.mjs`](#import-from-commanderesmmjs)
## RegExp .option() parameter
The `.option()` method allowed a RegExp as the third parameter to restrict what values were accepted.
```js
program.option('-c,--coffee <type>', 'coffee', /short-white|long-black/);
```
Removed from README in Commander v3. Deprecated from Commander v7.
The newer functionality is the Option `.choices()` method, or using a custom option processing function.
## noHelp
This was an option passed to `.command()` to hide the command from the built-in help:
```js
program.command('example', 'examnple command', { noHelp: true });
```
The option was renamed `hidden` in Commander v5.1. Deprecated from Commander v7.
## Default import of global Command object
The default import was a global Command object.
```js
const program = require('commander');
```
The global Command object is exported as `program` from Commander v5, or import the Command object.
```js
const { program } = require('commander');
// or
const { Command } = require('commander');
const program = new Command()
```
- Removed from README in Commander v5.
- Deprecated from Commander v7.
- Removed from TypeScript declarations in Commander v8.
## Callback to .help() and .outputHelp()
These routines allowed a callback parameter to process the built-in help before display.
```js
program.outputHelp((text) => {
return colors.red(text);
});
```
The newer approach is to directly access the built-in help text using `.helpInformation()`.
```js
console.error(colors.red(program.helpInformation()));
```
Deprecated from Commander v7.
## .on('--help')
This was the way to add custom help after the built-in help. From Commander v3.0.0 this used the custom long help option flags, if changed.
```js
program.on('--help', function() {
console.log('')
console.log('Examples:');
console.log(' $ custom-help --help');
console.log(' $ custom-help -h');
});
```
The replacement is `.addHelpText()`:
```js
program.addHelpText('after', `
Examples:
$ custom-help --help
$ custom-help -h`
);
```
Deprecated from Commander v7.
## .on('command:*')
This was emitted when the command argument did not match a known subcommand (as part of the implementation of `.command('*')`).
One use was for adding an error for an unknown subcommand. An error is now the default built-in behaviour.
A second related use was for making a suggestion for an unknown subcommand. The replacement built-in support is `.showSuggestionAfterError()`,
or for custom behaviour catch the `commander.unknownCommand` error.
Deprecated from Commander v8.3.
## .command('*')
This was used to add a default command to the program.
```js
program
.command('*')
.action(() => console.log('List files by default...'));
```
You may now pass a configuration option of `isDefault: true` when adding a command, whether using a subcommand with an action handler or a stand-alone executable subcommand.
```js
program
.command('list', { isDefault: true })
.action(() => console.log('List files by default...'));
```
Removed from README in Commander v5. Deprecated from Commander v8.3.
## cmd.description(cmdDescription, argDescriptions)
This was used to add command argument descriptions for the help.
```js
program
.command('price <book>')
.description('show price of book', {
book: 'ISBN number for book'
});
```
The new approach is to use the `.argument()` method.
```js
program
.command('price')
.description('show price of book')
.argument('<book>', 'ISBN number for book');
```
Deprecated from Commander v8.
## InvalidOptionArgumentError
This was used for throwing an error from custom option processing, for a nice error message.
```js
function myParseInt(value, dummyPrevious) {
// parseInt takes a string and a radix
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new commander.InvalidOptionArgumentError('Not a number.');
}
return parsedValue;
}
```
The replacement is `InvalidArgumentError` since can be used now for custom command-argument processing too.
```js
function myParseInt(value, dummyPrevious) {
// parseInt takes a string and a radix
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new commander.InvalidArgumentError('Not a number.');
}
return parsedValue;
}
```
Deprecated from Commander v8.
## Short option flag longer than a single character
Short option flags like `-ws` were never supported, but the old README did not make this clear. The README now states that short options are a single character.
README updated in Commander v3. Deprecated from Commander v9.
## Import from `commander/esm.mjs`
The first support for named imports required an explicit entry file.
```js
import { Command } from 'commander/esm.mjs';
```
This is no longer required, just import directly from the module.
```js
import { Command } from 'commander';
```
README updated in Commander v9. Deprecated from Commander v9.