How to use the showHelp function from yargs

Find comprehensive JavaScript yargs.showHelp code examples handpicked from public code repositorys.

yargs.showHelp is a method that displays the usage information and options for a command-line application that uses the yargs module for argument parsing.

22
23
24
25
26
27
28
29
30
31
            .options('ppi', {describe:'Pixels per inch used to convert m, mm, cm, in, pt, pc to pixels', default:90.714});

var options = yargs.argv;

if (options.help) {
    yargs.showHelp();
    process.exit(0);
}

if (options.version) {
fork icon128
star icon641
watch icon144

+ 5 other calls in file

42
43
44
45
46
47
48
49
50
51
  }
}

function processInputs (text, opts) {
  if (!text.length) {
    yargs.showHelp()
    process.exit(1)
  }

  if (opts.output) {
fork icon585
star icon0
watch icon90

+ 25 other calls in file

How does yargs.showHelp work?

yargs.showHelp works by displaying the usage information and options for a command-line application that uses the yargs module for argument parsing.

When called, yargs.showHelp prints out the usage information and options that have been defined using the yargs API, including:

  • The usage message, which is typically a brief description of how to use the application and its arguments.
  • The positional arguments and options that have been defined using yargs.positional, yargs.option, and other related methods.

The output of yargs.showHelp is typically displayed in the console, and is often triggered by the user passing the --help option to the application.

By using yargs.showHelp, you can provide users with a helpful message that explains how to use your application's command-line arguments and options, and helps them to troubleshoot any issues they may encounter.

100
101
102
103
104
105
106
107
108
109
  default:
    throw new Error('Invalid type!');
  }
}

main(argv, () => yargs.showHelp())
  .then(() => process.exit(0))
  .catch((e) => {
    console.log(`Fatal Error: ${e.message}`);
    d(e.stack);
fork icon25
star icon404
watch icon6

+ 14 other calls in file

33
34
35
36
37
38
39
40
41
42
  let pkgJson = require(path.join(__dirname, '..', 'package.json'));
  console.log(`Surf ${pkgJson.version}`);
  process.exit(0);
}

main(argv.r, argv.j, argv.help, () => yargs.showHelp())
  .then(() => process.exit(0))
  .catch((e) => {
    console.log(`Fatal Error: ${e.message}`);
    d(e.stack);
fork icon25
star icon405
watch icon6

+ 9 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const yargs = require("yargs");

const options = yargs.usage("Usage: $0 [options] ").options({
  v: {
    alias: "verbose",
    describe: "Enable verbose logging",
    type: "boolean",
    default: false,
  },
  h: {
    alias: "help",
    describe: "Display usage information",
  },
}).argv;

if (options.help) {
  // Display usage information and options
  yargs.showHelp();
} else {
  // Do something with the command-line arguments
  console.log("Processing file:", options._[0]);
}

In this example, we first use yargs to define the command-line arguments and options for our tool. We specify a usage message that describes how to use the tool and its options, and define two options: -v/--verbose, which enables verbose logging, and -h/--help, which displays usage information. We then check whether the --help option has been passed by the user. If it has, we call yargs.showHelp() to display the usage information and options. If it hasn't, we assume that the user has passed a file name as an argument, and we log a message indicating that we are processing that file. By using yargs.showHelp(), we make it easy for users to learn how to use our command-line tool and its options, and provide a helpful error message if they pass an invalid option or argument.

25
26
27
28
29
30
31
32
33
34


function main() {
    var args = argv.argv;
    if (args.h === true || args.help === true) {
        argv.showHelp();
        return;
    }

    var bduss = args.bduss || process.env.BDUSS;
fork icon2
star icon7
watch icon1

+ 39 other calls in file

17
18
19
20
21
22
23
24
25
26
.commandDir('cmds/core/')
.commandDir('cmds/checker/')
.commandDir('cmds/util/')
.command({
  command: '*',
  handler() { yargs.showHelp(); }
})
.showHelpOnFail(true)
.version('version', 'show version number', '0.8.1')
.help('help', 'show usage instructions')
fork icon7
star icon76
watch icon2

+ 3 other calls in file

38
39
40
41
42
43
44
                })
                .array('environments'),
        argv    = yargs.argv;

if(argv._.length == 0 && argv.file == undefined) {
        yargs.showHelp('log');
}
fork icon1
star icon6
watch icon2

177
178
179
180
181
182
183
184
185
186
    startKongDashboard(argv, angularConfig);
  });
}

function abortAndShowHelp() {
  program.showHelp();
  process.exit(1);
}

function startKongDashboard(backendConfig, angularConfig) {
fork icon402
star icon0
watch icon89

+ 3 other calls in file

Other functions in yargs

Sorted by popularity

function icon

yargs.argv is the most popular function in yargs (1012 examples)