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

Commandline args --help #2

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

Commandline args --help #2

wants to merge 7 commits into from

Conversation

adi20f
Copy link
Collaborator

@adi20f adi20f commented Oct 20, 2019

Added ability to parse command line args as well as preliminary --help message.

@adi20f adi20f requested a review from nlbrown2 October 20, 2019 18:06
main.cpp Outdated
*/
try {
parse_commandline_args(argc, argv);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Probably should add a way for this to return a boolean or something. If we get a help message or if someone requests some option that doesn't exist we would want to exit without error instead of going through work

main.cpp Outdated
Comment on lines 14 to 28
const string help_header = "\
KTPuild\n\
An application to help making the compiling of large projects easier. The\n\
intended use for EECS 280 and 281 students attending the University of \n\
Michigan. Built and maintained by Kappa Theta Pi Alpha Chapter. For reporting\n\
bugs or requesting features please visit: https://github.com/ktp-dev/KTPuild\n";

const string help_msg = "\n\
Usages:\n\
KTPuild\n\
KTPuild -h | --help\n\
\n\
Options:\n\
-h | --help: display help information\n\n";

Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: use raw strings if desired to not worry about the trailing \n\ of each line.

Also, please use a consistent const char* const for global static strings unless you have to use std::string functions on the global string (and even then, I'd encourage using std::string_view). The reason is that while const string = "blah" looks innocent, it places the contents of this string into dynamic memory (the heap). This is needless bc there is already a copy of it in the static section!

Comment on lines +112 to +121
bool is_created(const string &filename){

// tests for the existence of the file with success being a 0
if(access(filename.c_str(), F_OK) == 0)
{
return true;
}
return false;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this diff will go away once you merge in pull #1 to this branch

return false;
}

bool parse_commandline_args(int argc, char** &argv)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason we take a char** by reference?

main.cpp Outdated

int option, optionIndex;

while((option = getopt_long(argc, argv, "h", longOpts, &optionIndex))!= -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the point of optionIndex? Does it have to be a non-null pointer, or can it be nullptr?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's meant to be there to cycle through all the options listened in longOpts. It is incremented every time getopt_long is called so we can just keep going till it hits the final null struct and returns back a -1

//now we know what cpp files have changed
if (changed.empty()) //TODO: also ensure exe exists
// if there is nothing to recompile and the executable has been created we are done
if (changed.empty() && is_created(buildfile.get_executable_name()))
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto about merging pull #1

main.cpp Outdated

using namespace std;
const int MAX_PROCESSES = 4;
const char* const COMPILER = "g++";

const string help_header = "\
KTPuild\n\
An application to help making the compiling of large projects easier. The\n\
Copy link
Contributor

Choose a reason for hiding this comment

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

Some english critiques that you can feel free to ignore:

"application that makes the compiling" as opposed to "help making."
"Intended for use in EECS 280"

bugs or requesting features please visit: https://github.com/ktp-dev/KTPuild\n";

const string help_msg = "\n\
Usages:\n\
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we should include some more information about what the normal usage will do? Or is that better left for man pages in a later commit?

main.cpp Outdated

default:
{
return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I am a fan of using c++ exceptions to indicate error conditions. This allows the calling convention to not include error statuses and usually simplifies control logic. In this case I don't think it will have a big impact beyond encouraging future consistency. Long story short, are you opposed to throwing a std::runtime_exception here with the message equal to help_msg? This would allow the caller to do a simple if(!parse_command_line(blah)) return; // false return value means do not continue with execution and a universal handling of error cases.

Also, it avoids the subtle but potentially important bug where the return code of the program is 0 even if an invalid option was specified.

main.cpp Outdated
default:
{
return false;
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, you don't need a break after a return statement ;P

main.cpp Outdated
if(parse_commandline_args(argc, argv) == false)
{
cout << help_msg;
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

See above comment about how this is buggy- will return a 0 (OK) status code even when an invalid flag was provided

@nlbrown2
Copy link
Contributor

I know I left what seems like a lot of comments, but I think you did a great job on this! Incorporating getopt_long was smart, and I liked the messages you generated!

Also, please merge master into this branch to resolve those merge conflicts

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.

2 participants