Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored replace_command() #49

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,26 @@ def regedit_add(folder: str, name: str = None, typ: str = None, value: str = Non
process.wait()


def replace_command(orig: str, repl: str) -> None:
def replace_command(orig: str, repl: str, match_flags: re.RegexFlag = re.IGNORECASE) -> bool:
""" Make a commandline replacement in sys.argv
"""
Returns if there was any match.

log.info(f'Changing {orig} to {repl}')
By default the search is case insensitive,
you can override this behaviour with re.RegexFlag.NOFLAG
"""
found = False
for idx, arg in enumerate(sys.argv):
if orig in arg:
sys.argv[idx] = arg.replace(orig, repl)
replaced = re.sub(orig, repl, arg, flags=match_flags)
if replaced == arg:
continue
sys.argv[idx] = replaced
found = True

R1kaB3rN marked this conversation as resolved.
Show resolved Hide resolved
if found:
log.info(f'Changed "{orig}" to "{repl}"')
else:
log.warn(f'Can not change "{orig}" to "{repl}", command not found')
return found


def append_argument(argument: str) -> None:
Expand Down