From 6ae076e99dd947dabb6d2501954e78765d5b505c Mon Sep 17 00:00:00 2001 From: TJ LeBlanc Date: Thu, 1 Oct 2020 20:02:18 -0400 Subject: [PATCH 1/5] Added support for unix and windows style arguments --- README.md | 5 ++++- gus.py | 28 ++++++++++------------------ help.txt | 17 ----------------- 3 files changed, 14 insertions(+), 36 deletions(-) delete mode 100644 help.txt diff --git a/README.md b/README.md index 85ef08b..83c041d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,10 @@ GUS can be used from the command line or python shell.\ The location to a source file must be supplied. #### From the command line ``` -python gus.py example.html +python gus.py -f example.html or python gus.py --file example.html to check a file +python gus.py -v or python gus.py --version for version info +python gus.py -h or python gus.py --help for help + ``` #### From the python shell ``` diff --git a/gus.py b/gus.py index cd9bf05..899c197 100644 --- a/gus.py +++ b/gus.py @@ -1,4 +1,5 @@ # release 0.1 +import argparse import http.client import re import sys @@ -10,12 +11,9 @@ # main function def tavo(source = ''): - if len(source) == 0: - get_help() # calls for help if no arg provided - else: - urls = get_list(source) # creates list of all urls from provided source file - checked = check_list(urls) # checks if each urls is nested and then checks http status - print_rtf(source, checked) # prints results to output.rtf + urls = get_list(source) # creates list of all urls from provided source file + checked = check_list(urls) # checks if each urls is nested and then checks http status + print_rtf(source, checked) # prints results to output.rtf # open file and return list of regex matches def get_list(source): @@ -82,16 +80,10 @@ def print_rtf(source, results): except: print('error writing list') -# print intructions on how to use tool -def get_help(): - try: - help = open('help.txt').read() - print(help) - except: - print("error printing help") +# Create parser that allows for arguments to be used with the tool (-f, --file, -v, --version, -h, --help) +parser = argparse.ArgumentParser(description='Checks for dead urls in a file') +parser.add_argument('-f', '--file', help='Check URLS in text file (e.g, gus.py -f index.html') +parser.add_argument('-v', '--version', action="version", version='Get-Url-Status (GUS) Text-As-Visual-Output (TAVO) version 0.1', help='Display version info') +args = parser.parse_args() -if __name__ == "__main__": - if (len(sys.argv)==1): - get_help() # calls for help if no arg provided - else: - tavo(str(sys.argv[1])) # send command line arg to main function \ No newline at end of file +tavo(str(sys.argv[2])) # send command line arg to main function \ No newline at end of file diff --git a/help.txt b/help.txt deleted file mode 100644 index 3d6a6f5..0000000 --- a/help.txt +++ /dev/null @@ -1,17 +0,0 @@ -************************************************************* -***** Get-Url-Status (GUS) Text-As-Visual-Output (TAVO) ***** - --- version 0.1 --- - -GUS can be used from the command line or python shell -The location to a source file must be supplied. - -From the command line: ->>> python gus.py example.txt -or ->>> python gus.py ~/another/file.html - -From the python shell: ->>> import gus ->>> gus.tavo("your.filename.here") - -************************************************************* \ No newline at end of file From 59dca026eb505d19ab97f382c99b5a540c4540ab Mon Sep 17 00:00:00 2001 From: TJ LeBlanc <44535539+IcemanEtika@users.noreply.github.com> Date: Thu, 1 Oct 2020 20:03:23 -0400 Subject: [PATCH 2/5] Added new commandline arguments to README --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 83c041d..22d2b3c 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ The location to a source file must be supplied. python gus.py -f example.html or python gus.py --file example.html to check a file python gus.py -v or python gus.py --version for version info python gus.py -h or python gus.py --help for help - ``` #### From the python shell ``` From 249fe7517e0e3a82abe5cb328ff5401a4dd0b053 Mon Sep 17 00:00:00 2001 From: TJ LeBlanc Date: Sat, 3 Oct 2020 22:40:56 -0400 Subject: [PATCH 3/5] Fixed errors caused by argparse --- gus.py | 37 +++++++++++++++++++++++++++---------- help.txt | 28 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 help.txt diff --git a/gus.py b/gus.py index 899c197..3f787e4 100644 --- a/gus.py +++ b/gus.py @@ -1,5 +1,4 @@ # release 0.1 -import argparse import http.client import re import sys @@ -11,9 +10,12 @@ # main function def tavo(source = ''): - urls = get_list(source) # creates list of all urls from provided source file - checked = check_list(urls) # checks if each urls is nested and then checks http status - print_rtf(source, checked) # prints results to output.rtf + if len(source) == 0: + get_help() # calls for help if no arg provided + else: + urls = get_list(source) # creates list of all urls from provided source file + checked = check_list(urls) # checks if each urls is nested and then checks http status + print_rtf(source, checked) # prints results to output.rtf # open file and return list of regex matches def get_list(source): @@ -80,10 +82,25 @@ def print_rtf(source, results): except: print('error writing list') -# Create parser that allows for arguments to be used with the tool (-f, --file, -v, --version, -h, --help) -parser = argparse.ArgumentParser(description='Checks for dead urls in a file') -parser.add_argument('-f', '--file', help='Check URLS in text file (e.g, gus.py -f index.html') -parser.add_argument('-v', '--version', action="version", version='Get-Url-Status (GUS) Text-As-Visual-Output (TAVO) version 0.1', help='Display version info') -args = parser.parse_args() +# print intructions on how to use tool +def get_help(): + try: + help = open('help.txt').read() + print(help) + except: + print("error printing help") -tavo(str(sys.argv[2])) # send command line arg to main function \ No newline at end of file +if __name__ == "__main__": + if len(sys.argv) == 1: + get_help() # calls for help if no arg provided + elif len(sys.argv) == 2 and str(sys.argv[1])[:1] != "-": + tavo(str(sys.argv[1])) # send command line arg to main function + else: + if len(sys.argv) == 3 and (sys.argv[1] == "-f" or sys.argv[1] == "--file"): + tavo(str(sys.argv[2])) # send command line arg to main function + elif len(sys.argv) == 2 and sys.argv[1] == "-v" or sys.argv[1] == "--version": + print('Get-Url-Status (GUS) Text-As-Visual-Output (TAVO) version 0.1') #print version info + elif len(sys.argv) == 2 and sys.argv[1] == "-h" or sys.argv[1] == "--help": + get_help() + else: + print('invalid argument error') diff --git a/help.txt b/help.txt new file mode 100644 index 0000000..66ad876 --- /dev/null +++ b/help.txt @@ -0,0 +1,28 @@ +************************************************************* +***** Get-Url-Status (GUS) Text-As-Visual-Output (TAVO) ***** + --- version 0.1 --- + +GUS can be used from the command line or python shell +The location to a source file must be supplied. + +To check files from the command line: +>>> python gus.py example.txt +or +>>> python gus.py ~/another/file.html +or +>>> python gus.py -f or --file example.txt +or +>>> python gus.py -f or --file ~/another/file.html + +To check version information: +>>> python gus.py -v or --version + +To get help: +>>> python gus.py -h or --help + + +To check files from the python shell: +>>> import gus +>>> gus.tavo("your.filename.here") + +************************************************************* \ No newline at end of file From 34be10ba63e44b2297159e03a1b277719b2d60fe Mon Sep 17 00:00:00 2001 From: TJ LeBlanc <44535539+IcemanEtika@users.noreply.github.com> Date: Sat, 3 Oct 2020 22:48:17 -0400 Subject: [PATCH 4/5] Added new commands --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 22d2b3c..53c2c59 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ GUS can be used from the command line or python shell.\ The location to a source file must be supplied. #### From the command line ``` +python gus.py example.html python gus.py -f example.html or python gus.py --file example.html to check a file python gus.py -v or python gus.py --version for version info python gus.py -h or python gus.py --help for help From e5dd205e78cd4ca26fbc3245f28f19f108e82382 Mon Sep 17 00:00:00 2001 From: TJ LeBlanc Date: Sat, 3 Oct 2020 22:53:42 -0400 Subject: [PATCH 5/5] Fixed bug where --version and --help would run if more arguments were typed in --- gus.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gus.py b/gus.py index 3f787e4..ff78380 100644 --- a/gus.py +++ b/gus.py @@ -98,9 +98,9 @@ def get_help(): else: if len(sys.argv) == 3 and (sys.argv[1] == "-f" or sys.argv[1] == "--file"): tavo(str(sys.argv[2])) # send command line arg to main function - elif len(sys.argv) == 2 and sys.argv[1] == "-v" or sys.argv[1] == "--version": - print('Get-Url-Status (GUS) Text-As-Visual-Output (TAVO) version 0.1') #print version info - elif len(sys.argv) == 2 and sys.argv[1] == "-h" or sys.argv[1] == "--help": + elif len(sys.argv) == 2 and (sys.argv[1] == "-v" or sys.argv[1] == "--version"): + print('Get-Url-Status (GUS) Text-As-Visual-Output (TAVO) version 0.1') # print version info + elif len(sys.argv) == 2 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): get_help() else: - print('invalid argument error') + print('invalid argument error') # print if user inputs wrong argument or if there are too many arguments