How to use the argv function from yargs

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

yargs.argv contains the parsed command-line arguments using yargs module.

54
55
56
57
58
59
60
61
62
63
        .help('h')
        .alias('h', 'help')

        .epilog('\nMore information about the library: www.alasql.org');

let argv = yargs.argv;
let sql = '';
let params = [];
let pipedData = '';
stdin.on('data', function (chunk) {
fork icon625
star icon0
watch icon137

13
14
15
16
17
18
19
20
21
22

const TAG_PREFIX = '';
const VERSION_PREFIX = 'v';
const RELEASE_COMMIT_MSG = 'chore(release): publish version %s';

const argv = yargs.argv;
const options = argv;
const cwd = process.cwd();

const childProcess = require('./child-process.js');
fork icon396
star icon0
watch icon97

+ 5 other calls in file

How does yargs.argv work?

yargs.argv is a property in the Yargs package in Node.js that returns an object containing the parsed command-line arguments. When yargs.argv is called, Yargs parses the command-line arguments based on the configuration provided, and then returns an object with the parsed arguments. The object contains properties representing the options and positional arguments passed in, along with their values. The argv object also includes additional properties, such as _, which is an array of all the positional arguments, and $0, which contains the name of the script file. If any errors are encountered during parsing, such as invalid arguments or missing required arguments, Yargs will throw an error. Additionally, the help and version options can be used to automatically generate help text or a version number, respectively.

19
20
21
22
23
24
25
26
27
28

const yargs = require('yargs')
const { mkdirp } = require('mkdirp')
const g = require('glob')

const argv = yargs.argv
const env = process.env

/**
 * Flatten a string or list of strings into a list of strings,
fork icon270
star icon820
watch icon43

19
20
21
22
23
24
25
26
27
28
            .options('f', {alias:'file', describe:'Outputs to the given file instead of stdout.'})
            .options('o', {alias:'output', describe:'Specify output format (mapnik, json)', default:'mapnik'})
            .options('q', {alias:'quiet', boolean:true, default:false, describe:'Do not output any warnings'})
            .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);
fork icon128
star icon642
watch icon144

+ 5 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
23
24
25
26
27
28
// index.js
const yargs = require("yargs");

const options = {
  name: {
    describe: "Your name",
    type: "string",
    demandOption: true,
  },
  age: {
    describe: "Your age",
    type: "number",
    demandOption: true,
  },
  male: {
    describe: "Whether you are male",
    type: "boolean",
    default: false,
  },
};

yargs.options(options).parse();

console.log(
  `Hello, ${yargs.argv.name}! You are ${yargs.argv.age} years old and ${
    yargs.argv.male ? "male" : "not male"
  }.`
);

This script defines three command line options: name, age, and male. It then uses yargs.options to set up the options, and finally calls yargs.parse() to parse the command line arguments. The values of the options are then available as properties of yargs.argv. The script then logs a greeting that includes the values of the options. Here's an example of running the script: css Copy code

102
103
104
105
106
107
108
109
110
            .example('gulp -o /Users/hitchcock -n my-custom-rpd', 'write the files to `/Users/hitchcock/my-custom-rpd.css` and ' +
                          '`/Users/hitchcock/my-custom-rpd.min.js`')
            .example('gulp -z /Users/hitchcock/my-style -r svg -x json -n my-rpd', 'include user style located at `/Users/hitchcock/my-style`, add SVG renderer, add JSON I/O module and place the files at `./dist/my-rpd.min.js` and `./dist/my-rpd.css`')
            .epilogue('See http://shamansir.github.io/rpd for detailed documentation. © shaman.sir, 2016');

var argv = yargs.argv;

var pkg = require('./package.json');
var Server = require('karma').Server;
fork icon67
star icon427
watch icon24

+ 3 other calls in file

26
27
28
29
30
31
32
33
34
35
      'Path to the output folder (will be generated if non-existing). Defaults to overwrite the source files in-place',
    type: 'string',
    demand: false,
  });

const commandLineOptions = yargs.argv;

if (commandLineOptions.h) {
  yargs.showHelp();
  process.exit(0);
fork icon44
star icon487
watch icon14

+ 3 other calls in file

50
51
52
53
54
55
56
57
58
  .version(pkg.version)
  .alias('v', 'version')
  .usage('Usage: $0 start|stop')
  .demand(1)

var argv = yargs.argv

if (argv._[0] === 'start') return start()
if (argv._[0] === 'stop') return stop()
fork icon42
star icon336
watch icon19

+ 9 other calls in file

5
6
7
8
9
10
11
12
13
14
require('dotenv').config();

// Would be passed to script like this:
// `ts-node set-env.ts --environment=dev`
// we get it from yargs's argv object
const environment = yargs.argv.environment;
const isProd = environment === 'prod';

const targetPath = `./src/environments/environment${environment ? '.' + environment : ''}.ts`;
const envConfigFile = `
fork icon19
star icon13
watch icon7

+ 13 other calls in file

4
5
6
7
8
9
10
11
  .commandDir('commands')
  .version(`eureka: ${require('../package.json').version}`)
  .help()
  .showHelpOnFail(false, 'Specify --help for available options')
  .epilogue(`For more help, you can use 'eureka help [command]' for the detailed information`);
if (!yargs.argv.version && yargs.argv._.length < 1) {
  yargs.showHelp();
}
fork icon9
star icon56
watch icon0

+ 13 other calls in file

146
147
148
149
150
151
152
153
154
      'dev-endpoints': 'modules',
    })
    .strict()
    .help();

  return yargs.argv;
};

const argv = createYargsConfig();
fork icon28
star icon32
watch icon16

109
110
111
112
113
114
115
116
117
118
setup.callModules('yargs', yargs);

// Calling argv parses the options and must be done after all of the option specifiers
// are defined. It's exported as a property so code can look at it if needed, but
// normally you'd handle options during loadSettings when it's passed as a parameter.
setup.argv = yargs.argv;

// Queue all of the loadSettings calls
for(var ii = 0; ii < setupModules.length; ii++) {
        if (setupModules[ii]['loadSettings'] != undefined) {
fork icon7
star icon25
watch icon7

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
        .alias('a', 'action')
        .alias('c', 'context')
        .usage('Usage for single key: $0 <key> <type> --action=[true|false] --context=[true|false] ')
        .usage('Usage for file: $0 <file>')

const args = yargs.argv
console.log('create map')
const key = args._[0]
const mapper = require('../lib/createmap/mapper')
mapper(key, args)
fork icon3
star icon16
watch icon12

18
19
20
21
22
23
24
25
26
27
    console.error('emojiporter <action> <userProvideToken>');
    return;
}

// Validate action
const action = yargs.argv._[0];
if (ActionList.indexOf(action) < 0) {
    console.error(`${action} is an invalid action. It must be one of [${ActionList}]`)
    return;
}
fork icon3
star icon14
watch icon1

+ 5 other calls in file

3
4
5
6
7
8
9
10
11
12

const scssVariables = 'src/scss/variables.scss';
const { distDirs } = require('./package.json');
const yargs = require('yargs');

const docs = yargs.argv.docs;

const generatedConfig: Config = {
  namespace: 'lib',
  buildEs5: process.argv.includes('--es5') || 'prod',
fork icon12
star icon70
watch icon7

+ 7 other calls in file

69
70
71
72
73
74
75
76
77
78

        result = await createObject("timages", tImage);
    }
}

const app = yargs.argv.a;
const server = yargs.argv.s;
const namespace = yargs.argv.n;

createImage(app, server, namespace);
fork icon13
star icon12
watch icon18

+ 7 other calls in file

49
50
51
52
53
54
55
56
57
58
        }))
        .pipe(dest('./'));
});

task('commit-changes', () => {
    const commitMsg = yargs.argv.m;
    return src('.')
        .pipe(git.add())
        .pipe(git.commit(commitMsg || '【Prerelease】Bumped version number'));
});
fork icon8
star icon11
watch icon10

+ 7 other calls in file

23
24
25
26
27
28
29
30
31
32

    .default('port', '9964');


function main() {
    var args = argv.argv;
    if (args.h === true || args.help === true) {
        argv.showHelp();
        return;
    }
fork icon2
star icon7
watch icon1

+ 19 other calls in file

35
36
37
38
39
40
41
42
43
44
                        describe: 'Set project\'s environments',
                        type: 'array',
                        alias: 'e'
                })
                .array('environments'),
        argv    = yargs.argv;

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

5
6
7
8
9
10
11
12
13
var yargs = require('yargs')

var exec = require('./runSuite');


var fileName = yargs.argv.file
var file = path.resolve(__dirname, '../', fileName);

run(file)
fork icon1
star icon11
watch icon4

+ 9 other calls in file

58
59
60
61
62
63
64
65
66
67
'use strict'

// LOGGING AND DEBUGGING 

let yargs = require('yargs');
let logLevel = yargs.argv.debugp2p?'debug':'info';
const _PROTOCOL = "/troupe/1.0.0"


const logger = require('../logger.js').mkLogger('p2p',logLevel);
fork icon9
star icon0
watch icon1

Other functions in yargs

Sorted by popularity

function icon

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