11#!/usr/bin/env python3
22"""
33Author : Ken Youens-Clark <kyclark@gmail.com>
4- Date : 24 October 2018
54Purpose: Python program to write a Python program
65"""
76
@@ -19,18 +18,13 @@ def get_args():
1918 """Get arguments"""
2019
2120 parser = argparse .ArgumentParser (
22- description = 'Create Python argparse/simple program' ,
21+ description = 'Create Python argparse program' ,
2322 formatter_class = argparse .ArgumentDefaultsHelpFormatter )
2423
2524 defaults = get_defaults ()
2625
2726 parser .add_argument ('program' , help = 'Program name' , type = str )
2827
29- parser .add_argument ('-s' ,
30- '--simple' ,
31- help = 'Use simple format' ,
32- action = 'store_true' )
33-
3428 parser .add_argument ('-n' ,
3529 '--name' ,
3630 type = str ,
@@ -59,7 +53,10 @@ def get_args():
5953 args .program = args .program .strip ().replace ('-' , '_' )
6054
6155 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 } >'
6360
6461 return args
6562
@@ -72,22 +69,19 @@ def main():
7269 program = args .program
7370
7471 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] ' )
7673 if not answer .lower ().startswith ('y' ):
7774 print ('Will not overwrite. Bye!' )
7875 sys .exit ()
7976
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 ()))
8581
86- out_fh = open (program , 'w' )
87- out_fh .write (header + text )
88- out_fh .close ()
82+ print (text , file = open (program , 'wt' ), end = '' )
8983 subprocess .run (['chmod' , '+x' , program ])
90- print ('Done, see new script "{}."' . format ( program ) )
84+ print (f 'Done, see new script "{ program } ."' )
9185
9286
9387# --------------------------------------------------
@@ -102,47 +96,25 @@ def preamble(**args):
10296
10397
10498# --------------------------------------------------
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 """
130101
102+ return f"""#!/usr/bin/env python3
103+ \" \" \"
104+ Author : { args ['name' ]} { args ['email' ]}
105+ Date : { args ['date' ]}
106+ Purpose: { args ['purpose' ]}
107+ \" \" \"
131108
132- # --------------------------------------------------
133- def body (purpose ):
134- text = """
135109import argparse
136- import os
137- import sys
138110
139111
140112# --------------------------------------------------
141113def get_args():
142114 \" \" \" Get command-line arguments\" \" \"
143115
144116 parser = argparse.ArgumentParser(
145- description='{}',
117+ description='{ args [ "purpose" ] } ',
146118 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
147119
148120 parser.add_argument('positional',
@@ -189,18 +161,18 @@ def main():
189161 flag_arg = args.on
190162 pos_arg = args.positional
191163
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 }}"')
194166 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 }}"')
197169
198170
199171# --------------------------------------------------
200172if __name__ == '__main__':
201173 main()
202174"""
203- return text . format ( purpose )
175+
204176
205177# --------------------------------------------------
206178def get_defaults ():
0 commit comments