本文整理匯總了TypeScript中yargs-parser類的典型用法代碼示例。如果您正苦於以下問題:TypeScript yargs-parser類的具體用法?TypeScript yargs-parser怎麽用?TypeScript yargs-parser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了yargs-parser類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: runHelp
export async function runCommand(commandMap: CommandMap,
args: string[],
logger: logging.Logger,
context: CommandContext): Promise<any> {
// if not args supplied, just run the help command.
if (!args || args.length === 0) {
args = ['help'];
}
const rawOptions = yargsParser(args, { alias: { help: ['h'] }, boolean: [ 'help' ] });
let commandName = rawOptions._[0];
// remove the command name
rawOptions._ = rawOptions._.slice(1);
const executionScope = context.project.isEmberCLIProject()
? CommandScope.inProject
: CommandScope.outsideProject;
let Cmd: CommandConstructor;
Cmd = findCommand(commandMap, commandName);
if (!Cmd && !commandName && (rawOptions.v || rawOptions.version)) {
commandName = 'version';
Cmd = findCommand(commandMap, commandName);
}
if (!Cmd && rawOptions.help) {
commandName = 'help';
Cmd = findCommand(commandMap, commandName);
}
if (!Cmd) {
logger.error(oneLine`The specified command (${commandName}) is invalid.
For a list of available options, run \`ng help\`.`);
throw '';
}
const command = new Cmd(context, logger);
args = await command.initializeRaw(args);
let options = parseOptions(args, command.options, command.arguments, command.argStrategy);
await command.initialize(options);
options = parseOptions(args, command.options, command.arguments, command.argStrategy);
if (commandName === 'help') {
options.commandMap = commandMap;
}
if (options.help) {
return await runHelp(command, options);
} else {
verifyCommandInScope(command, executionScope);
delete options.h;
delete options.help;
return await validateAndRunCommand(command, options);
}
}
示例2: command
export async function runCommand(commandMap: CommandMap,
args: string[],
logger: logging.Logger,
context: CommandContext): Promise<number | void> {
// if not args supplied, just run the help command.
if (!args || args.length === 0) {
args = ['help'];
}
const rawOptions = yargsParser(args, { alias: { help: ['h'] }, boolean: [ 'help' ] });
let commandName = rawOptions._[0];
// remove the command name
rawOptions._ = rawOptions._.slice(1);
const executionScope = insideProject()
? CommandScope.inProject
: CommandScope.outsideProject;
let Cmd: CommandConstructor | null;
Cmd = findCommand(commandMap, commandName);
if (!Cmd && !commandName && (rawOptions.v || rawOptions.version)) {
commandName = 'version';
Cmd = findCommand(commandMap, commandName);
}
if (!Cmd && rawOptions.help) {
commandName = 'help';
Cmd = findCommand(commandMap, commandName);
}
if (!Cmd) {
const commandsDistance = {} as { [name: string]: number };
const allCommands = listAllCommandNames(commandMap).sort((a, b) => {
if (!(a in commandsDistance)) {
commandsDistance[a] = levenshtein(a, commandName);
}
if (!(b in commandsDistance)) {
commandsDistance[b] = levenshtein(b, commandName);
}
return commandsDistance[a] - commandsDistance[b];
});
logger.error(tags.stripIndent`
The specified command ("${commandName}") is invalid. For a list of available options,
run "ng help".
Did you mean "${allCommands[0]}"?
`);
return 1;
}
const command = new Cmd(context, logger);
args = await command.initializeRaw(args);
let options = parseOptions(args, command.options, command.arguments, command.argStrategy);
await command.initialize(options);
options = parseOptions(args, command.options, command.arguments, command.argStrategy);
if (commandName === 'help') {
options.commandMap = commandMap;
}
if (options.help) {
command.printHelp(options);
return;
} else {
if (Cmd.scope !== undefined && Cmd.scope !== CommandScope.everywhere) {
if (Cmd.scope !== executionScope) {
let errorMessage;
if (Cmd.scope === CommandScope.inProject) {
errorMessage = `This command can only be run inside of a CLI project.`;
} else {
errorMessage = `This command can not be run inside of a CLI project.`;
}
logger.fatal(errorMessage);
return 1;
}
if (Cmd.scope === CommandScope.inProject) {
if (!context.project.configFile) {
logger.fatal('Invalid project: missing workspace file.');
return 1;
}
if (['.angular-cli.json', 'angular-cli.json'].includes(context.project.configFile)) {
// --------------------------------------------------------------------------------
// If changing this message, please update the same message in
// `packages/@angular/cli/bin/ng-update-message.js`
const message = tags.stripIndent`
The Angular CLI configuration format has been changed, and your existing configuration
can be updated automatically by running the following command:
ng update @angular/cli
`;
//.........這裏部分代碼省略.........
示例3: levenshtein
export async function runCommand(commandMap: CommandMap,
args: string[],
logger: logging.Logger,
context: CommandContext): Promise<number | void> {
// if not args supplied, just run the help command.
if (!args || args.length === 0) {
args = ['help'];
}
const rawOptions = yargsParser(args, { alias: { help: ['h'] }, boolean: [ 'help' ] });
let commandName = rawOptions._[0];
// remove the command name
rawOptions._ = rawOptions._.slice(1);
const executionScope = context.project.isEmberCLIProject()
? CommandScope.inProject
: CommandScope.outsideProject;
let Cmd: CommandConstructor;
Cmd = findCommand(commandMap, commandName);
if (!Cmd && !commandName && (rawOptions.v || rawOptions.version)) {
commandName = 'version';
Cmd = findCommand(commandMap, commandName);
}
if (!Cmd && rawOptions.help) {
commandName = 'help';
Cmd = findCommand(commandMap, commandName);
}
if (!Cmd) {
function levenshtein(a: string, b: string): number {
if (a.length === 0) {
return b.length;
}
if (b.length === 0) {
return a.length;
}
if (a.length > b.length) {
let tmp = a;
a = b;
b = tmp;
}
const row = Array(a.length);
for (let i = 0; i <= a.length; i++) {
row[i] = i;
}
let result: number;
for (let i = 1; i <= b.length; i++) {
result = i;
for (let j = 1; j <= a.length; j++) {
let tmp = row[j - 1];
row[j - 1] = result;
result = b[i - 1] === a[j - 1]
? tmp
: Math.min(tmp + 1, Math.min(result + 1, row[j] + 1));
}
}
return result;
}
const commandsDistance = {} as { [name: string]: number };
const allCommands = listAllCommandNames(commandMap).sort((a, b) => {
if (!(a in commandsDistance)) {
commandsDistance[a] = levenshtein(a, commandName);
}
if (!(b in commandsDistance)) {
commandsDistance[b] = levenshtein(b, commandName);
}
return commandsDistance[a] - commandsDistance[b];
});
logger.error(tags.stripIndent`
The specified command ("${commandName}") is invalid. For a list of available options,
run "ng help".
Did you mean "${allCommands[0]}"?
`);
return 1;
}
const command = new Cmd(context, logger);
args = await command.initializeRaw(args);
let options = parseOptions(args, command.options, command.arguments, command.argStrategy);
await command.initialize(options);
options = parseOptions(args, command.options, command.arguments, command.argStrategy);
if (commandName === 'help') {
options.commandMap = commandMap;
}
if (options.help) {
return await runHelp(command, options);
//.........這裏部分代碼省略.........