diff --git a/01_hello/Makefile b/01_hello/Makefile index 1cbd24b42..92b302777 100644 --- a/01_hello/Makefile +++ b/01_hello/Makefile @@ -2,3 +2,7 @@ test: pytest -xv test.py + +# learning Make, I put this to run hello2.py with all arguments. +hello2: + /bin/python3 /home/centos/tiny_python_projects/01_hello/hello2.py hello -a 'man' -i 17 -f /etc/hosts -o diff --git a/01_hello/hello.py b/01_hello/hello.py new file mode 100755 index 000000000..ac1b43fe7 --- /dev/null +++ b/01_hello/hello.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 +import argparse +parser = argparse.ArgumentParser() +parser.parse_args() + +print('Hello, World!') +print('This is to test git commit, pull and push the 2nd time.') + + diff --git a/01_hello/hello2.py b/01_hello/hello2.py new file mode 100644 index 000000000..345bb75e7 --- /dev/null +++ b/01_hello/hello2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +""" +Author: +Date: +Purpose: +""" + +import argparse +import os +import sys + +#------------------------------------------------------------------ +def get_args(): + """ Get command-line argumenst """ + parser = argparse.ArgumentParser( + description='Rock the Casbah', + formatter_class=argparse.ArgumentDefaultsHelpFormatter + ) + parser.add_argument('positional', metavar='str', help='A positional argument') + parser.add_argument('-a', '--arg', help='A named string argument', metavar='str', type=str, default='') + parser.add_argument('-i', '--int', help='A named integer argument', metavar='int', type=int, default=0) + parser.add_argument('-f', '--file', help='A readable file', metavar='FILE', type=argparse.FileType('r'), default=None) + parser.add_argument('-o', '--on', help='A boolean flag', action='store_true') + return parser.parse_args() + +#----------------------------------------------------------------- +def main(): + """ Make some noise """ + args = get_args() + str_arg = args.arg + int_arg = args.int + file_arg = args.file + flag_arg = args.on + pos_arg = args.positional + + print(f'str_arg = "{str_arg}"') + print(f'int_arg = "{int_arg}"') + print('file_arg = "{}"'.format(file_arg.name if file_arg else '')) + print(f'flag_arg = "{flag_arg}"') + print(f'positional_arg = "{pos_arg}"') + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/02_crowsnest/02_crowsnest.py b/02_crowsnest/02_crowsnest.py new file mode 100755 index 000000000..36f39bab8 --- /dev/null +++ b/02_crowsnest/02_crowsnest.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +""" +Author : centos +Date : 2022-07-15 +Purpose: Rock the Casbah +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description='Rock the Casbah', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + parser.add_argument('positional', + metavar='str', + help='A positional argument') + + parser.add_argument('-a', + '--arg', + help='A named string argument', + metavar='str', + type=str, + default='') + + parser.add_argument('-i', + '--int', + help='A named integer argument', + metavar='int', + type=int, + default=0) + + parser.add_argument('-f', + '--file', + help='A readable file', + metavar='FILE', + type=argparse.FileType('rt'), + default=None) + + parser.add_argument('-o', + '--on', + help='A boolean flag', + action='store_true') + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + str_arg = args.arg + int_arg = args.int + file_arg = args.file + flag_arg = args.on + pos_arg = args.positional + + print(f'str_arg = "{str_arg}"') + print(f'int_arg = "{int_arg}"') + print('file_arg = "{}"'.format(file_arg.name if file_arg else '')) + print(f'flag_arg = "{flag_arg}"') + print(f'positional = "{pos_arg}"') + + +# -------------------------------------------------- +if __name__ == '__main__': + main()