diff --git a/.idea/runConfigurations/main.xml b/.idea/runConfigurations/main.xml deleted file mode 100644 index 4231025..0000000 --- a/.idea/runConfigurations/main.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - \ No newline at end of file diff --git a/README.md b/README.md index d01ff19..b69b6f1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,42 @@ # py-regex-replace -perform regex-replacement on stdin and output result to stdout +Perform regex-replacement on stdin and output result to stdout. + +## Usage + +``` +usage: py-regex-replace [-h] --pattern PATTERN --repl REPL [--count COUNT] + +options: + -h, --help show this help message and exit + --pattern PATTERN, -p PATTERN + regex search pattern + --repl REPL, -r REPL replacement + --count COUNT, -c COUNT + expected count of pattern in string. -1 allows any + count +``` + +``` +usage: py-replace [-h] --text TEXT --repl REPL [--count COUNT] + +options: + -h, --help show this help message and exit + --text TEXT, -t TEXT text to replace + --repl REPL, -r REPL replacement + --count COUNT, -c COUNT + expected count of text in string. -1 allows any + count +``` + +## Examples + +Some small example scripts can be found in [./test](./test). They can be run from inside the test directory: + +```shell +cd test +./test.sh +``` ## Installation diff --git a/setup.cfg b/setup.cfg index 47506f5..e556f16 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ [metadata] name = py-regex-replace -version = 0.0.2 +version = 0.1.0 author = Daniel Langbein author_email = daniel@systemli.org description = perform regex-replacement on stdin and output result to stdout diff --git a/src/py_regex_replace/main.py b/src/py_regex_replace/main.py index b553b36..5c73882 100755 --- a/src/py_regex_replace/main.py +++ b/src/py_regex_replace/main.py @@ -19,8 +19,9 @@ def main(): # Return all non-overlapping matches of pattern in string, as a list of strings or tuples. # The string is scanned left-to-right, and matches are returned in order. matches = re.findall(pattern, string, flags=re.MULTILINE) - if len(matches) != count: - print(f'[ERROR] Expected {count} matches of pattern in string, but got {len(matches)}.') + if count != -1 and len(matches) != count: + print(f'[ERROR] Expected {count} matches of pattern in string, but got {len(matches)}.', + file=sys.stderr) exit(1) # https://docs.python.org/3/library/re.html#re.sub @@ -41,14 +42,15 @@ def parse_args(): help='replacement', required=True, type=str) parser.add_argument('--count', '-c', - help='expected count of pattern in string', + help='expected count of pattern in string. -1 allows any count', default=1, type=int) args = parser.parse_args() string = sys.stdin.read() if len(string) == 0: - print('[WARNING] The given string is empty.', file=sys.stderr) + print('[WARNING] The given string is empty.', + file=sys.stderr) return args.pattern, args.repl, string, args.count diff --git a/src/py_replace/main.py b/src/py_replace/main.py index c557441..f4c0235 100755 --- a/src/py_replace/main.py +++ b/src/py_replace/main.py @@ -8,8 +8,9 @@ def main(): text, repl, string, count = parse_args() occurrences = string.count(text) - if occurrences != count: - print(f'[ERROR] Expected {count} occurrences of text in string, but got {occurrences}.') + if count != -1 and occurrences != count: + print(f'[ERROR] Expected {count} occurrences of text in string, but got {occurrences}.', + file=sys.stderr) exit(1) string = string.replace(text, repl) @@ -18,7 +19,7 @@ def main(): def parse_args(): - parser = argparse.ArgumentParser(prog='py-regex-replace') + parser = argparse.ArgumentParser(prog='py-replace') parser.add_argument('--text', '-t', help='text to replace', @@ -27,14 +28,15 @@ def parse_args(): help='replacement', required=True, type=str) parser.add_argument('--count', '-c', - help='expected count of text in string', + help='expected count of text in string. -1 allows any count', default=1, type=int) args = parser.parse_args() string = sys.stdin.read() if len(string) == 0: - print('[WARNING] The given string is empty.', file=sys.stderr) + print('[WARNING] The given string is empty.', + file=sys.stderr) return args.text, args.repl, string, args.count diff --git a/test/test.sh b/test/test.sh old mode 100644 new mode 100755 diff --git a/test/test_multiline_pattern.sh b/test/test_multiline_pattern.sh old mode 100644 new mode 100755 diff --git a/test/test_multiline_replacement.sh b/test/test_multiline_replacement.sh old mode 100644 new mode 100755