Reputation: 844
I created a method which splits a sentence into words and returns the first word of the sentence (you could use NLTK tokenizer
or argparse
for this, but as this is a class project meant for learning Python, I created a simple tokenizer from scratch) The method also has a useful 'help' argument, in which passing -h
or --help
will bring up help text. However I want the function to output the help text then 'break' or 'pass' if the user passes -h or --help. Here's my method:
class example_method(object):
def __init__(self):
self.data = []
def parse(self, message):
if(("-h" or "--help") in message):
print("This is a helpful message")
else:
self.data = [x.strip() for x in message.split(' ')]
return self.data
If the user input a regular message, the method works. Let me illustrate:
example = example_method()
input_message = "Hello there how are you?"
print(example.parse(input_message)[0])
The above works well. However, if the user inputs -h or --help, the method returns an error:
example = example_method()
input_message = "--help"
print(example.parse(input_message)[0])
The above will return: TypeError: 'NoneType' object is not subscriptable
I realize that a possible solution is:
try: print(example.parse(input_message)[0])
except: pass
But is there a way to return pass
from within the method like this?
def parse(self, message):
if(("-h" or "--help") in message):
print("This is a helpful message")
return pass
else:
self.data = [x.strip() for x in message.split(' ')]
return self.data
My aim is that I do not want an error message as this method is part of a bigger program and an error will just make the output look ugly. It would look far more professional if the method outputs help text and then exits without an error.
Upvotes: 0
Views: 663
Reputation: 844
Thanks to all your suggestions, I created my own command parsing module to solve this problem. You can find it on GitHub: https://github.com/MonkeyBot2020/comparse
Here are some examples of its usage:
from comparse import comparse
query = comparse(False)
query.add_argument("log_channel", "str", "logs.txt", "Logs the channel in specified file. DEFAULT ARGUMENT(S): log_channel 'logs.txt'")
query.add_argument("content_filter", "str", "None", "Filter log by content. DEFAULT ARGUMENT(S): content_filter 'None'")
query.add_argument("log_attachments", "bool", True, "Logs attachments. DEFAULT ARGUMENT(S): log_attachments 'True'")
query.add_argument("log_author", "bool", False, "Logs author of message. DEFAULT ARGUMENT(S): log_author=False")
query.add_argument("exclude_content", "str", "None", "Exclude content from log. DEFAULT ARGUMENT(S): exclude_content='None'")
#FIRST EXAMPLE
message = "log_channel --h"
file_name = query.parse(message)
try: print(file_name['exclude_content'][0])
except: print(query.parse("--help"))
#SECOND EXAMPLE
message = "log_channel=example.txt, content_filter=posted, log_attachments=True, log_author=True, exclude_content='apple, pear, orange'"
file_name = query.parse(message)
try: print(file_name)
except: print(query.parse("--help"))
#THIRD EXAMPLE
message = "log_channel, exclude_content='apple, pear'"
file_name = query.parse(message)
try: print(file_name['exclude_content'])
except: print(query.parse("--help"))
#FOURTH EXAMPLE
message = "i made a booboo"
file_name = query.parse(message)
try: print(file_name['mistaken_content'][0])
except: print(query.parse("--help"))
Hope this is useful :)
Upvotes: 0
Reputation: 51653
You should overthink your design ... add a
def help(self):
print("Cool description")
method and remove the "help switch parsing" from def parse(self, message)
.
You are crippling the capabilities of parser ... think about parsing this totally contrieved message:
"Paris is a cool town-however you travel it: by foot, by bike or by parachute."
See SRP (Single responsibility principle) - parse
should not print help messages.
You can also use documentation to provide help()
:
class well_documented_example_class(object):
"""Totally well documented class"""
def parse(self, message):
"""This method does coool things with your 'message'
'message' : a string with text in it to be parsed"""
self.data = [x.strip() for x in message.split(' ')]
return self.data
You get help by issuing:
print(help(well_documented_example_class))
to get:
class well_documented_example_class(builtins.object)
| Totally well documented class
|
| Methods defined here:
|
| parse(self, message)
| This method does coool things with your 'message'
|
| 'message' : a string with text in it to be parsed
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
Upvotes: 0
Reputation: 78700
Consider using argparse
in order to automatically generate the -h
and --help
flags and the help text.
Low effort demo:
script.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-p', help='This will be printed')
args = parser.parse_args()
print(args.p)
usage:
$ python3 script.py -p hello
hello
$ python3 script.py -h
usage: script.py [-h] [-p P]
optional arguments:
-h, --help show this help message and exit
-p P This will be printed
As you can see, using -h
(or --help
) displays the help message and does not execute any other code (by default).
Upvotes: 1
Reputation: 16184
maybe just arrange for your parse
function to return None
, then the calling function can check for this case and handle it…
for example:
class example_method(object):
# …
def parse(self, message):
if message in {"-h", "--help"}:
print("This is a helpful message")
return # implicitly returns None
self.data = [x.strip() for x in message.split(' ')]
return self.data
res = example.parse(input_message)
if res is None:
return
print(res[0])
Upvotes: 2
Reputation: 12100
You can use exit()
to immediately stop program execution.
def parse(self, message):
if(("-h" or "--help") in message):
print("This is a helpful message")
exit()
else:
self.data = [x.strip() for x in message.split(' ')]
return self.data
Upvotes: 1