11#!/usr/bin/env node
22
33// 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 ( ) ;
66
77// This example shows using some hooks for life cycle events.
88
99const timeLabel = 'command duration' ;
1010program
11- . option ( '-p, - -profile' , 'show how long command takes' )
11+ . option ( '--profile' , 'show how long command takes' )
1212 . hook ( 'preAction' , ( thisCommand ) => {
1313 if ( thisCommand . opts ( ) . profile ) {
1414 console . time ( timeLabel ) ;
@@ -21,7 +21,7 @@ program
2121 } ) ;
2222
2323program
24- . option ( '-t, - -trace' , 'display trace statements for commands' )
24+ . option ( '--trace' , 'display trace statements for commands' )
2525 . hook ( 'preAction' , ( thisCommand , actionCommand ) => {
2626 if ( thisCommand . opts ( ) . trace ) {
2727 console . log ( '>>>>' ) ;
@@ -32,25 +32,34 @@ program
3232 }
3333 } ) ;
3434
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+ }
4145 } ) ;
4246
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+ } ) ;
4657
4758// Some of the hooks or actions are async, so call parseAsync rather than parse.
4859program . parseAsync ( ) . then ( ( ) => { } ) ;
4960
5061// 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