1
1
#!/usr/bin/env node
2
2
3
3
// const commander = require('commander'); // (normal include)
4
- const commander = require ( '../' ) ; // include commander in git clone of commander repo
5
- const program = new commander . Command ( ) ;
4
+ const { Command , Option } = require ( '../' ) ; // include commander in git clone of commander repo
5
+ const program = new Command ( ) ;
6
6
7
7
// This example shows using some hooks for life cycle events.
8
8
9
9
const timeLabel = 'command duration' ;
10
10
program
11
- . option ( '-p, - -profile' , 'show how long command takes' )
11
+ . option ( '--profile' , 'show how long command takes' )
12
12
. hook ( 'preAction' , ( thisCommand ) => {
13
13
if ( thisCommand . opts ( ) . profile ) {
14
14
console . time ( timeLabel ) ;
@@ -21,7 +21,7 @@ program
21
21
} ) ;
22
22
23
23
program
24
- . option ( '-t, - -trace' , 'display trace statements for commands' )
24
+ . option ( '--trace' , 'display trace statements for commands' )
25
25
. hook ( 'preAction' , ( thisCommand , actionCommand ) => {
26
26
if ( thisCommand . opts ( ) . trace ) {
27
27
console . log ( '>>>>' ) ;
@@ -32,25 +32,34 @@ program
32
32
}
33
33
} ) ;
34
34
35
- program . command ( 'delay' )
36
- . option ( '--message <value>' , 'custom message to display' , 'Thanks for waiting' )
37
- . argument ( '[seconds]' , 'how long to delay' , '1' )
38
- . action ( async ( waitSeconds , options ) => {
39
- await new Promise ( resolve => setTimeout ( resolve , parseInt ( waitSeconds ) * 1000 ) ) ;
40
- console . log ( options . message ) ;
35
+ program
36
+ . option ( '--env <filename>' , 'specify environment file' )
37
+ . hook ( 'preSubcommand' , ( thisCommand , subcommand ) => {
38
+ if ( thisCommand . opts ( ) . env ) {
39
+ // One use case for this hook is modifying environment variables before
40
+ // parsing the subcommand, say by reading .env file.
41
+ console . log ( `Reading ${ thisCommand . opts ( ) . env } ...` ) ;
42
+ process . env . PORT = 80 ;
43
+ console . log ( `About to call subcommand: ${ subcommand . name ( ) } ` ) ;
44
+ }
41
45
} ) ;
42
46
43
- program . command ( 'hello' )
44
- . option ( '-e, --example' )
45
- . action ( ( ) => console . log ( 'Hello, world' ) ) ;
47
+ program . command ( 'start' )
48
+ . argument ( '[script]' , 'script name' , 'server.js' )
49
+ . option ( '-d, --delay <seconds>' , 'how long to delay before starting' )
50
+ . addOption ( new Option ( '-p, --port <number>' , 'port number' ) . default ( 8080 ) . env ( 'PORT' ) )
51
+ . action ( async ( script , options ) => {
52
+ if ( options . delay ) {
53
+ await new Promise ( resolve => setTimeout ( resolve , parseInt ( options . delay ) * 1000 ) ) ;
54
+ }
55
+ console . log ( `Starting ${ script } on port ${ options . port } ` ) ;
56
+ } ) ;
46
57
47
58
// Some of the hooks or actions are async, so call parseAsync rather than parse.
48
59
program . parseAsync ( ) . then ( ( ) => { } ) ;
49
60
50
61
// Try the following:
51
- // node hook.js hello
52
- // node hook.js --profile hello
53
- // node hook.js --trace hello --example
54
- // node hook.js delay
55
- // node hook.js --trace delay 5 --message bye
56
- // node hook.js --profile delay
62
+ // node hook.js start
63
+ // node hook.js --trace start --port 9000 test.js
64
+ // node hook.js --profile start --delay 5
65
+ // node hook.js --env=production.env start
0 commit comments