|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Author : gmartins <gmartins@localhost> |
| 4 | +Date : 2024-12-18 |
| 5 | +Purpose: Rock the Casbah |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | + |
| 10 | +TEMPLATE = 'You are bringing {}.' |
| 11 | + |
| 12 | + |
| 13 | +# -------------------------------------------------- |
| 14 | +def get_args(): |
| 15 | + """Get command-line arguments""" |
| 16 | + |
| 17 | + parser = argparse.ArgumentParser( |
| 18 | + description='Picnic game', |
| 19 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 20 | + |
| 21 | + parser.add_argument('positional', |
| 22 | + metavar='str', |
| 23 | + nargs='+', |
| 24 | + help='Item(s) to bring') |
| 25 | + |
| 26 | + parser.add_argument('-s', |
| 27 | + '--sorted', |
| 28 | + help='Sort the items', |
| 29 | + action='store_true') |
| 30 | + |
| 31 | + parser.add_argument('-n', |
| 32 | + '--not-oxford', |
| 33 | + help='Do not print Oxford comma', |
| 34 | + action='store_true') |
| 35 | + |
| 36 | + parser.add_argument('-r', |
| 37 | + '--sep', |
| 38 | + metavar='separator', |
| 39 | + default=',', |
| 40 | + help='Separator') |
| 41 | + |
| 42 | + return parser.parse_args() |
| 43 | + |
| 44 | + |
| 45 | +# -------------------------------------------------- |
| 46 | +def main(): |
| 47 | + """Make a jazz noise here""" |
| 48 | + |
| 49 | + args = get_args() |
| 50 | + sorted_arg = args.sorted |
| 51 | + |
| 52 | + pos_arg = args.positional |
| 53 | + oxford_arg = args.not_oxford |
| 54 | + sep_arg = args.sep + ' ' |
| 55 | + |
| 56 | + if sep_arg != ', ': |
| 57 | + oxford_arg = True |
| 58 | + |
| 59 | + if sorted_arg: |
| 60 | + pos_arg.sort() |
| 61 | + |
| 62 | + if len(pos_arg) < 1: |
| 63 | + print('Error') |
| 64 | + elif len(pos_arg) == 1: |
| 65 | + print(TEMPLATE.format(pos_arg[0])) |
| 66 | + else: |
| 67 | + if len(pos_arg) == 2: |
| 68 | + s = ' and '.join(pos_arg) |
| 69 | + else: |
| 70 | + pos_arg[-1] = 'and ' + pos_arg[-1] |
| 71 | + s = sep_arg.join(pos_arg) |
| 72 | + if oxford_arg: |
| 73 | + s = s.replace(sep_arg + 'and', ' and') |
| 74 | + print(TEMPLATE.format(s)) |
| 75 | + |
| 76 | + |
| 77 | +# -------------------------------------------------- |
| 78 | +if __name__ == '__main__': |
| 79 | + main() |
0 commit comments