Compare commits
5 Commits
d0dd2cff17
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 5381381081 | |||
| 47e6ee9ca8 | |||
| 33b91986a1 | |||
| ba159e8021 | |||
| 6b2bedefe5 |
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Nicolò Paraciani
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -6,17 +6,18 @@ import sys
|
||||
|
||||
def find_line(line: str, pattern: str, i_case: bool, is_regexp=False) -> bool:
|
||||
"""
|
||||
Distinguish between regexp or not (default is False)
|
||||
Check if line contains matches for pattern, with case and regexp flags.
|
||||
Returns True if pattern is found in line.
|
||||
"""
|
||||
|
||||
found = False
|
||||
|
||||
if i_case:
|
||||
line = line.lower()
|
||||
pattern = pattern.lower()
|
||||
|
||||
if is_regexp:
|
||||
matches = re.findall(pattern, line)
|
||||
FIND_COND = len(matches) > 0
|
||||
elif i_case:
|
||||
lower = line.lower()
|
||||
FIND_COND = lower.find(pattern.lower()) != -1
|
||||
else:
|
||||
FIND_COND = line.find(pattern) != -1
|
||||
|
||||
@@ -72,12 +73,12 @@ def parse_args(args: list) -> dict:
|
||||
Parse cli arguments and return the tree as a dict
|
||||
"""
|
||||
# TODO move to class
|
||||
OPTS = [
|
||||
OPTS = (
|
||||
'i', # Case-insensitive search
|
||||
'E', # Pattern is a full regexp
|
||||
'r', # Search recursively in dir
|
||||
'n', # Print line numbers
|
||||
]
|
||||
)
|
||||
# TODO Handle extended options? (e.g. --ignore-case)
|
||||
LONG_OPTS = [
|
||||
'--ignore-case',
|
||||
@@ -157,8 +158,7 @@ def process_grep(args_tree: dict) -> list:
|
||||
return found_lines
|
||||
|
||||
|
||||
# When the script is executed directly...
|
||||
if __name__ == "__main__":
|
||||
def main():
|
||||
args = sys.argv[1:]
|
||||
|
||||
if not check_args(args):
|
||||
@@ -178,3 +178,10 @@ if __name__ == "__main__":
|
||||
except exceptions.InvalidPatternError as e:
|
||||
print(e.get_message())
|
||||
sys.exit(1)
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
# When the script is executed directly...
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user