-
Notifications
You must be signed in to change notification settings - Fork 26
Fixes some error handling when unexpected input is given #4
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,24 +39,31 @@ def focusQuestion(questions, count): | |
| userInput = '0' | ||
| #Looping while the user wants to see more input | ||
| while(userInput != 'm'): | ||
| userInput = promptUser("Press m for more, or a number to select: ") | ||
| if(userInput == 'm'): | ||
| break | ||
| if(0 < int(userInput) and int(userInput) <= count): | ||
| print("\n\n\n\n\n\n") | ||
| printFullQuestion(questions[int(userInput)- 1]) | ||
| branchInput = '0' #user deciding whether to branch to open browser, or to return to search | ||
| while(branchInput != 'x'): | ||
| branchInput = promptUser("Press b to launch browser, or x to return to search: ") | ||
| if(branchInput == 'b'): | ||
| webbrowser.open(questions[int(userInput)-1].json['link'], new=0, autoraise=True) | ||
| #User selects x to return to search | ||
| if(branchInput == 'x'): | ||
| print("\n\n\n\n\n\n\n\n\n\n\n\n") | ||
| #Ranging over the 5 questions including the user's choice | ||
| for j in range(5*int((int(userInput)-1)/5), 5*int((int(userInput)-1)/5)+5): | ||
| printQuestion(questions[j], j+1) | ||
| continue #exit the inner while loop | ||
| userInput = promptUser("Enter m for more, a question number to select, or q to exit: ") | ||
| try: | ||
| if(userInput == 'q'): | ||
| sys.exit() | ||
| if(userInput == 'm'): | ||
| break | ||
| if(0 < int(userInput) and int(userInput) <= count): | ||
| print("\n\n\n\n\n\n") | ||
| printFullQuestion(questions[int(userInput)- 1]) | ||
| branchInput = '0' #user deciding whether to branch to open browser, or to return to search | ||
| while(branchInput != 'x'): | ||
| branchInput = promptUser("Enter b to launch browser, or x to return to search: ") | ||
| if(branchInput == 'b'): | ||
| webbrowser.open(questions[int(userInput)-1].json['link'], new=0, autoraise=True) | ||
| #User selects x to return to search | ||
| if(branchInput == 'x'): | ||
| print("\n\n\n\n\n\n\n\n\n\n\n\n") | ||
| #Ranging over the 5 questions including the user's choice | ||
| for j in range(5*int((int(userInput)-1)/5), 5*int((int(userInput)-1)/5)+5): | ||
| printQuestion(questions[j], j+1) | ||
| continue #exit the inner while loop | ||
| except: | ||
| if (userInput != 'q'): | ||
| print("The input entered was not recognized as a valid choice. Exiting...") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though there's a possibility that this try/except structure could throw errors besides having an invalid character, that seems unlikely. Why is this "invalid input" warning not printed every time an error is thrown in this block?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is printed everytime, only not printed when the choice
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha––that makes sense. I wasn't thinking of |
||
| sys.exit() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would rather this point returned to the initial prompt––"Enter m for more, a question number to select, or q to exit: "––instead of sys.exit()-ing. Allows for user mistakes. There is a manual sys.exit() option in the input 'q' now, as well as control-C. tl;dr: would rather not quit without explicit user request.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So would you rather it be
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd rather it was except:
if (userInput != 'q'):
print("The input entered was not recognizes as a valid choice.")
continue
else: # If the user input is 'q', no warning is necessary
sys.exit()
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, added in latest commit, works for both |
||
|
|
||
| def searchTerm(term, tags): | ||
| print('Searching for: %s... \n' % term,) | ||
|
|
@@ -124,7 +131,7 @@ def printFullQuestion(question): | |
| answertext = h.handle(answerdiv.find('div', attrs={'class': 'post-text'}).prettify()) | ||
| for cell in soup.find_all('td', attrs={'class': 'postcell'}): | ||
| questiontext = h.handle(cell.find('div', attrs={'class': 'post-text'}).prettify()) | ||
| print(pColor.BLUE + "-------------------------QUESTION------------------------\n" + question.title + "\n" + questiontext | ||
| print(pColor.BLUE + "-------------------------QUESTION------------------------\n" + question.title + "\n" + questiontext | ||
| + pColor.END + "\n\n-------------------------------ANSWER------------------------------------\n" + answertext) | ||
|
|
||
| def searchVerbose(term): | ||
|
|
@@ -133,11 +140,11 @@ def searchVerbose(term): | |
| questionurl = question.json['link'] | ||
| answerid = question.json['accepted_answer_id'] | ||
| printFullQuestion(question) | ||
|
|
||
|
|
||
| def getParser(): | ||
| parser = argparse.ArgumentParser(description="Parses command-line arguments for StackIt") | ||
| parser.add_argument("-s", "--search", metavar="QUERY", help="Searches StackOverflow for your query") | ||
| parser.add_argument("-s", "--search", metavar="QUERY", help="Searches StackOverflow for your query") | ||
| parser.add_argument("-e", "--stderr", metavar="EXECUTE", help="Runs an executable command (i.e. python script.py) and automatically inputs error message to StackOverflow") | ||
| parser.add_argument("-t", "--tag", metavar="TAG1 TAG2", help="Searches StackOverflow for your tags") | ||
| parser.add_argument("--verbose", help="displays full text of most relevant question and answer", action="store_true") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
userInput == 'q'case is handled above, in line 44––is there a reason for this redundancy?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you throw the exception after the check, it automatically propagates to the
except:block. So when letting the user know what happened, you need to check if the exception occured because ofq, if it didn't then let the user know why. If it did then just exit as normal.