73
73
#
74
74
# Please enter the relevant GitHub issue number here:
75
75
#
76
- .. gh-issue:
76
+ .. gh-issue: {gh}
77
77
78
78
#
79
79
# Uncomment one of these "section:" lines to specify which section
90
90
#.. section: IDLE
91
91
#.. section: Tools/Demos
92
92
#.. section: C API
93
+ {section}
93
94
94
95
# Write your Misc/NEWS.d entry below. It should be a simple ReST paragraph.
95
96
# Don't start with "- Issue #<n>: " or "- gh-issue-<n>: " or that sort of stuff.
@@ -451,6 +452,8 @@ def parse(self, text, *, metadata=None, filename="input"):
451
452
line_number = None
452
453
453
454
def throw (s ):
455
+ nonlocal filename
456
+ nonlocal line_number
454
457
raise BlurbError (f"Error in { filename } :{ line_number } :\n { s } " )
455
458
456
459
def finish_entry ():
@@ -870,7 +873,7 @@ def find_editor():
870
873
871
874
872
875
@subcommand
873
- def add ():
876
+ def add (* , gh = "" , section = "" ):
874
877
"""
875
878
Add a blurb (a Misc/NEWS.d/next entry) to the current CPython repo.
876
879
"""
@@ -881,24 +884,23 @@ def add():
881
884
os .close (handle )
882
885
atexit .register (lambda : os .unlink (tmp_path ))
883
886
884
- def init_tmp_with_template ():
885
- with open (tmp_path , "wt" , encoding = "utf-8" ) as file :
886
- # hack:
887
- # my editor likes to strip trailing whitespace from lines.
888
- # normally this is a good idea. but in the case of the template
889
- # it's unhelpful.
890
- # so, manually ensure there's a space at the end of the gh-issue line.
891
- text = template
892
-
893
- issue_line = ".. gh-issue:"
894
- without_space = "\n " + issue_line + "\n "
895
- with_space = "\n " + issue_line + " \n "
896
- if without_space not in text :
897
- sys .exit ("Can't find gh-issue line to ensure there's a space on the end!" )
898
- text = text .replace (without_space , with_space )
899
- file .write (text )
887
+ if gh :
888
+ try :
889
+ int (gh )
890
+ except ValueError :
891
+ error (f"blurb add --gh argument { gh } is not a valid integer!" )
892
+
893
+ if section :
894
+ if section not in sections :
895
+ error (
896
+ f"blurb add --section argument { section !r} is not a valid section!"
897
+ " Use one of:\n " + "\n " .join (sections )
898
+ )
900
899
901
- init_tmp_with_template ()
900
+ section = f".. section: { section } \n "
901
+
902
+ with open (tmp_path , "wt" , encoding = "utf-8" ) as file :
903
+ file .write (template .format (gh = gh , section = section ))
902
904
903
905
# We need to be clever about EDITOR.
904
906
# On the one hand, it might be a legitimate path to an
@@ -1221,36 +1223,55 @@ def main():
1221
1223
kwargs = {}
1222
1224
for name , p in inspect .signature (fn ).parameters .items ():
1223
1225
if p .kind == inspect .Parameter .KEYWORD_ONLY :
1224
- assert isinstance (p .default , bool ), "blurb command-line processing only handles boolean options"
1226
+ assert isinstance (
1227
+ p .default , (bool , str )
1228
+ ), "blurb command-line processing only handles boolean options"
1225
1229
kwargs [name ] = p .default
1226
1230
short_options [name [0 ]] = name
1227
1231
long_options [name ] = name
1228
1232
1229
1233
filtered_args = []
1230
1234
done_with_options = False
1235
+ needs_oparg = None
1231
1236
1232
- def handle_option (s , dict ):
1237
+ def handle_option (s , dict , fn_name ):
1238
+ nonlocal needs_oparg
1233
1239
name = dict .get (s , None )
1234
1240
if not name :
1235
- sys .exit (f'blurb: Unknown option for { subcommand } : "{ s } "' )
1236
- kwargs [name ] = not kwargs [name ]
1241
+ sys .exit (f'blurb: Unknown option for { fn_name } : "{ s } "' )
1242
+
1243
+ value = kwargs [name ]
1244
+ if isinstance (value , bool ):
1245
+ kwargs [name ] = not value
1246
+ else :
1247
+ needs_oparg = name
1237
1248
1238
1249
# print(f"short_options {short_options} long_options {long_options}")
1239
1250
for a in args :
1251
+ if needs_oparg :
1252
+ kwargs [needs_oparg ] = a
1253
+ needs_oparg = None
1254
+ continue
1255
+
1240
1256
if done_with_options :
1241
1257
filtered_args .append (a )
1242
1258
continue
1259
+
1243
1260
if a .startswith ('-' ):
1244
1261
if a == "--" :
1245
1262
done_with_options = True
1246
1263
elif a .startswith ("--" ):
1247
- handle_option (a [2 :], long_options )
1264
+ handle_option (a [2 :], long_options , fn . __name__ )
1248
1265
else :
1249
1266
for s in a [1 :]:
1250
- handle_option (s , short_options )
1267
+ handle_option (s , short_options , fn . __name__ )
1251
1268
continue
1252
1269
filtered_args .append (a )
1253
1270
1271
+ if needs_oparg :
1272
+ sys .exit (
1273
+ f"Error: blurb: { fn_name } { needs_oparg } most be followed by an option argument"
1274
+ )
1254
1275
1255
1276
sys .exit (fn (* filtered_args , ** kwargs ))
1256
1277
except TypeError as e :
0 commit comments