12
12
from datetime import date
13
13
from pathlib import Path
14
14
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
+
15
25
16
26
# --------------------------------------------------
17
- def get_args ():
27
+ def get_args () -> Args :
18
28
"""Get arguments"""
19
29
20
30
parser = argparse .ArgumentParser (
21
31
description = 'Create Python argparse program' ,
22
32
formatter_class = argparse .ArgumentDefaultsHelpFormatter )
23
33
24
34
defaults = get_defaults ()
35
+ username = os .getenv ('USER' ) or 'Anonymous'
36
+ hostname = os .getenv ('HOSTNAME' ) or 'localhost'
25
37
26
38
parser .add_argument ('program' , help = 'Program name' , type = str )
27
39
28
40
parser .add_argument ('-n' ,
29
41
'--name' ,
30
42
type = str ,
31
- default = defaults .get ('name' , os . getenv ( 'USER' ) ),
43
+ default = defaults .get ('name' , username ),
32
44
help = 'Name for docstring' )
33
45
34
46
parser .add_argument ('-e' ,
35
47
'--email' ,
36
48
type = str ,
37
- default = defaults .get ('email' , ' ' ),
49
+ default = defaults .get ('email' , f' { username } @ { hostname } ' ),
38
50
help = 'Email for docstring' )
39
51
40
52
parser .add_argument ('-p' ,
41
53
'--purpose' ,
42
54
type = str ,
43
- default = ' Rock the Casbah' ,
55
+ default = defaults . get ( 'purpose' , ' Rock the Casbah') ,
44
56
help = 'Purpose for docstring' )
45
57
46
58
parser .add_argument ('-f' ,
@@ -55,55 +67,37 @@ def get_args():
55
67
if not args .program :
56
68
parser .error (f'Not a usable filename "{ args .program } "' )
57
69
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 )
62
71
63
72
64
73
# --------------------------------------------------
65
- def main ():
74
+ def main () -> None :
66
75
"""Make a jazz noise here"""
67
76
68
77
args = get_args ()
69
78
program = args .program
70
79
71
- if os .path .isfile (program ) and not args .force :
80
+ if os .path .isfile (program ) and not args .overwrite :
72
81
answer = input (f'"{ program } " exists. Overwrite? [yN] ' )
73
82
if not answer .lower ().startswith ('y' ):
74
- print ('Will not overwrite. Bye!' )
75
- sys .exit ()
83
+ sys .exit ('Will not overwrite. Bye!' )
76
84
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 = '' )
83
86
subprocess .run (['chmod' , '+x' , program ])
84
87
print (f'Done, see new script "{ program } ."' )
85
88
86
89
87
90
# --------------------------------------------------
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 :
100
92
""" The program template """
101
93
94
+ today = str (date .today ())
95
+
102
96
return f"""#!/usr/bin/env python3
103
97
\" \" \"
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 }
107
101
\" \" \"
108
102
109
103
import argparse
@@ -114,7 +108,7 @@ def get_args():
114
108
\" \" \" Get command-line arguments\" \" \"
115
109
116
110
parser = argparse.ArgumentParser(
117
- description='{ args [ " purpose" ] } ',
111
+ description='{ args . purpose } ',
118
112
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
119
113
120
114
parser.add_argument('positional',
0 commit comments