1212from datetime import date
1313from pathlib import Path
1414
15+ from typing import NamedTuple
16+
17+
18+ class Args (NamedTuple ):
19+ program : str
20+ name : str
21+ email : str
22+ purpose : str
23+ overwrite : bool
24+
1525
1626# --------------------------------------------------
17- def get_args ():
27+ def get_args () -> Args :
1828 """Get arguments"""
1929
2030 parser = argparse .ArgumentParser (
2131 description = 'Create Python argparse program' ,
2232 formatter_class = argparse .ArgumentDefaultsHelpFormatter )
2333
2434 defaults = get_defaults ()
35+ username = os .getenv ('USER' ) or 'Anonymous'
36+ hostname = os .getenv ('HOSTNAME' ) or 'localhost'
2537
2638 parser .add_argument ('program' , help = 'Program name' , type = str )
2739
2840 parser .add_argument ('-n' ,
2941 '--name' ,
3042 type = str ,
31- default = defaults .get ('name' , os . getenv ( 'USER' ) ),
43+ default = defaults .get ('name' , username ),
3244 help = 'Name for docstring' )
3345
3446 parser .add_argument ('-e' ,
3547 '--email' ,
3648 type = str ,
37- default = defaults .get ('email' , ' ' ),
49+ default = defaults .get ('email' , f' { username } @ { hostname } ' ),
3850 help = 'Email for docstring' )
3951
4052 parser .add_argument ('-p' ,
4153 '--purpose' ,
4254 type = str ,
43- default = ' Rock the Casbah' ,
55+ default = defaults . get ( 'purpose' , ' Rock the Casbah') ,
4456 help = 'Purpose for docstring' )
4557
4658 parser .add_argument ('-f' ,
@@ -55,55 +67,37 @@ def get_args():
5567 if not args .program :
5668 parser .error (f'Not a usable filename "{ args .program } "' )
5769
58- if args .email :
59- args .email = f'<{ args .email } >'
60-
61- return args
70+ return Args (args .program , args .name , args .email , args .purpose , args .force )
6271
6372
6473# --------------------------------------------------
65- def main ():
74+ def main () -> None :
6675 """Make a jazz noise here"""
6776
6877 args = get_args ()
6978 program = args .program
7079
71- if os .path .isfile (program ) and not args .force :
80+ if os .path .isfile (program ) and not args .overwrite :
7281 answer = input (f'"{ program } " exists. Overwrite? [yN] ' )
7382 if not answer .lower ().startswith ('y' ):
74- print ('Will not overwrite. Bye!' )
75- sys .exit ()
83+ sys .exit ('Will not overwrite. Bye!' )
7684
77- text = body (name = args .name ,
78- email = args .email ,
79- purpose = args .purpose ,
80- date = str (date .today ()))
81-
82- print (text , file = open (program , 'wt' ), end = '' )
85+ print (body (args ), file = open (program , 'wt' ), end = '' )
8386 subprocess .run (['chmod' , '+x' , program ])
8487 print (f'Done, see new script "{ program } ."' )
8588
8689
8790# --------------------------------------------------
88- def preamble (** args ):
89- return f"""#!/usr/bin/env python3
90- \" \" \"
91- Author : { args ['name' ]} { ' <' + args ['email' ] + '>' if args ['email' ] else '' }
92- Date : { args ['date' ]}
93- Purpose: { args ['purpose' ]}
94- \" \" \"
95- """
96-
97-
98- # --------------------------------------------------
99- def body (** args ):
91+ def body (args : Args ) -> str :
10092 """ The program template """
10193
94+ today = str (date .today ())
95+
10296 return f"""#!/usr/bin/env python3
10397\" \" \"
104- Author : { args [ ' name' ] } { args [ ' email' ] }
105- Date : { args [ 'date' ] }
106- Purpose: { args [ ' purpose' ] }
98+ Author : { args . name } { ' <' + args . email + '>' if args . email else '' }
99+ Date : { today }
100+ Purpose: { args . purpose }
107101\" \" \"
108102
109103import argparse
@@ -114,7 +108,7 @@ def get_args():
114108 \" \" \" Get command-line arguments\" \" \"
115109
116110 parser = argparse.ArgumentParser(
117- description='{ args [ " purpose" ] } ',
111+ description='{ args . purpose } ',
118112 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
119113
120114 parser.add_argument('positional',
0 commit comments