What is @oclif/parser?
@oclif/parser is a command-line argument parser designed for use with the Oclif framework. It helps in parsing command-line arguments, flags, and options in a structured and efficient manner.
What are @oclif/parser's main functionalities?
Parsing Arguments
This feature allows you to parse positional arguments from the command line. The code sample demonstrates how to define and parse two positional arguments.
const { parse } = require('@oclif/parser');
const argv = ['arg1', 'arg2'];
const options = { args: [{ name: 'firstArg' }, { name: 'secondArg' }] };
const result = parse(argv, options);
console.log(result.args); // { firstArg: 'arg1', secondArg: 'arg2' }
Parsing Flags
This feature allows you to parse flags from the command line. The code sample demonstrates how to define and parse two string flags.
const { parse } = require('@oclif/parser');
const argv = ['--flag1', 'value1', '--flag2', 'value2'];
const options = { flags: { flag1: { type: 'string' }, flag2: { type: 'string' } } };
const result = parse(argv, options);
console.log(result.flags); // { flag1: 'value1', flag2: 'value2' }
Parsing Boolean Flags
This feature allows you to parse boolean flags from the command line. The code sample demonstrates how to define and parse two boolean flags.
const { parse } = require('@oclif/parser');
const argv = ['--flag1', '--no-flag2'];
const options = { flags: { flag1: { type: 'boolean' }, flag2: { type: 'boolean' } } };
const result = parse(argv, options);
console.log(result.flags); // { flag1: true, flag2: false }
Parsing Options with Default Values
This feature allows you to define default values for flags. The code sample demonstrates how to set and retrieve a default value for a flag.
const { parse } = require('@oclif/parser');
const argv = [];
const options = { flags: { flag1: { type: 'string', default: 'defaultValue' } } };
const result = parse(argv, options);
console.log(result.flags); // { flag1: 'defaultValue' }
Other packages similar to @oclif/parser
yargs
Yargs is a popular command-line argument parser that provides a rich set of features for parsing arguments, flags, and options. It offers a more extensive API compared to @oclif/parser and is widely used in the Node.js community.
commander
Commander is another widely-used command-line argument parser that provides a simple and intuitive API for defining commands, arguments, and options. It is known for its ease of use and flexibility, making it a good alternative to @oclif/parser.
minimist
Minimist is a lightweight command-line argument parser that focuses on simplicity and minimalism. It is suitable for projects that require basic argument parsing without the overhead of more feature-rich libraries like @oclif/parser.
@oclif/parser
This library has been replaced by @oclif/core and is now in maintenance mode. We will only consider PRs that address security concerns.
arg and flag parser for oclif
CLI flag parser.
Usage:
const CLI = require('cli-flags')
const {flags, args} = CLI.parse({
flags: {
'output-file': CLI.flags.string({char: 'o'}),
force: CLI.flags.boolean({char: 'f'})
},
args: [
{name: 'input', required: true}
]
})
if (flags.force) {
console.log('--force was set')
}
if (flags['output-file']) {
console.log(`output file is: ${flags['output-file']}`)
}
console.log(`input arg: ${args.input}`)