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(repl): add --eval flag for evaluating code when the repl starts #11590

Merged
merged 2 commits into from
Aug 6, 2021

Conversation

dsherret
Copy link
Member

@dsherret dsherret commented Aug 5, 2021

Closes #3173

At the moment, this only adds support for an --eval <code> flag to provide some code to execute on startup.

For example:

deno repl --eval 'import * as hash from "https://deno.land/std@0.77.0/hash#stdhash"'
  1. On success, the result of running the script will not be outputted, but still assigned to _.
  2. On thrown error, the error will be displayed to the user and assigned to _error (unless it's a parse error). The REPL will not fail to start in order to not block the user allowing them to ignore any setup errors and just use the REPL.

We will consider adding support for URL and file paths in the future, but for now this is a first step.

cli/flags.rs Show resolved Hide resolved
.long("eval")
.help("Evaluates the provided code when the REPL starts.")
.takes_value(true)
.value_name("code"),
Copy link
Member Author

Choose a reason for hiding this comment

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

I've noticed some parts of the code require an equals sign. Should this? deno repl --eval="console.log(5);"? I'm not sure when to use the equals sign and when not to.

Copy link
Member

Choose a reason for hiding this comment

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

I think it shouldn't require equals. The equals sign is required in some places to disambiguate. I don't remember the exact situation off hand, but we should attempt to remove the equals-sign-requirement if the ambiguity has evaporated.

if let EvaluationOutput::Error(error_text) = output {
println!("error in --eval flag. {}", error_text);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

It was super easy to implement this thanks to the recent refactorings in this code 😀

@dsherret dsherret changed the title feat: add repl --eval flag for evaluating code when the repl starts feat(repl): add --eval flag for evaluating code when the repl starts Aug 5, 2021
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM too

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.

Support for deno eval interactive option
3 participants