1
1
#!/usr/bin/env python3
2
2
"""
3
3
Author : Ken Youens-Clark <[email protected] >
4
- Date : 24 October 2018
5
4
Purpose: Python program to write a Python program
6
5
"""
7
6
@@ -19,18 +18,13 @@ def get_args():
19
18
"""Get arguments"""
20
19
21
20
parser = argparse .ArgumentParser (
22
- description = 'Create Python argparse/simple program' ,
21
+ description = 'Create Python argparse program' ,
23
22
formatter_class = argparse .ArgumentDefaultsHelpFormatter )
24
23
25
24
defaults = get_defaults ()
26
25
27
26
parser .add_argument ('program' , help = 'Program name' , type = str )
28
27
29
- parser .add_argument ('-s' ,
30
- '--simple' ,
31
- help = 'Use simple format' ,
32
- action = 'store_true' )
33
-
34
28
parser .add_argument ('-n' ,
35
29
'--name' ,
36
30
type = str ,
@@ -59,7 +53,10 @@ def get_args():
59
53
args .program = args .program .strip ().replace ('-' , '_' )
60
54
61
55
if not args .program :
62
- parser .error ('Not a usable filename "{}"' .format (args .program ))
56
+ parser .error (f'Not a usable filename "{ args .program } "' )
57
+
58
+ if args .email :
59
+ args .email = f'<{ args .email } >'
63
60
64
61
return args
65
62
@@ -72,22 +69,19 @@ def main():
72
69
program = args .program
73
70
74
71
if os .path .isfile (program ) and not args .force :
75
- answer = input ('"{}" exists. Overwrite? [yN] ' . format ( program ) )
72
+ answer = input (f '"{ program } " exists. Overwrite? [yN] ' )
76
73
if not answer .lower ().startswith ('y' ):
77
74
print ('Will not overwrite. Bye!' )
78
75
sys .exit ()
79
76
80
- header = preamble (name = args .name ,
81
- email = args .email ,
82
- purpose = args .purpose ,
83
- date = str (date .today ()))
84
- text = simple () if args .simple else body (args .purpose )
77
+ text = body (name = args .name ,
78
+ email = args .email ,
79
+ purpose = args .purpose ,
80
+ date = str (date .today ()))
85
81
86
- out_fh = open (program , 'w' )
87
- out_fh .write (header + text )
88
- out_fh .close ()
82
+ print (text , file = open (program , 'wt' ), end = '' )
89
83
subprocess .run (['chmod' , '+x' , program ])
90
- print ('Done, see new script "{}."' . format ( program ) )
84
+ print (f 'Done, see new script "{ program } ."' )
91
85
92
86
93
87
# --------------------------------------------------
@@ -102,47 +96,25 @@ def preamble(**args):
102
96
103
97
104
98
# --------------------------------------------------
105
- def simple ():
106
- return """
107
- import os
108
- import sys
109
-
110
-
111
- # --------------------------------------------------
112
- def main():
113
- \" \" \" Make a jazz noise here\" \" \"
114
-
115
- args = sys.argv[1:]
116
-
117
- if len(args) != 1:
118
- print('Usage: {} ARG'.format(os.path.basename(sys.argv[0])))
119
- sys.exit(1)
120
-
121
- arg = args[0]
122
-
123
- print('Arg is "{}"'.format(arg))
124
-
125
-
126
- # --------------------------------------------------
127
- if __name__ == '__main__':
128
- main()
129
- """
99
+ def body (** args ):
100
+ """ The program template """
130
101
102
+ return f"""#!/usr/bin/env python3
103
+ \" \" \"
104
+ Author : { args ['name' ]} { args ['email' ]}
105
+ Date : { args ['date' ]}
106
+ Purpose: { args ['purpose' ]}
107
+ \" \" \"
131
108
132
- # --------------------------------------------------
133
- def body (purpose ):
134
- text = """
135
109
import argparse
136
- import os
137
- import sys
138
110
139
111
140
112
# --------------------------------------------------
141
113
def get_args():
142
114
\" \" \" Get command-line arguments\" \" \"
143
115
144
116
parser = argparse.ArgumentParser(
145
- description='{}',
117
+ description='{ args [ "purpose" ] } ',
146
118
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
147
119
148
120
parser.add_argument('positional',
@@ -189,18 +161,18 @@ def main():
189
161
flag_arg = args.on
190
162
pos_arg = args.positional
191
163
192
- print('str_arg = "{{}}"'.format(str_arg) )
193
- print('int_arg = "{{}}"'.format(int_arg) )
164
+ print(f 'str_arg = "{{str_arg }}"')
165
+ print(f 'int_arg = "{{int_arg }}"')
194
166
print('file_arg = "{{}}"'.format(file_arg.name if file_arg else ''))
195
- print('flag_arg = "{{}}"'.format(flag_arg) )
196
- print('positional = "{{}}"'.format(pos_arg) )
167
+ print(f 'flag_arg = "{{flag_arg }}"')
168
+ print(f 'positional = "{{pos_arg }}"')
197
169
198
170
199
171
# --------------------------------------------------
200
172
if __name__ == '__main__':
201
173
main()
202
174
"""
203
- return text . format ( purpose )
175
+
204
176
205
177
# --------------------------------------------------
206
178
def get_defaults ():
0 commit comments