The mighty option parser used by yargs for parsing command-line arguments with extensive configuration options.
The main parsing function that converts command-line arguments into structured JavaScript objects. This is the primary interface for yargs-parser.
import parser from "yargs-parser";function parser(args: ArgsInput, opts?: Partial<Options>): Arguments;
type ArgsInput = string | any[];
interface Arguments {
/** Non-option arguments */
_: (string | number)[];
/** Arguments after the end-of-options flag `--` */
'--'?: (string | number)[];
/** All remaining options */
[argName: string]: any;
}import parser from "yargs-parser";
// Parse process arguments
const argv = parser(process.argv.slice(2));
// Parse custom array
const result = parser(['--foo', 'bar', '--number', '42', 'positional']);
console.log(result);
// Output: { _: ['positional'], foo: 'bar', number: 42 }// Parse argument string
const result = parser("--foo=99 --bar hello -x 33");
console.log(result);
// Output: { _: [], foo: 99, bar: 'hello', x: 33 }
// Parse complex string with positional args
const complex = parser("command --verbose --file input.txt output.txt");
console.log(complex);
// Output: { _: ['command', 'output.txt'], verbose: true, file: 'input.txt' }// Short and long boolean flags
const booleans = parser(['-v', '--debug', '--no-color']);
console.log(booleans);
// Output: { _: [], v: true, debug: true, color: false }// Automatic type inference
const mixed = parser(['--name', 'Alice', '--port', '3000', '--ratio', '0.75']);
console.log(mixed);
// Output: { _: [], name: 'Alice', port: 3000, ratio: 0.75 }// Multiple values create arrays
const arrays = parser(['--file', 'a.txt', '--file', 'b.txt', '--tag', 'dev', '--tag', 'test']);
console.log(arrays);
// Output: { _: [], file: ['a.txt', 'b.txt'], tag: ['dev', 'test'] }// Combined short options
const grouped = parser(['-abc', 'value']);
console.log(grouped);
// Output: { _: [], a: true, b: true, c: 'value' }// Hyphenated options become camelCase
const camelCase = parser(['--foo-bar', 'value', '--multi-word-option']);
console.log(camelCase);
// Output: { _: [], 'foo-bar': 'value', fooBar: 'value', 'multi-word-option': true, multiWordOption: true }// Nested object creation
const nested = parser(['--db.host', 'localhost', '--db.port', '5432', '--server.name', 'api']);
console.log(nested);
// Output: { _: [], db: { host: 'localhost', port: 5432 }, server: { name: 'api' } }// Arguments after -- are treated as positional
const endOfOptions = parser(['--verbose', '--', '--not-an-option', 'file.txt']);
console.log(endOfOptions);
// Output: { _: [], verbose: true, '--': ['--not-an-option', 'file.txt'] }// Explicit type specification
const typed = parser(['--count', '5', '--name', 'test', '--debug'], {
number: ['count'],
string: ['name'],
boolean: ['debug']
});
console.log(typed);
// Output: { _: [], count: 5, name: 'test', debug: true }// Define option aliases
const withAliases = parser(['-n', 'Alice', '-v'], {
alias: {
name: ['n'],
verbose: ['v']
}
});
console.log(withAliases);
// Output: { _: [], n: 'Alice', name: 'Alice', v: true, verbose: true }// Provide default values
const withDefaults = parser(['--name', 'Bob'], {
default: {
port: 3000,
debug: false,
name: 'Anonymous'
}
});
console.log(withDefaults);
// Output: { _: [], name: 'Bob', port: 3000, debug: false }The main parser function does not throw errors for invalid input but may return unexpected results. For error handling, use the detailed parsing mode.
// Basic parsing continues with invalid input
const result = parser(['--invalid-number', 'not-a-number']);
console.log(result);
// Output: { _: [], 'invalid-number': 'not-a-number' }Install with Tessl CLI
npx tessl i tessl/npm-yargs-parser