How to use the opts function from commander

Find comprehensive JavaScript commander.opts code examples handpicked from public code repositorys.

commander.opts is a method in the Commander.js library that returns an object containing the options passed to a command.

15
16
17
18
19
20
21
22
23
24
25
26
  .option('-s, --start-page [start-page]', 'Specify a start page. Defaults to index.html', 'index.html')
  .option('-f, --fallback [fallback]', 'Fallback to the start page when route is not found')
  .option('-v, --verbose [verbose]', 'Turning on logging on the server and client side. Defaults to false', false)
  .parse(process.argv)


const options = program.opts()


const runFile = path.join(os.tmpdir(), 'reload-' + Math.random().toString().slice(2))
const serverFile = path.join(__dirname, '../lib/reload-server.js')

fork icon48
star icon282
watch icon8

38
39
40
41
42
43
44
45
46
47
    smtpPassword,
    smtpFromDomain,
    sepalHost,
    sepalUsername,
    sepalPassword
} = program.opts()

log.info('Configuration loaded')

module.exports = {
fork icon45
star icon180
watch icon0

How does commander.opts work?

commander.opts is a method in the Commander.js library that allows for the definition of options for a command-line interface. It provides a way to specify the various options that a command can take and their configuration, such as short and long flag names, descriptions, and default values. The method also defines how the options are parsed and mapped to the command's action function.

54
55
56
57
58
59
60
61
62
63
  .action((dest) => {
    appPathFromArgument = dest;
  })
  .parse(process.argv);

const optionsFromArguments = program.opts();
const { attributesToDisplay = [], attributesForFaceting = [] } =
  optionsFromArguments;

const getQuestions = ({ appName }) => ({
fork icon472
star icon0
watch icon131

204
205
206
207
208
209
210
211
212
213

let configExplorerResult;

let configFile: ConfigurationFileInterface = {};

const programOptions = program.opts();

if (programOptions.config) {
    let configFilePath = programOptions.config;
    let testConfigFilePath = configFilePath.match(process.cwd());
fork icon385
star icon0
watch icon77

Ai Example

1
2
3
4
5
6
7
8
9
const { program } = require("commander");

program
  .option("-p, --port ", "Port number", parseInt)
  .option("-h, --host ", "Host name or IP address")
  .option("-d, --debug", "Enable debug logging")
  .parse();

console.log(program.opts());

In the example above, .option() is used to define three options: --port, --host, and --debug. The first argument is the option string and the second argument is the description. The third argument is an optional transformer function that can be used to parse or transform the option value. Finally, .parse() is called to parse the command-line arguments and .opts() returns an object containing the parsed options.

154
155
156
157
158
159
160
161
162
163
  .option('--bucket [bucket]', 'S3 bucket name')
  .option('--tag [tag]', 'Version tag')
  .option('--no-cache-entry', 'Prevent CDN/browser caching of entry point')
  .parse(process.argv);

const cliOpts = commander.opts();

const options = {
  tag: cliOpts.tag,
  cacheEntry: cliOpts.cacheEntry,
fork icon181
star icon569
watch icon25

98
99
100
101
102
103
104
105
106
107
    : process.argv;
if (normalizedArgv.length > 1) {
    program.parse(normalizedArgv);
}

const options = program.opts();

module.exports = () => new Promise((resolve, reject) => {
    // Change working directory to 'server' before require('./server')
    process.chdir(path.resolve(__dirname, 'server'));
fork icon22
star icon102
watch icon11

+ 5 other calls in file

99
100
101
102
103
104
105
106
107
108

program
  .command('build <modelFolder> <outputFile>')
  .description('Builds single SQL script from project')
  .action((modelFolder, outputFile) => {
    const { engine } = program.opts();
    // const hooks = [];
    runAndExit(
      dbgateApi.generateModelSql({
        // client,
fork icon143
star icon0
watch icon26

+ 11 other calls in file

92
93
94
95
96
97
98
99
100
101
  for (const input of inputs) {
      input.opts.showProgressBar = (program.opts().progress === true); // force true or undefined to be true or false.
      input.opts.quiet = (program.opts().quiet === true);
      input.opts.verbose = (program.opts().verbose === true);
      input.opts.retryOn429 = (program.opts().retry === true);
      input.opts.aliveStatusCodes = program.opts().alive;
input.opts.config = program.opts().config.trim();
      
      if (program.projectBaseUrl) {
          input.opts.projectBaseUrl = `file://${program.projectBaseUrl}`;
fork icon110
star icon449
watch icon0

+ 3 other calls in file

35
36
37
38
39
40
41
42
43
44
    .option('--allowed-declaration-file-subpaths <paths>',
        'Sub paths of allowed declaration files, separated by ","')
    .option('--parseVariables', 'If set, the parser will traverse variables too.')
    .parse(process.argv);

const options = commander.opts();

console.log('make-api params', [
  options.in,
  options.isFile,
fork icon103
star icon68
watch icon14

40
41
42
43
44
45
46
47
48
  // If the input is "fanyi config", then translate the word config.
  if (process.argv.length === 3) {
    return runFY();
  }
  const { color, iciba, youdao, dictionaryapi, openaiApiKey } = args;
  const { say } = program.opts();
  const options = resolveOptions({ color, iciba, youdao, dictionaryapi, say, openaiApiKey });
  return config.write(options);
});
fork icon134
star icon0
watch icon17

+ 10 other calls in file

44
45
46
47
48
49
50
51
52
    .option('--verbose', 'show additional log info (warnings)')
    .option('--quiet', 'hide processing info')
    .parse(process.argv);

// Gather cli arguments and options
var programOpts = program.opts();

// Load the atomizer config file
var config = getConfig(programOpts.config);
fork icon100
star icon0
watch icon45

+ 9 other calls in file

9
10
11
12
13
14
15
16
17
18
  .option('--name [name]', '项目名称')
  .option('--description [description]', '项目介绍')
  .option('--example', '创建示例项目')
  .parse(process.argv)

const { name, description = '', example = false } = program.opts()

const projectName = program.args[0] || name

const project = new Project({
fork icon11
star icon8
watch icon3

169
170
171
172
173
174
175
176
177
178
  option('-t --test', 'Test the current configuration').
  option('-v --verbose', 'Run in verbose mode', false).
  version(require('../package.json').version).
  parse(process.argv);

let options = program.opts();

if (options.args) {
  options.file = options.args[0];
}
fork icon0
star icon4
watch icon7

38
39
40
41
42
43
44
45
46
47
  .option('--no-prune-snapshot', 'Avoid pruning unused snapshot')
  .option('--update-snapshot', 'Updates snapshot')
  .option('--verbose', 'Enables verbose output')
  .parse(process.argv);

const { browser: browsers, spec: specs, ...rest } = program.opts();

const cloptions = {
  browsers: (browsers && Array.from(browsers)) || [],
  specs: (specs && Array.from(specs)) || [],
fork icon115
star icon141
watch icon14

93
94
95
96
97
98
99
100
101
input.opts.showProgressBar = (program.opts().progress === true); // force true or undefined to be true or false.
input.opts.quiet = (program.opts().quiet === true);
input.opts.verbose = (program.opts().verbose === true);
input.opts.retryOn429 = (program.opts().retry === true);
input.opts.aliveStatusCodes = program.opts().alive;
const config = program.opts().config;
if (config) {
    input.opts.config = config.trim();
}
fork icon110
star icon458
watch icon7

+ 11 other calls in file

41
42
43
44
45
46
47
48
49
50
    )
    console.log('')
  })
  .parse(process.argv)

const {clean, remoteCdn: remoteCdnOption} = program.opts()

const remoteCdn = remoteCdnOption || suiWidgetEmbedderConfig.remoteCdn

if (clean) {
fork icon31
star icon150
watch icon41

19
20
21
22
23
24
if (options.args.length < 1) {
  console.error('The source_dir is required.\nRun `notebookgen --help`')
  process.exit(1)
}

notebookgen(options.args[0], options.opts())
fork icon47
star icon110
watch icon3

27
28
29
30
31
32
33
34
35
36
    console.log('    $ find-process -t port 80    # find by port "80"')
    console.log()
  })
  .parse(process.argv)

const opts = program.opts()

// check keyword
if (!keyword) {
  console.error(chalk.red('Error: search keyword cannot be empty!'))
fork icon41
star icon110
watch icon4

8
9
10
11
12
13
14
15
16
17
        .option('-h, --recourse', '');
        
        
program.parse();

const cli = program.opts();


// console.log('you ordered a pizza with:');
if (cli.recourse){
fork icon38
star icon80
watch icon13

108
109
110
111
112
113
114
115
116
117
    // open = true,
    port,
    dll = true,
    kootTest = false,
    kootDevelopment = false,
} = program.opts();

initNodeEnv();
setEnvFromCommand({
    config,
fork icon13
star icon65
watch icon7

Other functions in commander

Sorted by popularity

function icon

commander.Option is the most popular function in commander (1786 examples)