How to use the json function from yargs

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

yargs.json allows you to load the configuration of a CLI program from a JSON file.

21
22
23
24
25
26
27
28
29
30
31
32
  throw err;
});


const source = (argv.source === undefined) ? paths.appSrc : argv.source;
const out = (argv.out === undefined) ? paths.appDocs : argv.out;
const json = (argv.json === undefined) ? paths.appJsonDocs : argv.json;


const baseUrlOptions = (argv.baseUrl === undefined) ? [] : ["--baseUrl", argv.baseUrl];
const includeOptions = (argv.includes === undefined) ? [] : ["--includes", argv.includes];

fork icon193
star icon513
watch icon0

+ 4 other calls in file

How does yargs.json work?

yargs.json is a configuration method in the yargs package that allows loading command-line arguments from a JSON file, providing a convenient way to set defaults and avoid redundancy in command-line inputs. When invoked, yargs reads the configuration file and merges its contents with the parsed arguments, allowing a convenient way to load default values and simplify the use of command-line applications.

Ai Example

1
2
3
4
5
6
7
8
9
const argv = require("yargs")
  .command("serve [port]", "start the server", (yargs) => {
    yargs.positional("port", {
      describe: "port to bind on",
      default: 5000,
    });
  })
  .config("config", (configPath) => require(configPath))
  .help().argv;

In the above example, yargs.json() is used with the .config() method to load configuration from a JSON file. The .config() method takes a string representing the path to the JSON file and returns a yargs instance. In this case, the configuration file is loaded with the key config. This means that the JSON file must have a top-level key of config, and its contents will be merged with the options provided through the command line.

Other functions in yargs

Sorted by popularity

function icon

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