From 9833101e37beec9cb93dbe7b484220ff084f620a Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Sat, 11 Dec 2021 18:22:49 +0000 Subject: [PATCH] Strict typing --- bin/genpw | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/bin/genpw b/bin/genpw index cce1d68..eb8b87f 100755 --- a/bin/genpw +++ b/bin/genpw @@ -1,37 +1,38 @@ #!/usr/bin/env python -"""Module to generate a random string +""" +Module to generate a random string Attributes: LENGTH (int): Defaults to a length of 16 """ -import sys -import optparse -import os +import argparse import base64 +import os +import sys LENGTH = 16 -def generate(length): +def generate(length: int) -> str: """Simple function to call OpenSSL Args: length (int): length of string to return """ - return base64.encodestring(os.urandom(256))[:length] - - -def main(): - """Main function to call - """ - parser = optparse.OptionParser() - parser.add_option('-n', dest='length', default=LENGTH, type=int) - opts, _ = parser.parse_args() - - sys.stdout.write(generate(opts.length)) + return base64.encodebytes(os.urandom(256)).decode()[:length] if __name__ == '__main__': - main() - sys.exit(0) + + def main() -> int: + """Main function to call""" + parser = argparse.ArgumentParser() + parser.add_argument('-n', dest='length', default=LENGTH, type=int) + args = parser.parse_args() + + sys.stdout.write(generate(args.length)) + + return 0 + + sys.exit(main())