Improved argument system to more accurately handle incorrect user inputs

This commit is contained in:
2022-05-30 02:38:18 -04:00
parent f760b5420c
commit d116582144
+47 -42
View File
@@ -217,7 +217,7 @@ def tweak(): # runs configuration wizard
def print_info(): # prints help text based on argument def print_info(): # prints help text based on argument
if argument == 'help' or argument == '--help' or argument == '-h': if argument.startswith('help') or argument.startswith('--help') or argument.startswith('-h'):
print('\n\u001b[1msshyp copyright (c) 2021-2022 randall winkhart\u001b[0m\n') print('\n\u001b[1msshyp copyright (c) 2021-2022 randall winkhart\u001b[0m\n')
print("this is free software, and you are welcome to redistribute it under certain conditions;\nthis program " print("this is free software, and you are welcome to redistribute it under certain conditions;\nthis program "
"comes with absolutely no warranty;\ntype `sshyp license' for details") "comes with absolutely no warranty;\ntype `sshyp license' for details")
@@ -251,7 +251,7 @@ def print_info(): # prints help text based on argument
print('gen:') print('gen:')
print(' update/-u generate a password for an existing entry\n') print(' update/-u generate a password for an existing entry\n')
print("\u001b[1mtip:\u001b[0m you can quickly read an entry with 'sshyp /<entry name>'") print("\u001b[1mtip:\u001b[0m you can quickly read an entry with 'sshyp /<entry name>'")
elif argument == 'version' or argument == '-v': elif argument.startswith('version') or argument.startswith('-v'):
print('\nsshyp is a simple, self-hosted, sftp-synchronized password manager\nfor unix(-like) systems (currently' print('\nsshyp is a simple, self-hosted, sftp-synchronized password manager\nfor unix(-like) systems (currently'
' haiku/linux/termux)\n\nsshyp is a viable alternative to (and compatible with) pass/password-store\n') ' haiku/linux/termux)\n\nsshyp is a viable alternative to (and compatible with) pass/password-store\n')
print(" .. \u001b[38;5;9m♥♥ ♥♥\u001b[0m ..\n .''.''/()\\ \u001b[38;5;13m" print(" .. \u001b[38;5;9m♥♥ ♥♥\u001b[0m ..\n .''.''/()\\ \u001b[38;5;13m"
@@ -273,21 +273,21 @@ def print_info(): # prints help text based on argument
'\u001b[38;5;7;48;5;8m/\u001b[0m') '\u001b[38;5;7;48;5;8m/\u001b[0m')
print('\u001b[38;5;7;48;5;8m<><><><><><><><><><><><><><><><><><><><><><><><><><><><>\u001b[0m\n') print('\u001b[38;5;7;48;5;8m<><><><><><><><><><><><><><><><><><><><><><><><><><><><>\u001b[0m\n')
print('see https://github.com/rwinkhart/sshyp for more information\n') print('see https://github.com/rwinkhart/sshyp for more information\n')
elif argument == 'license': elif argument.startswith('license'):
print('\nThis program is free software: you can redistribute it and/or modify it under the terms of the GNU ' print('\nThis program is free software: you can redistribute it and/or modify it under the terms of the GNU '
'General\nPublic License as published by the Free Software Foundation, either version 3 of the License,' 'General\nPublic License as published by the Free Software Foundation, either version 3 of the License,'
'\nor (at your option) any later version.\n\nThis program is distributed in the hope that it will be ' '\nor (at your option) any later version.\n\nThis program is distributed in the hope that it will be '
'useful, but WITHOUT ANY WARRANTY;\nwithout even the implied warranty of MERCHANTABILITY or FITNESS FOR A' 'useful, but WITHOUT ANY WARRANTY;\nwithout even the implied warranty of MERCHANTABILITY or FITNESS FOR A'
' PARTICULAR PURPOSE.\nSee the GNU General Public License for more details.' ' PARTICULAR PURPOSE.\nSee the GNU General Public License for more details.'
'\n\nhttps://opensource.org/licenses/GPL-3.0\n') '\n\nhttps://opensource.org/licenses/GPL-3.0\n')
elif argument == 'add': elif argument.startswith('add'):
print('\n\u001b[1musage:\u001b[0m sshyp add [flag [<entry name>]]\u001b[0m\n') print('\n\u001b[1musage:\u001b[0m sshyp add [flag [<entry name>]]\u001b[0m\n')
print('\u001b[1mflags:\u001b[0m') print('\u001b[1mflags:\u001b[0m')
print('add:') print('add:')
print(' password/-p add a password entry') print(' password/-p add a password entry')
print(' note/-n add a note entry') print(' note/-n add a note entry')
print(' folder/-f add a new folder for entries\n') print(' folder/-f add a new folder for entries\n')
elif argument == 'edit': elif argument.startswith('edit'):
print('\n\u001b[1musage:\u001b[0m sshyp edit [flag [<entry name>]]\u001b[0m\n') print('\n\u001b[1musage:\u001b[0m sshyp edit [flag [<entry name>]]\u001b[0m\n')
print('\u001b[1mflags:\u001b[0m') print('\u001b[1mflags:\u001b[0m')
print('edit:') print('edit:')
@@ -296,7 +296,7 @@ def print_info(): # prints help text based on argument
print(' password/-p change the password of an entry') print(' password/-p change the password of an entry')
print(' url/-l change the url attached to an entry') print(' url/-l change the url attached to an entry')
print(' note/-n change the note attached to an entry\n') print(' note/-n change the note attached to an entry\n')
elif argument == 'copy': elif argument.startswith('copy'):
print('\n\u001b[1musage:\u001b[0m sshyp copy [flag [<entry name>]]\u001b[0m\n') print('\n\u001b[1musage:\u001b[0m sshyp copy [flag [<entry name>]]\u001b[0m\n')
print('\u001b[1mflags:\u001b[0m') print('\u001b[1mflags:\u001b[0m')
print('copy:') print('copy:')
@@ -663,54 +663,59 @@ if __name__ == "__main__":
tweak() tweak()
s_exit(0) s_exit(0)
# run function based on argument # run function based on arguments
error = 0 # create an error flag to determine if syncing should occur at end
if argument.startswith('/'): if argument.startswith('/'):
read_shortcut() read_shortcut()
elif argument == '': elif argument == '':
no_arg() no_arg()
elif argument == 'help' or argument == '--help' or argument == '-h' or argument == 'license' or argument \ elif argument == 'help' or argument == '--help' or argument == '-h' or argument == 'license' or argument \
== 'version' or argument == '-v' or argument == 'add' or argument == 'edit' or argument == 'copy': == 'version' or argument == '-v':
print_info() print_info()
elif argument.startswith('add note') or argument.startswith('add -n') or argument.startswith('add password') \ elif argument_list[0] == 'add':
or argument.startswith('add -p'): if len(argument_list) == 1:
add_entry() print_info()
elif argument.startswith('add folder') or argument.startswith('add -f'): elif argument_list[1] == 'note' or argument_list[1] == '-n' or argument_list[1] == 'password' or \
add_folder() argument_list[1] == '-p':
elif argument.startswith('edit rename') or argument.startswith('edit relocate') or argument.startswith( add_entry()
'edit -r'): elif argument_list[1] == 'folder' or argument_list[1] == '-f':
silent_sync = 1 add_folder()
rename() else:
elif argument.startswith('edit username') or argument.startswith('edit -u') or argument.startswith( print_info()
'edit password') \ s_exit(0)
or argument.startswith('edit -p') or argument.startswith('edit url') or argument.startswith( elif argument_list[0] == 'edit':
'edit -l') or \ if len(argument_list) == 1:
argument.startswith('edit note') or argument.startswith('edit -n'): print_info()
edit() elif argument_list[1] == 'rename' or argument_list[1] == 'relocate' or argument_list[1] == '-r':
elif argument.startswith('gen'): silent_sync = 1
rename()
elif argument_list[1] == 'username' or argument_list[1] == '-u' or argument_list[1] == 'password' or \
argument_list[1] == '-p' or argument_list[1] == 'url' or argument_list[1] == '-l' or \
argument_list[1] == 'note' or argument_list[1] == '-n':
edit()
else:
print_info()
s_exit(0)
elif argument_list[0] == 'gen':
gen() gen()
elif argument.startswith('copy username') or argument.startswith('copy -u') or argument.startswith( elif argument_list[0] == 'copy':
'copy password') \ if len(argument_list) == 1:
or argument.startswith('copy -p') or argument.startswith('copy url') or argument.startswith('copy -l') \ print_info()
or argument.startswith('copy note') or argument.startswith('copy -n'): elif argument_list[1] == 'username' or argument_list[1] == '-u' or argument_list[1] == 'password' or \
copy_data() argument_list[1] == '-p' or argument_list[1] == 'url' or argument_list[1] == '-l' or \
elif argument.startswith('shear') or argument.startswith('-rm'): argument_list[1] == 'note' or argument_list[1] == '-n':
copy_data()
else:
print_info()
elif argument_list[0] == 'shear' or argument_list[0] == '-rm':
remove_data() remove_data()
elif argument == 'sync' or argument == '-s':
sync()
else: else:
error = 1 # set error flag to 1 to stop syncing
print("\nArgument error! Please run 'sshyp help' for a list of usable commands.\n") print("\nArgument error! Please run 'sshyp help' for a list of usable commands.\n")
s_exit(1) s_exit(1)
# sync at end of program if any changes were made # sync if any changes were made
if argument != '' and argument != 'help' and argument != '--help' and argument != '-h' and argument != \ if argument_list[0] == 'sync' or argument_list[0] == '-s' or argument_list[0] == 'gen' or argument_list[0] == \
'license' and argument != 'add' and argument != 'sync' and argument != 'edit' and not argument\ 'shear' or argument_list[0] == '-rm' or ((argument_list[0] == 'add' or argument_list[0] == 'edit')
.startswith('copy username') and not argument.startswith('copy -u') and not argument\ and len(argument_list) != 1):
.startswith('copy password') and not argument.startswith('copy -p') and not argument\
.startswith('copy url') and not argument.startswith('copy -l') and not argument.startswith('copy note')\
and not argument.startswith('copy -n') and argument != 'copy' and argument != 'version' and argument \
!= '-v' and error == 0 and argument.startswith('/') is False:
sync() sync()
s_exit(0) s_exit(0)