Onionr/src/onionrcommands/parser/recommend.py

39 lines
1.4 KiB
Python
Raw Normal View History

"""Onionr - Private P2P Communication.
Try to provide recommendations for invalid Onionr commands
"""
2019-07-31 05:10:28 +00:00
import sys
from difflib import SequenceMatcher
import logger
from . import arguments
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
2019-07-31 05:10:28 +00:00
2019-12-19 10:32:40 +00:00
2019-08-29 22:17:47 +00:00
def recommend(print_default: bool = True):
"""Print out a recommendation for argv cmd if one is available."""
2019-07-31 05:10:28 +00:00
tried = sys.argv[1]
args = arguments.get_arguments()
print_message = 'Command not found:'
for key in args.keys():
for word in key:
if SequenceMatcher(None, tried, word).ratio() >= 0.75:
2019-12-19 10:32:40 +00:00
logger.warn(f'{print_message} "{tried}", '
2020-01-30 05:21:13 +00:00
+ f'did you mean "{word}"?',
2019-12-19 10:32:40 +00:00
terminal=True)
2019-07-31 05:10:28 +00:00
return
if print_default:
logger.error('%s "%s"' % (print_message, tried), terminal=True)