Skip to content

Commit 1dffaab

Browse files
committed
Go on a picnic
1 parent 5597879 commit 1dffaab

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ tex2pdf*
1111
.coverage
1212
.idea
1313
.vscode
14-
03_picnic/picnic.py
1514
04_jump_the_five/jump.py
1615
05_howler/howler.py
1716
06_wc/wc.py

03_picnic/picnic.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Ryan Campbell <me@ryancampbell.name>
4+
Date : 2023-09-25
5+
Purpose: Picnic game
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description="Picnic game",
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
18+
)
19+
20+
parser.add_argument("items", metavar="str", help="Item(s) to bring", nargs="+")
21+
22+
parser.add_argument("-s", "--sorted", help="Sort the items", action="store_true")
23+
24+
return parser.parse_args()
25+
26+
27+
# --------------------------------------------------
28+
def main():
29+
"""Make a jazz noise here"""
30+
31+
args = get_args()
32+
33+
if len(args.items) == 1:
34+
print(f"You are bringing {args.items[0]}.")
35+
elif len(args.items) == 2:
36+
if args.sorted:
37+
args.items.sort()
38+
print(f"You are bringing {' and '.join(args.items)}.")
39+
else:
40+
if args.sorted:
41+
args.items.sort()
42+
last_item = args.items.pop()
43+
print(f"You are bringing {', '.join(args.items)}, and {last_item}.")
44+
45+
46+
# TODO
47+
# Add an option so the user can choose not to print with the Oxford comma (even though that is a morally indefensible option).
48+
# Add an option to separate items with a character passed in by the user (like a semicolon if the list of items needs to contain commas).
49+
50+
# --------------------------------------------------
51+
if __name__ == "__main__":
52+
main()

0 commit comments

Comments
 (0)