How to use the positional function from yargs

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

yargs.positional is a method in the yargs library that allows you to specify positional arguments in a command line interface.

56
57
58
59
60
61
62
63
64
65
    type: 'string',
    describe: 'Rules to use for transitive dependency resolution.',
    choices: ['deps', 'assoc']
});

yargs.positional('package', {
    type: 'string',
    describe: 'Path to the code package to be dockerized. Can be relative to cwd.',
    default: '.'
});
fork icon18
star icon24
watch icon2

162
163
164
165
166
167
168
169
170
171


yargs
    .command('generate [path]',
             'automatically generate a new lock file for repository',
             (yargs) => yargs.positional('path', {describe: 'path to git repository', default: './'}),
             (argv) => generateLockFileForRepo(argv.path).then((lf) => console.log(lockFileToString(lf))))
    .command('check [path]',
             'check licenses against repository\'s lock file',
             (yargs) => yargs.positional('path', {describe: 'path to git repository', default: './'}),
fork icon16
star icon18
watch icon82

+ 19 other calls in file

How does yargs.positional work?

yargs.positional() is a method in the Yargs library that allows you to define and parse positional arguments passed to a command-line interface (CLI) program.

This method lets you define the positional arguments, specify their data types and provide optional configuration options, such as specifying default values and making them required.

When the program is run, Yargs will parse the positional arguments and pass them to the callback function provided in the .command() method.

7
8
9
10
11
12
13
14
15
16
.usage('$0 <cmd> [args]')
.command(
  'init <name>',
  'Initialize an empty starter project with the given name where you can write the code for your framework',
  (yargs) => {
    return yargs.positional('name', {
      describe: 'name of the framework',
      type: 'string',
    })
  },
fork icon1
star icon11
watch icon1

76
77
78
79
80
81
82
83
84
85
    )
    .wrap(100)
    .help();
})
.command("get <resource>", "Get resource/s <<resource>>", (yargs: any) => {
  yargs.positional("resource", {
    describe: "The resource to get information about",
    choices: Object.values(ResourceEnum),
  });
  yargs.version(false).wrap(150).help();
fork icon4
star icon10
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const yargs = require("yargs");

yargs.command({
  command: "test ",
  describe: "Test a file",
  handler: (argv) => {
    console.log(`Testing file: ${argv.filename}`);
  },
});

yargs.parse();

Here, test defines a positional argument called filename, which is accessed through argv.filename in the handler function. When the user runs the command node app.js test file.txt, the output will be Testing file: file.txt.

38
39
40
41
42
43
44
45
46
.version(pkg.version)
.command(
    'dump-sprites <config> <archive>',
    'dump sprites PNGs from entitiy, media or textures jag/mem archives',
    (yargs) => {
        yargs.positional('config', {
            description: 'config jag archive',
            type: 'string'
        });
fork icon4
star icon6
watch icon3

+ 3 other calls in file

48
49
50
51
52
53
54
55
56
57
58
const Server = new server.Server(ProcessController)


// Parse the arguments on initialization
const argv = yargs
  .command('start', 'Whether or not to immediately start python process or not', (yargs) => {
    yargs.positional('start', {
      type: 'boolean',
      default: true,
      describe: 'Whether to start process immediately'
    })
fork icon1
star icon6
watch icon0

19
20
21
22
23
24
25
26
27
28
29
const command = {};


command.command = 'deploy [dir]';
command.describe = 'Deploy the output of a static generator';
command.builder = (yargs) => {
  yargs.positional('dir', {
    describe: 'Optional location of build artefacts ',
    type: 'string',
    default: null,
  });
fork icon0
star icon2
watch icon3

Other functions in yargs

Sorted by popularity

function icon

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