Skip to content
Myr

custom-command-class.js

This file

Path

examples/custom-command-class.js

46 lines · 1.3 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

The walk stopped after 200 commits: lines older than 471ec40 point at it.

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 1why, one step back
1#!/usr/bin/env node

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

line 2why, one step back
2import { Command } from 'commander';

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 3–5why, one step back
3
4// Use a class override to customise the command and its subcommands.5

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

line 6why, one step back
6class CommandWithTrace extends Command {

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

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

ESLint changes preparing for Prettier (#2153)John Geebefore adoption6fc933e

line 13why, one step back
13}

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 14why, one step back
14

Merge branch 'develop' into release/13.xJohn Geebefore adoption5a79585

line 15why, one step back
15function inspectCommand(command) {

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 16–19why, one step back
16 // The option value is stored as property on command because we called .storeOptionsAsProperties()
17 console.log(`Called '${command.name()}'`);18 console.log(`args: ${command.args}`);19 console.log('opts: %o', command.opts());

ESLint changes preparing for Prettier (#2153)John Geebefore adoption6fc933e

line 20why, one step back
20}

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 21–22why, one step back
21
22const program = new CommandWithTrace('program')

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

line 23why, one step back
23 .option('-v, --verbose')

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 24why, one step back
24 .action((options, command) => {

Merge branch 'develop' into release/13.xJohn Geebefore adoption5a79585

line 25why, one step back
25 inspectCommand(command);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 26–31why, one step back
26 });
2728program29 .command('serve [params...]')30 .option('-p, --port <number>', 'port number')31 .action((params, options, command) => {

Merge branch 'develop' into release/13.xJohn Geebefore adoption5a79585

line 32why, one step back
32 inspectCommand(command);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 33–34why, one step back
33 });
34

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 35why, one step back
35program.command('build <target>').action((buildTarget, options, command) => {

Merge branch 'develop' into release/13.xJohn Geebefore adoption5a79585

line 36why, one step back
36 inspectCommand(command);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 37why, one step back
37});

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 38–46why, one step back
38
39program.parse();4041// Try the following:42// node custom-command-class.js --help43// node custom-command-class.js serve --help44// node custom-command-class.js serve -t -p 80 a b c45// node custom-command-class.js build --help46// node custom-command-class.js build --trace foo