How to use getopts - 9 common examples

To help you get started, we’ve selected a few getopts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github elastic / kibana / packages / kbn-test / src / functional_tests / lib / run_cli.js View on Github external
export async function runCli(getHelpText, run) {
  try {
    const userOptions = getopts(process.argv.slice(2)) || {};
    if (userOptions.help) {
      console.log(getHelpText());
      return;
    }

    await run(userOptions);
  } catch (error) {
    if (!(error instanceof Error)) {
      error = new Error(`${inspect(error)} thrown!`);
    }

    console.log();
    console.log(chalk.red(error.message));

    // CliError is a special error class that indicates that the error is produced as a part
    // of using the CLI, and does not need a stack trace to make sense, so we skip the stack
github elastic / kibana / packages / kbn-test / src / mocha / run_mocha_cli.js View on Github external
export function runMochaCli() {
  const opts = getopts(process.argv.slice(2), {
    alias: {
      t: 'timeout',
    },
    boolean: ['no-timeouts'],
  });

  const runInBand =
    process.execArgv.includes('--inspect') || process.execArgv.includes('--inspect-brk');

  // ensure that mocha exits when test have completed
  process.argv.push('--exit');

  // check that we aren't leaking any globals
  process.argv.push('--check-leaks');
  // prevent globals injected from canvas plugins from triggering leak check
  process.argv.push('--globals', '__core-js_shared__,core,_, ');
github elastic / kibana / src / dev / notice / cli.js View on Github external
* specific language governing permissions and limitations
 * under the License.
 */

import { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';

import getopts from 'getopts';
import dedent from 'dedent';
import { ToolingLog, pickLevelFromFlags } from '@kbn/dev-utils';

import { REPO_ROOT } from '../constants';
import { generateNoticeFromSource } from './generate_notice_from_source';

const unknownFlags = [];
const opts = getopts(process.argv.slice(2), {
  boolean: [
    'help',
    'validate',
    'verbose',
    'debug',
  ],
  unknown(flag) {
    unknownFlags.push(flag);
  }
});

const log = new ToolingLog({
  level: pickLevelFromFlags(opts),
  writeTo: process.stdout
});
github areslabs / alita / src / index.ts View on Github external
}

program.usage('alita [--dev] [--analyzer] [--config=]')
program.on('-h', help)
program.on('--help', help)
program
    .version(packagz.version, '-v --version')
    .option('--config', 'specify configuration file')
    .option('--dev', 'development mode')
    .option('--analyzer', 'analyzer bundle size')
    .parse(process.argv)




const options = getopts(process.argv, {
    alias: {
        w: 'watch',
        v: 'version',
        config: 'config',
        dev: 'dev',


        analyzer: 'analyzer'
    },
})

conf.dev = !!options.dev

if (conf.dev) {
    process.env.NODE_ENV = "development"
} else {
github osdevisnot / klap / src / options.js View on Github external
klap = {},
    source = 'src/index.js',
    browserlist = '>1%, not ie 11, not op_mini all',
  } = pkg;
  const {
    name = pkg.name,
    port = 1234,
    sourcemap = true,
    minify = true,
    target = 'es',
    globals = {},
    namedExports = {},
    fallback = 'public/index.html',
    example = 'public/index.js',
  } = klap;
  const opts = getopts(process.argv.slice(2), {
    boolean: ['sourcemap', 'minify'],
    alias: {
      name: 'n',
      port: 'p',
      source: 's',
      target: 't',
      fallback: 'f',
      example: 'e',
      browserlist: 'b',
    },
    default: {
      name: safePackageName(name),
      source,
      port,
      target,
      sourcemap,
github elastic / kibana / src / dev / run_check_core_api_changes.ts View on Github external
(async () => {
  const log = new ToolingLog({
    level: 'info',
    writeTo: process.stdout,
  });

  const extraFlags: string[] = [];
  const opts = (getopts(process.argv.slice(2), {
    boolean: ['accept', 'docs', 'help'],
    default: {
      project: undefined,
    },
    unknown(name) {
      extraFlags.push(name);
      return false;
    },
  }) as any) as Options;

  if (extraFlags.length > 0) {
    for (const flag of extraFlags) {
      log.error(`Unknown flag: ${flag}`);
    }

    opts.help = true;
github elastic / kibana / src / dev / tslint / run_tslint_cli.ts View on Github external
export function runTslintCli(projects?: Project[]) {
  const log = new ToolingLog({
    level: 'info',
    writeTo: process.stdout,
  });

  const opts = getopts(process.argv.slice(2));
  projects = projects || filterProjectsByFlag(opts.project);

  if (!opts.format) {
    process.argv.push('--format', 'stylish');
  }

  const getProjectArgs = (project: Project) => [
    ...process.argv.slice(2),
    '--project',
    project.tsConfigPath,
  ];

  execInProjects(log, projects, 'tslint', getProjectArgs);
}
github elastic / kibana / src / dev / typescript / run_type_check_cli.ts View on Github external
export function runTypeCheckCli() {
  const extraFlags: string[] = [];
  const opts = getopts(process.argv.slice(2), {
    boolean: ['skip-lib-check', 'help'],
    default: {
      project: undefined,
    },
    unknown(name) {
      extraFlags.push(name);
      return false;
    },
  });

  const log = new ToolingLog({
    level: 'info',
    writeTo: process.stdout,
  });

  if (extraFlags.length) {
github elastic / kibana / packages / kbn-dev-utils / src / run / flags.ts View on Github external
export function getFlags(argv: string[], options: Options): Flags {
  const unexpected: string[] = [];
  const flagOpts = options.flags || {};

  const { verbose, quiet, silent, debug, help, _, ...others } = getopts(argv, {
    string: flagOpts.string,
    boolean: [...(flagOpts.boolean || []), 'verbose', 'quiet', 'silent', 'debug', 'help'],
    alias: {
      ...(flagOpts.alias || {}),
      v: 'verbose',
    },
    default: flagOpts.default,
    unknown: (name: string) => {
      unexpected.push(name);

      if (options.flags && options.flags.allowUnexpected) {
        return true;
      }

      return false;
    },

getopts

Parse CLI arguments.

MIT
Latest version published 4 years ago

Package Health Score

67 / 100
Full package analysis

Popular getopts functions