File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ """
3+ Author : Johan Runesson <info@johanrunesson.se>
4+ Date : 2020-08-03
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+ parser .add_argument ('item' ,
20+ metavar = 'item' ,
21+ nargs = '+' ,
22+ help = 'Item(s) to bring' )
23+
24+ parser .add_argument ('-s' ,
25+ '--sorted' ,
26+ action = 'store_true' ,
27+ help = 'Sort the items' )
28+
29+ return parser .parse_args ()
30+
31+
32+ # --------------------------------------------------
33+ def main ():
34+ """Start things up"""
35+
36+ args = get_args ()
37+ items = args .item
38+ sorted = args .sorted
39+ num = len (items )
40+
41+ if sorted :
42+ items .sort ()
43+
44+ if num > 1 :
45+ items .insert (- 1 , 'and' )
46+
47+ if num > 3 :
48+ last = items .pop ();
49+ items = ', ' .join (items ) + ' ' + last
50+ else :
51+ items = ' ' .join (items )
52+
53+ print (f'You are bringing { items } .' )
54+
55+
56+ # --------------------------------------------------
57+ if __name__ == '__main__' :
58+ main ()
You can’t perform that action at this time.
0 commit comments