Skip to content
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

Feature: Ability to parse arguments without execution #647

Open
andrewthauer opened this issue Jul 19, 2023 · 3 comments · May be fixed by #707
Open

Feature: Ability to parse arguments without execution #647

andrewthauer opened this issue Jul 19, 2023 · 3 comments · May be fixed by #707

Comments

@andrewthauer
Copy link
Contributor

andrewthauer commented Jul 19, 2023

I think it would be useful if it were possible to parse the command line arguments without also executing the command actions. This would allow you add some logic in between parsing and execution.

A simple use case would be that you want to run a periodic check on startup, but only run for certain commands. For example you run a doctor command every day, but say you have a shell hook command (e.g. eval "$(my-cli hook zsh)") which you always want to be fast and never run the doctor checking logic when the hook command is run. The same could be said for auto update checking, etc.

NOTE: I've been able to hack this by adding custom parsing logic before calling rootCommand.parse. However, this is not ideal as my custom parsing logic doesn't have all the same logic as the cliffy parse command.

@andrewthauer andrewthauer changed the title feature request: ability to parse arguments without execution Feature: Ability to parse arguments without execution Jul 19, 2023
@andrewthauer
Copy link
Contributor Author

@c4spar - Curious if you have any thoughts on this?

@c4spar
Copy link
Owner

c4spar commented Sep 7, 2023

Sry for late reply!

Yes, i already had this in mind.
My plan was to expose the execute method and add an execute option to the parse method to skip the command execution and only return the parse result. Something like this:

const { options, args, cmd } = await cmd.parse(Deno.args, { execute: false });

if (cmd !== zshHookCommand) {
  await doctor();
}

await cmd.execute(options, args);

@andrewthauer
Copy link
Contributor Author

👍 Yes, this seems like a reasonable approach that is backwards compatible as well.

@andrewthauer andrewthauer linked a pull request May 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants