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

Add clap for parsing command line arguments #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Add clap for parsing command line arguments #52

wants to merge 4 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Jul 12, 2017

Fixes #41

@ghost
Copy link
Author

ghost commented Jul 12, 2017

@polybuildr Can you suggest something to fix the CI error?
I don't know rust well enough 😕

@polybuildr
Copy link
Owner

Hello! Thanks a lot for the PR, and sorry for the delay in review.

Just about the CI errors:

  • There's one error regarding compiling rustfmt, that's my fault, I'll fix it.
  • Rustfmt is a formatter for Rust code. Look at its github repository (install rustfmt-nightly) and run it on the code to format it. If there's no version issues, that should fix the lint error.

Copy link
Contributor

@bollu bollu left a comment

Choose a reason for hiding this comment

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

Thanks for the patch :) I just have some minor changes to request.

src/main.rs Outdated
let mut app = App::new("balloon")
.version("0.1.0")
.arg(Arg::with_name("file")
.help("run the file")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a little confused, what is the difference between this and run? They have the same .help :)

Copy link
Author

Choose a reason for hiding this comment

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

I put file to deal with balloon program.bl and run for balloon --run program.bl
They're the same because run is the default mode.
If we use something like --mode, then I can put run as the default value if the user doesn't supply one, but I can't think of another alternative for the current scenario.
What do you suggest?

Copy link
Owner

Choose a reason for hiding this comment

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

I think the easy way out for now is to simply leave a comment explaning why both are the same (just mention "default mode" or something). Eventually we'll stabilise the command line API and hopefully then, I'll have a sensible API that does not require duplication. :P

src/main.rs Outdated
};
}
else {
run_file(m.value_of("file").unwrap(), AstWalkInterpreter::new());
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid unwrap please, we can handle the error a little more gracefully with expect.

Copy link
Author

Choose a reason for hiding this comment

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

Is there a possibility of a panic at this line?

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps not right now, but someone could unintentionally change the code later :)

Copy link
Owner

Choose a reason for hiding this comment

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

If I'm reading this correctly, and the unwrap is for the .value_of, then simply switching to the if-let pattern described elsewhere will remove the need for the unwrap, solving this problem.

src/main.rs Outdated
repl::run_repl(LLVMInterpreter::new());
}
else if m.is_present("run") {
run_file(m.value_of("run").unwrap(), AstWalkInterpreter::new())
Copy link
Contributor

Choose a reason for hiding this comment

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

I dislike this pattern of checking with is_present and then calling unwrap, it defeats the entire purpose of having an Option type in the language. We can write this as:

if let Some(f) = m.value_of("run") {
    run_file(f, AstWalkInterpreter::new())
}
else if let Some(f) = m.is_present("check") {
    typecheck_file(f).unwrap());
}
...

Copy link
Author

@ghost ghost Jul 13, 2017

Choose a reason for hiding this comment

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

Right, I was kind of confused about the whole Option thing in the beginning.
I'll change this, thanks.

Copy link
Owner

Choose a reason for hiding this comment

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

Just a note, you can also use match if you feel that is more suited to the task. Sometimes if-let is nicer, sometimes match is. if-let is completely fine here, I'm just leaving options for you.

Copy link
Owner

@polybuildr polybuildr left a comment

Choose a reason for hiding this comment

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

Great work on this PR! :) Once again, thanks for the help.

Left a few comments inline, hopefully helping the discussion. Also, just checking, but you have built and tested this locally, right? I will do that once myself, but I'd prefer that both of us did it. :P

src/main.rs Outdated
let mut app = App::new("balloon")
.version("0.1.0")
.arg(Arg::with_name("file")
.help("run the file")
Copy link
Owner

Choose a reason for hiding this comment

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

I think the easy way out for now is to simply leave a comment explaning why both are the same (just mention "default mode" or something). Eventually we'll stabilise the command line API and hopefully then, I'll have a sensible API that does not require duplication. :P

src/main.rs Outdated
repl::run_repl(LLVMInterpreter::new());
}
else if m.is_present("run") {
run_file(m.value_of("run").unwrap(), AstWalkInterpreter::new())
Copy link
Owner

Choose a reason for hiding this comment

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

Just a note, you can also use match if you feel that is more suited to the task. Sometimes if-let is nicer, sometimes match is. if-let is completely fine here, I'm just leaving options for you.

src/main.rs Outdated
};
}
else {
run_file(m.value_of("file").unwrap(), AstWalkInterpreter::new());
Copy link
Owner

Choose a reason for hiding this comment

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

If I'm reading this correctly, and the unwrap is for the .value_of, then simply switching to the if-let pattern described elsewhere will remove the need for the unwrap, solving this problem.

@ghost
Copy link
Author

ghost commented Jul 16, 2017

I ran it locally and it seems to work as expected.
Also, I probably messed up while rebasing my commits over yours, so it looks weird for now.
I'll squash them at the end.

How do you want to deal with completion?
Clap can generate completion functions for Bash, Fish, Zsh and PowerShell which then have to be sourced accordingly.

@polybuildr
Copy link
Owner

I'll look at the code again and run it myself once. Will also try to resolve the CI errors.

I think we should hold off

@polybuildr polybuildr closed this Jul 16, 2017
@polybuildr polybuildr reopened this Jul 16, 2017
@polybuildr
Copy link
Owner

Sorry, bad click!

... hold off on autocomplete for now, because the API isn't stable. Clap being able to generate it is pretty awesome! Since you've so kindly implemented the usage of Clap, we should be able to do this easily later. :)

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.

3 participants