Skip to content

Commit 67fac31

Browse files
committed
change name
1 parent 4fab192 commit 67fac31

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

bin/new.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Ken Youens-Clark <kyclark@gmail.com>
4+
Date : 24 October 2018
5+
Purpose: Python program to write a Python program
6+
"""
7+
8+
import argparse
9+
import os
10+
import subprocess
11+
import sys
12+
from datetime import date
13+
from dire import die
14+
15+
16+
# --------------------------------------------------
17+
def get_args():
18+
"""Get arguments"""
19+
20+
parser = argparse.ArgumentParser(
21+
description='Create Python argparse/simple program',
22+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
23+
24+
parser.add_argument('program', help='Program name', type=str)
25+
26+
parser.add_argument('-s',
27+
'--simple',
28+
help='Use simple format',
29+
action='store_true')
30+
31+
parser.add_argument('-f',
32+
'--force',
33+
help='Overwrite existing',
34+
action='store_true')
35+
36+
return parser.parse_args()
37+
38+
39+
# --------------------------------------------------
40+
def main():
41+
"""Make a jazz noise here"""
42+
43+
args = get_args()
44+
out_file = args.program.strip().replace('-', '_')
45+
46+
if not out_file: die('Not a usable filename "{}"'.format(out_file))
47+
48+
if not out_file.endswith('.py'): out_file += '.py'
49+
50+
if os.path.isfile(out_file) and not args.force:
51+
answer = input('"{}" exists. Overwrite? [yN] '.format(out_file))
52+
if not answer.lower().startswith('y'):
53+
print('Will not overwrite. Bye!')
54+
sys.exit()
55+
56+
out_fh = open(out_file, 'w')
57+
preamble = PREAMBLE.format(os.getenv('USER'), str(date.today()))
58+
text = SIMPLE if args.simple else ARGPARSE
59+
60+
out_fh.write(preamble)
61+
out_fh.write(text)
62+
subprocess.run(['chmod', '+x', out_file])
63+
print('Done, see new script "{}."'.format(out_file))
64+
65+
66+
# --------------------------------------------------
67+
PREAMBLE = """#!/usr/bin/env python3
68+
\"\"\"
69+
Author : {}
70+
Date : {}
71+
Purpose: Rock the Casbah
72+
\"\"\"
73+
"""
74+
75+
# --------------------------------------------------
76+
SIMPLE = """
77+
import os
78+
import sys
79+
80+
81+
# --------------------------------------------------
82+
def main():
83+
\"\"\"Make a jazz noise here\"\"\"
84+
85+
args = sys.argv[1:]
86+
87+
if len(args) != 1:
88+
print('Usage: {} ARG'.format(os.path.basename(sys.argv[0])))
89+
sys.exit(1)
90+
91+
arg = args[0]
92+
93+
print('Arg is "{}"'.format(arg))
94+
95+
96+
# --------------------------------------------------
97+
if __name__ == '__main__':
98+
main()
99+
"""
100+
101+
# --------------------------------------------------
102+
ARGPARSE = """
103+
import argparse
104+
import os
105+
import sys
106+
107+
108+
# --------------------------------------------------
109+
def get_args():
110+
\"\"\"Get command-line arguments\"\"\"
111+
112+
parser = argparse.ArgumentParser(
113+
description='Argparse Python script',
114+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
115+
116+
parser.add_argument('positional',
117+
metavar='str',
118+
help='A positional argument')
119+
120+
parser.add_argument('-a',
121+
'--arg',
122+
help='A named string argument',
123+
metavar='str',
124+
type=str,
125+
default='')
126+
127+
parser.add_argument('-i',
128+
'--int',
129+
help='A named integer argument',
130+
metavar='int',
131+
type=int,
132+
default=0)
133+
134+
parser.add_argument('-f',
135+
'--flag',
136+
help='A boolean flag',
137+
action='store_true')
138+
139+
return parser.parse_args()
140+
141+
142+
# --------------------------------------------------
143+
def main():
144+
\"\"\"Make a jazz noise here\"\"\"
145+
146+
args = get_args()
147+
str_arg = args.arg
148+
int_arg = args.int
149+
flag_arg = args.flag
150+
pos_arg = args.positional
151+
152+
print('str_arg = "{}"'.format(str_arg))
153+
print('int_arg = "{}"'.format(int_arg))
154+
print('flag_arg = "{}"'.format(flag_arg))
155+
print('positional = "{}"'.format(pos_arg))
156+
157+
158+
# --------------------------------------------------
159+
if __name__ == '__main__':
160+
main()
161+
"""
162+
163+
# --------------------------------------------------
164+
if __name__ == '__main__':
165+
main()

0 commit comments

Comments
 (0)