Skip to content
Myr

custom-command-class.js

This file

Path

examples/custom-command-class.js

50 lines · 1.5 KiB · JavaScript

Last changed by

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

History of this file

Take it

The walk stopped after 200 commits: lines older than 1a3db74 point at it.

Bump required version of node to oldest LTS which is 10 (#1413)John Geebefore adoption1a3db74

lines 1–5why, one step back
1#!/usr/bin/env node
23// const commander = require('commander'); // (normal include)4const commander = require('../'); // include commander in git clone of commander repo5

Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63

line 6why, one step back
6// Use a class override to customise the command and its subcommands.

Bump required version of node to oldest LTS which is 10 (#1413)John Geebefore adoption1a3db74

line 7why, one step back
7

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

line 8why, one step back
8class CommandWithTrace extends commander.Command {

Bump required version of node to oldest LTS which is 10 (#1413)John Geebefore adoption1a3db74

line 9why, one step back
9 createCommand(name) {

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

lines 10–13why, one step back
10 const cmd = new CommandWithTrace(name);
11 // Add an option to subcommands created using `.command()`12 cmd.option('-t, --trace', 'display extra information when run command');13 return cmd;

Bump required version of node to oldest LTS which is 10 (#1413)John Geebefore adoption1a3db74

lines 14–16why, one step back
14 }
15};16

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

line 17why, one step back
17function inpectCommand(command) {

Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63

line 18why, one step back
18 // The option value is stored as property on command because we called .storeOptionsAsProperties()

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

line 19why, one step back
19 console.log(`Called '${command.name()}'`);

Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63

line 20why, one step back
20 console.log(`args: ${command.args}`);

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

line 21why, one step back
21 console.log('opts: %o', command.opts());

Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63

line 22why, one step back
22};

Bump required version of node to oldest LTS which is 10 (#1413)John Geebefore adoption1a3db74

line 23why, one step back
23

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

lines 24–27why, one step back
24const program = new CommandWithTrace('program')
25 .option('-v, ---verbose')26 .action((options, command) => {27 inpectCommand(command);

Bump required version of node to oldest LTS which is 10 (#1413)John Geebefore adoption1a3db74

lines 28–29why, one step back
28 });
29

Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63

line 30why, one step back
30program

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

lines 31–34why, one step back
31 .command('serve [params...]')
32 .option('-p, --port <number>', 'port number')33 .action((params, options, command) => {34 inpectCommand(command);

Use custom command example to show revert to Commander v6 behaviours (#1421)John Geebefore adoption75c2a63

lines 35–36why, one step back
35 });
36

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

lines 37–42why, one step back
37program
38 .command('build <target>')39 .action((buildTarget, options, command) => {40 inpectCommand(command);41 });42

Bump required version of node to oldest LTS which is 10 (#1413)John Geebefore adoption1a3db74

lines 43–45why, one step back
43program.parse();
4445// Try the following:

Make new excess arguments error opt-in (#1429)John Geebefore adoptione2670f4

lines 46–50why, one step back
46// node custom-command-class.js --help
47// node custom-command-class.js serve --help48// node custom-command-class.js serve -t -p 80 a b c49// node custom-command-class.js build --help50// node custom-command-class.js build --trace foo