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

feat(command): parse without execute #707

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andrewthauer
Copy link
Contributor

closes #647

@c4spar - This is a very early draft at implementing your suggested approach to allow the parse method to be called without executing. I've got this mostly working with minimal changes. However there are a couple of initial things I'm not sure how to proceed with and would love your guidance/suggestions:

  1. The options action handlers (e.g. --help) are not executed inside parseCommand and not by the execute function. The ParsingContext is the only thing that tracks these actions at the moment, and I suspect we don't want to expose that. We likely this needs to be unified into the execute command somehow?
  2. To avoid messing with types too much I'm re-using the literal field in place of args which satisfies the return type shape. However, the current name literal isn't the most intuitive either imo. Maybe it makes sense to rename literal to args if possible?

I've added an example which demonstrates the current usage. It seems to mostly work except for when passing --help. I've also added some inline comments on areas I think need work.

if (ctx.execute) {
return await this.execute(ctx.env, ctx.unknown);
} else {
// TODO: Resolve typing for options
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to satisfy options here since it's normally parsed later with more context.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work:

return { cmd: this, options, literal: [], args: ctx.unknown };

@@ -1750,6 +1761,9 @@ export class Command<
const args = this.parseArguments(ctx, options);
this.literalArgs = ctx.literal;

//
// TODO: How can we defer executing this when ctx.execute is false?
//
// Execute option action.
if (ctx.actions.length) {
Copy link
Contributor Author

@andrewthauer andrewthauer May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the problematic part that is executed only in the parse flow. I haven't given it much thought, but this probably needs to be deferred somehow and/or unified with the execute method so it's consistently handled.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should somehow be moved to the execute method.

I was looking into this some time ago and noticed this as well. I have indeed thought about exposing the context, because I have also thought about customizing the ActionHandler to have a context as well as an argument instead of the options and args arguments. But since this would be a major change, i have planned to look into this again for cliffy v2.

But maybe we find a nother way to solve this issue for v1.

} catch (error: unknown) {
this.handleError(error);
if (ctx.execute) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about this, but I don't think we want the help execution/etc. run and displayed on the command line when the user has provided execute: false.

Copy link
Owner

@c4spar c4spar Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think throwing an error is fine, but we need to call the handleError method and maybe add a throw option to it to throw the error.

import { CompletionsCommand } from '../../command/completions/completions_command.ts';
import { HelpCommand } from '../../command/help/help_command.ts';

function getCommandFullname(command: Command<any> | undefined): string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to avoid using Command<any> for this sort of thing. I've had this problem in my own code using cliffy as well.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no currently not. i decided to not add any as default value for the first generic param to make it clear that this type is using any, but i would be also fine to add any as default value.
But in generally i'm also not very happy with the generic type params, maybe i will refactor them in cliffy v2.

Copy link
Owner

@c4spar c4spar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx @andrewthauer for looking into this! 👍 I have added a few comments.

if (ctx.execute) {
return await this.execute(ctx.env, ctx.unknown);
} else {
// TODO: Resolve typing for options
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work:

return { cmd: this, options, literal: [], args: ctx.unknown };

@@ -1750,6 +1761,9 @@ export class Command<
const args = this.parseArguments(ctx, options);
this.literalArgs = ctx.literal;

//
// TODO: How can we defer executing this when ctx.execute is false?
//
// Execute option action.
if (ctx.actions.length) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should somehow be moved to the execute method.

I was looking into this some time ago and noticed this as well. I have indeed thought about exposing the context, because I have also thought about customizing the ActionHandler to have a context as well as an argument instead of the options and args arguments. But since this would be a major change, i have planned to look into this again for cliffy v2.

But maybe we find a nother way to solve this issue for v1.

} catch (error: unknown) {
this.handleError(error);
if (ctx.execute) {
Copy link
Owner

@c4spar c4spar Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think throwing an error is fine, but we need to call the handleError method and maybe add a throw option to it to throw the error.

import { CompletionsCommand } from '../../command/completions/completions_command.ts';
import { HelpCommand } from '../../command/help/help_command.ts';

function getCommandFullname(command: Command<any> | undefined): string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no currently not. i decided to not add any as default value for the first generic param to make it clear that this type is using any, but i would be also fine to add any as default value.
But in generally i'm also not very happy with the generic type params, maybe i will refactor them in cliffy v2.

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 this pull request may close these issues.

Feature: Ability to parse arguments without execution
2 participants