0.5.165 • Published 11 days ago

interactive-commander v0.5.165

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

Interactive Commander

Interactive Commander is an extension of the widely-used Commander.js library. It seamlessly integrates configurable interactive prompts for missing options in your CLI application, enhancing user experience with minimal effort.

Video Demo

Features

  • Full compatibility with Commander.js (utilizes Commander.js under the hood and serves as a drop-in replacement for it)
  • Customizable flags for interactive mode
  • Automatic prompting for missing options in interactive mode
  • Built-in Inquirer.js prompts for boolean, multiple-choice, and string options, along with the ability to create custom prompts
  • Support for plugins

Installation

npm install --save interactive-commander

You can also scaffold a new project with Interactive Commander and TypeScript with create-interactive-commander. To do so, run the following command and follow the prompts:

npm create interactive-commander@latest

Usage

import { InteractiveCommand, InteractiveOption } from "interactive-commander";

const program = new InteractiveCommand();

program
  .command("pizza")
  // Detached options are interactive by default
  .option("-d, --drink", "drink")

  // Missing mandatory options won't throw an error in interactive mode
  .requiredOption("-o, --olive-oil", "olive oil")

  // Boolean InteractiveOptions will show a confirmation prompt by default
  .option("-c, --cheese", "cheese")
  .option("-C, --no-cheese", "no cheese")

  .addOption(
    new InteractiveOption("-n, --count <number>", "number of pizzas")
      // InteractiveOption supports all the methods of Commander.js' Option
      .argParser(Number)
      .default(1),
  )

  .addOption(
    new InteractiveOption(
      "--non-interactive-option <value>",
      "non-interactive option",
    )
      // Passing in undefined to prompt will disable interactive mode for this option
      .prompt(undefined)
      .default("default value"),
  )

  // InteractiveOptions with choices will show a select prompt by default
  .addOption(
    new InteractiveOption("-s, --size <size>", "size")
      .choices(["small", "medium", "large"])
      .default("medium")
      .makeOptionMandatory(),
  )

  .addOption(
    new InteractiveOption("-m, --name <string>", "your name")
      // You can use the prompt method to implement your own prompt logic
      .prompt(async (currentValue, option, command) => {
        // TODO: Implement your own prompt logic here
        const answer = "world";

        // Return the answer
        return answer;
      })
      .makeOptionMandatory(),
  )

  .addOption(
    new InteractiveOption("-r, --voucher-code <string>", "voucher code")
      // The prompt input gets validated by the argParser function
      .argParser((value) => {
        if (typeof value !== "string" || !/^\d{4}$/.test(value)) {
          throw new TypeError("Invalid voucher code");
        }

        return value;
      }),
  )

  .action((_options, cmd: InteractiveCommand) => {
    console.log("Options: %o", cmd.opts());
  });

await program
  // Enables interactive mode (when -i or --interactive is passed in)
  // This should almost always be called on the root command right before
  // calling parseAsync
  .interactive("-i, --interactive", "interactive mode")
  .parseAsync(process.argv);

// Try the following commands:
// command-name pizza
// command-name pizza -i
// command-name pizza -i --count 2
// command-name pizza -i --count 2 --no-cheese
// command-name pizza -i --name "John Doe"
// command-name pizza -i --name "John Doe" --non-interactive-option abc

More examples can be found in the examples directory.

Recipes

Interactive options for the root command

Interactive options on main command won't be prompted for in interactive mode if no subcommand is invoked. That is because Commander.js doesn't support pre-parse (similar to preSubcommand hooks) hooks for the main command. As a workaround, you can define a subcommand as the default command to achieve a similar effect.

See default-subcommand.ts for an example.

Enable interactive mode by default

To enable interactive mode by default, you can define the interactive flags as negatable boolean options (e.g. --no-interactive).

See no-interactive.ts for an example.

Setting default values for option prompts based on other options

In some cases, it may be necessary for the default value of an option prompt to depend on the value of another option. For example, you might want the billing address to be automatically set to the same value as the user's input for the shipping address. To achieve that, you can decorate the readFunction of the of the billing address option to dynamically set the default value of the prompt.

See dependent-prompts.ts for an example.

0.5.165

11 days ago

0.5.164

14 days ago

0.5.163

23 days ago

0.5.162

24 days ago

0.5.161

25 days ago

0.5.158

27 days ago

0.5.159

27 days ago

0.5.160

26 days ago

0.5.157

1 month ago

0.5.156

2 months ago

0.5.154

2 months ago

0.5.155

2 months ago

0.5.153

2 months ago

0.5.152

2 months ago

0.5.151

2 months ago

0.5.150

2 months ago

0.5.149

2 months ago

0.5.148

2 months ago

0.5.147

2 months ago

0.5.146

2 months ago

0.5.145

2 months ago

0.5.143

2 months ago

0.5.144

2 months ago

0.5.142

2 months ago

0.5.141

2 months ago

0.5.140

2 months ago

0.5.139

2 months ago

0.5.138

2 months ago

0.5.137

2 months ago

0.5.136

2 months ago

0.5.135

2 months ago

0.5.134

3 months ago

0.5.132

3 months ago

0.5.133

3 months ago

0.5.131

3 months ago

0.5.130

3 months ago

0.5.129

3 months ago

0.5.127

3 months ago

0.5.125

3 months ago

0.5.126

3 months ago

0.5.124

3 months ago

0.5.123

3 months ago

0.5.121

3 months ago

0.5.120

3 months ago

0.5.122

3 months ago

0.5.119

3 months ago

0.5.118

3 months ago

0.5.116

3 months ago

0.5.117

3 months ago

0.5.115

3 months ago

0.5.114

3 months ago

0.5.113

3 months ago

0.5.112

3 months ago

0.5.110

3 months ago

0.5.111

3 months ago

0.5.109

4 months ago

0.5.108

4 months ago

0.5.107

4 months ago

0.5.106

4 months ago

0.5.105

4 months ago

0.5.104

4 months ago

0.5.103

4 months ago

0.5.102

4 months ago

0.5.101

4 months ago

0.5.100

4 months ago

0.5.98

4 months ago

0.5.99

4 months ago

0.5.97

4 months ago

0.5.96

4 months ago

0.5.95

4 months ago

0.5.94

4 months ago

0.5.93

4 months ago

0.5.92

4 months ago

0.5.91

4 months ago

0.5.90

4 months ago

0.5.89

4 months ago

0.5.88

4 months ago

0.5.87

4 months ago

0.5.86

4 months ago

0.5.85

4 months ago

0.5.84

4 months ago

0.5.83

4 months ago

0.5.81

5 months ago

0.5.82

5 months ago

0.5.80

5 months ago

0.5.79

5 months ago

0.5.77

5 months ago

0.5.78

5 months ago

0.5.76

5 months ago

0.5.75

5 months ago

0.5.74

5 months ago

0.5.72

5 months ago

0.5.73

5 months ago

0.5.70

5 months ago

0.5.71

5 months ago

0.5.65

5 months ago

0.5.66

5 months ago

0.5.63

5 months ago

0.5.64

5 months ago

0.5.61

5 months ago

0.5.62

5 months ago

0.5.60

5 months ago

0.5.67

5 months ago

0.5.68

5 months ago

0.5.54

6 months ago

0.5.55

5 months ago

0.5.52

6 months ago

0.5.53

6 months ago

0.5.50

6 months ago

0.5.51

6 months ago

0.5.58

5 months ago

0.5.59

5 months ago

0.5.56

5 months ago

0.5.57

5 months ago

0.5.43

6 months ago

0.5.44

6 months ago

0.5.41

6 months ago

0.5.42

6 months ago

0.5.40

6 months ago

0.5.49

6 months ago

0.5.47

6 months ago

0.5.48

6 months ago

0.5.45

6 months ago

0.5.46

6 months ago

0.5.38

6 months ago

0.5.39

6 months ago

0.5.36

6 months ago

0.5.37

6 months ago

0.5.35

6 months ago

0.5.32

6 months ago

0.5.33

6 months ago

0.5.30

6 months ago

0.5.31

6 months ago

0.5.34

6 months ago

0.5.29

7 months ago

0.5.28

7 months ago

0.5.27

7 months ago

0.5.26

7 months ago

0.5.25

7 months ago

0.5.24

7 months ago

0.5.23

7 months ago

0.5.22

7 months ago

0.5.21

7 months ago

0.5.20

7 months ago

0.5.19

7 months ago

0.5.18

7 months ago

0.5.17

7 months ago

0.5.16

7 months ago

0.5.15

7 months ago

0.5.14

7 months ago

0.5.13

7 months ago

0.5.12

7 months ago

0.5.11

7 months ago

0.5.10

7 months ago

0.5.9

7 months ago

0.5.8

7 months ago

0.5.7

7 months ago

0.5.6

7 months ago

0.5.5

7 months ago

0.5.4

7 months ago

0.5.3

7 months ago

0.5.2

7 months ago

0.5.1

7 months ago

0.5.0

7 months ago

0.4.6

7 months ago

0.4.5

7 months ago

0.4.4

7 months ago

0.4.3

7 months ago

0.4.2

7 months ago

0.4.1

8 months ago

0.4.0

8 months ago

0.3.1

8 months ago

0.3.0

8 months ago

0.2.6

8 months ago

0.2.5

8 months ago

0.2.4

8 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.1.5

8 months ago

0.1.4

8 months ago

0.1.3

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago