diff --git a/owodecode.py b/owodecode.py index ff4f080..f13bfe5 100644 --- a/owodecode.py +++ b/owodecode.py @@ -4,31 +4,27 @@ # Made by Glitch, 2020 # https://www.glitchfur.net -from sys import argv, stdout, stderr -from os.path import exists, split +from sys import stdout, stderr +from os.path import exists from os import remove -KEEP_ORIG = False -STDOUT_FLAG = False +import argparse def main(): - if len(argv) < 2: - print("The syntax for running this script is as follows:") - print("python owodecode.py [-kc] [ ... ]") - exit(0) - in_fns = argv[1:] - # There is probably a better way to handle parameters. But considering - # there are only two, I'm not too worried about it right now. - for param in in_fns: - if param.startswith("-"): - global KEEP_ORIG - global STDOUT_FLAG - if "k" in param: - KEEP_ORIG = True - if "c" in param: - STDOUT_FLAG = True - KEEP_ORIG = True # Output going to stdout, keep original file - in_fns.remove(param) + parser = argparse.ArgumentParser() + + parser.add_argument("encoded_file", metavar="", type=str, nargs="+") + parser.add_argument('-k', dest="keep_original", action='store_true', + help="Keep the input file. Do not delete it after doing decoding") + parser.add_argument('-c', dest="to_stdout", action='store_true', + help="Decode to stdout instead of a file. This implies -k. This also makes piping output possible") + + args = parser.parse_args() + + to_stdout = args.to_stdout + keep_original = args.keep_original or to_stdout + in_fns = args.encoded_file + for fn in in_fns: if not exists(fn): print("%s: No such file or directory" % fn, file=stderr) @@ -43,11 +39,11 @@ def main(): in_fns.remove(fn) out_fns = [fn[:-4] for fn in in_fns] for i in range(len(in_fns)): - decode(in_fns[i], out_fns[i]) + decode(in_fns[i], out_fns[i], to_stdout, keep_original) -def decode(in_fn, out_fn): +def decode(in_fn, out_fn, to_stdout=False, keep_original=False): in_fp = open(in_fn, "r") - if STDOUT_FLAG == False: + if to_stdout == False: out_fp = open(out_fn, "wb") else: out_fp = stdout.buffer @@ -66,9 +62,9 @@ def decode(in_fn, out_fn): "it is not an encoded file. Aborting." % in_fn) exit(1) in_fp.close() - if STDOUT_FLAG == False: + if to_stdout == False: out_fp.close() - if KEEP_ORIG == False: + if keep_original == False: remove(in_fn) if __name__ == "__main__": diff --git a/owoencode.py b/owoencode.py index afb2681..813441a 100644 --- a/owoencode.py +++ b/owoencode.py @@ -4,31 +4,27 @@ # Made by Glitch, 2020 # https://www.glitchfur.net -from sys import argv, stdout, stderr -from os.path import exists, split +from sys import stdout, stderr +from os.path import exists from os import remove -KEEP_ORIG = False -STDOUT_FLAG = False +import argparse def main(): - if len(argv) < 2: - print("The syntax for running this script is as follows:") - print("python owoencode.py [-kc] [ ... ]") - exit(0) - in_fns = argv[1:] - # There is probably a better way to handle parameters. But considering - # there are only two, I'm not too worried about it right now. - for param in in_fns: - if param.startswith("-"): - global KEEP_ORIG - global STDOUT_FLAG - if "k" in param: - KEEP_ORIG = True - if "c" in param: - STDOUT_FLAG = True - KEEP_ORIG = True # Output going to stdout, keep original file - in_fns.remove(param) + parser = argparse.ArgumentParser() + + parser.add_argument("encoded_file", metavar="", type=str, nargs="+") + parser.add_argument('-k', dest="keep_original", action='store_true', + help="Keep the input file. Do not delete it after doing encoding") + parser.add_argument('-c', dest="to_stdout", action='store_true', + help="Encode to stdout instead of a file. This implies -k. This also makes piping output possible") + + args = parser.parse_args() + + to_stdout = args.to_stdout + keep_original = args.keep_original or to_stdout + in_fns = args.encoded_file + for fn in in_fns: if not exists(fn): print("%s: No such file or directory" % fn, file=stderr) @@ -39,11 +35,11 @@ def main(): in_fns.remove(fn) out_fns = ["%s.owo" % fn for fn in in_fns] for i in range(len(in_fns)): - encode(in_fns[i], out_fns[i]) + encode(in_fns[i], out_fns[i], to_stdout, keep_original) -def encode(in_fn, out_fn): +def encode(in_fn, out_fn, to_stdout=False, keep_original=False): in_fp = open(in_fn, "rb") - if STDOUT_FLAG == False: + if to_stdout == False: out_fp = open(out_fn, "w") else: out_fp = stdout @@ -54,9 +50,9 @@ def encode(in_fn, out_fn): out_buffer = ''.join([bin(byte)[2:].zfill(8) for byte in in_buffer]) out_fp.write(out_buffer.replace("1", "OwO").replace("0", "UwU")) in_fp.close() - if STDOUT_FLAG == False: + if to_stdout == False: out_fp.close() - if KEEP_ORIG == False: + if keep_original == False: remove(in_fn) if __name__ == "__main__":