8000 Invalid command when using commandDir · Issue #1306 · yargs/yargs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Invalid command when using commandDir #1306

8000
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
smitp33 opened this issue Feb 25, 2019 · 6 comments
Open

Invalid command when using commandDir #1306

smitp33 opened this issue Feb 25, 2019 · 6 comments
Labels

Comments

@smitp33
Copy link
smitp33 commented Feb 25, 2019

If when using the commandDir() to load subcommands from a directory, the user specifies a command that is not valid i.e. is not in the directory, I am wondering what the best way to inform the user is that an invalid command has been specificed. Currently itit appears that nothing happens and the command simply executes nothing.

Perhaps using the check() with a function that parses the commands available and then checks against the one specified, before then issuing the showHelp()?

After reading the API docs I did try the strict() option, but this only appears to work on the top level command. The sub commands do not seem to honor it, even though I am also explicitly setting it in the builder at this level?

@visla
Copy link
visla commented Mar 22, 2019

I've had the same problem. this is not either documented anywhere or maybe not even possible. Any suggestions?

@znagadeon
Copy link

In my case, I'm using check like this(node@8.11.3, yargs@14.2.0)

module.exports = {
    // ...
    builder(yargs) {
        yargs.commandDir('my-subcommand')
            .check(argv => {
                if (argv.command) throw new Error('Command is invalid!!');
                return true;
            });
    }
    // ...
}

If yargs doesn't supply proper solution, this can be sort of.

@mleguen
Copy link
Member
mleguen commented Nov 13, 2019

Did you try using demandCommand(1) to force the user to specify a command?

Or strict() to forbid the use of unknown commands?

@znagadeon
Copy link
znagadeon commented Nov 14, 2019

@mleguen
I've tried using .strict().demandCommand(1). It works! 🎉
However, it seems that yargs parses them in a different way.

sh~~ ~~# node index.js unknown-command~~ ~~Unknown argumment: unknown-command~~ ~~

sh~~ ~~# node index.js my-command unknown-subcommand~~ ~~Not enough non-option arguments: got 0, need at least 1~~ ~~

I figured out why it doesn't show the same error message. They have different command types: yargs.command('command <cmd>').commandDir() and yargs.command('my-command').commandDir(). Thanks!

@bcoe bcoe added the question label Nov 15, 2019
@bcoe
Copy link
Member
bcoe commented Nov 15, 2019

@smitp33 can you verify whether your issue is solved with @mleguen's recommendations as well?

@Xunnamius
Copy link

I couldn't get this to work for deeply nested subcommands even after trying the recommended workarounds and linked issues. I worked around this and a few other issues by using a wrapper function to differentiate leaf commands from commands with registered subcommands when outputting help text and attempting to run command handlers.

This issue inspired black flag, a yargs wrapper I built mainly to work around this sort of prickly behavior from command and commandDir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants
0