English 中文(简体)
指挥官JS - 失踪的财富/选择
原标题:CommanderJS - Prompt for missing args/options

我试图让失踪的用户参与进来,需要一种动力/强制性选择。

我认为,我可以迅速使用一个前线,即修改<代码>程序.argv,然后允许指挥官将经过更新的遗体划入。

然而,在我ook的背心之后,我会发现这一错误:<代码>ror:要求选择——没有具体规定的<<>/code>。

看来,在老进程 h后,教 par继续。

For example my-cli subcommand --foo bar will work fine. my-cli subcommand will prompt for foo and push it to process.argv inside the hook but then exit with the error above.

Any ideas? I tried to suppress this error with exitOverride and re-calling parse afterwards, but my callback is not being reached. Surrounding in try-catch doesn t work either.

Here s my code:

program
        .name( cli )
        .description( foo )
        .version(version,  -v, --version )
        .addCommand(sub)
        .hook( preSubcommand , async (program, subcommand) => {
            await promptForMissingArgs(program, subcommand);
            console.log( here );
            console.log(process.argv);
            program.parse(process.argv); // called again with correct process.argv but still exits
        })
        .exitOverride((exitCode) => {
            console.log(`Exiting with code ${exitCode.exitCode}`); // not reached
            //  if (exitCode.exitCode !== 0) {
            //     program.parse(process.argv);
            //  }
        });
    // parse command line arguments and execute command
    try {
        program.parse(process.argv);
    } catch (error) {
        console.log( here ); // doesn t reach here
        console.error(error);
    }

From the docs I see this: if the first arg is a subcommand call preSubcommand hooks pass remaining arguments to subcommand, and process same way https://github.com/tj/commander.js/blob/HEAD/docs/parsing-and-hooks.md

这是否是因为我的 h是同yn?

最佳回答

我不是试图改变程序。 我无法找到改变论点价值的办法,因此我可以选择。

@shadowspawn 回答也有效,但我有多个次子,因此我选择在共享的<条码>前分门和<条码>上继续发言。 h ook而不是preAction。 这也让我保留强制性的备选办法,因为它们在目前阶段尚未在平息寿命周期中加以检查。

import { Command } from  commander ;

const program = new Command();
const subcommand= new Command();

(async () => {
subcommand
  .option( -p, --port <number> )
  .action(async (options) => {
    console.log(options);
  });

program.addCommand(subcommand)
  .hook( preSubcommand , async (thisCommand, sub) => {
    await promptForMissingArgs(thisCommand, sub);
  })

await program.parseAsync();
})();

采用<代码>promptForMissingArgs执行,例如:

async function promptForMissingArgs(command, subcommand) {
  const answers = await inquirer.prompt(getMissingFromArgv(subcommand.opts()));
  Object.keys(answers).forEach(ans => subcommand.setOptionValue(ans, answers[ans]));
}

function getMissingFromArgv(opts) {
  // compare process.argv against mandatory opts() to see what is missing
  // return list of prompts for inquirer
}
问题回答

指挥官不支持 par平时的争辩。 此外,在检查强制性选择之前,还很难在最后一刻进行处理。

What you can easily do is prompt for missing options without marking them as mandatory as such. A simple place to prompt is at the start of the action handler, but a hook may allow code reuse or separate your logic better. Here is an example with a hook in the spirit of your code.

import { Command } from  commander ;

const program = new Command();

program
  .option( -p, --port <number> )
  .hook( preAction , async (thisCommand) => {
    const options = thisCommand.opts();
    // Simulate prompting for and setting missing options.
    if (!options.port) {
      console.log( You forgot to specify a port number. );
      thisCommand.setOptionValue( port ,  3000 );
    }
  })
  .action(async (options) => {
    console.log(options);
  });

program.parseAsync();
% node index.mjs -p 80
{ port:  80  }
% node index.mjs      
You forgot to specify a port number.

(争端:我是指挥官的支持者)





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签