builtin-notes: Minor (mostly parse_options-related) fixes
Use PARSE_OPT_NONEG to disallow --no-<option> for message, file, reedit-message and reuse-message. for which --no-<option> does not make sense. This also simplifies the code in the option-handling callbacks. Also, use strbuf_addch(... '\n') instead of strbuf_addstr(... "\n") in couple of places. Finally, improve the short-help by dividing the options into two OPT_GROUPs. Suggested-by: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Johan Herland <johan@herland.net> Acked-by: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johan Herland committed
Feb 25, 2010 at 01:48 UTC
43a61b841d261fe51ac64b55d654677b0cd93ad9
1 file changed
+17
-21
builtin-notes.c
+17
-21
@@ -171,12 +171,9 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
171
{
172
struct msg_arg *msg = opt->value;
173
174
- if (!arg)
175
- return -1;
176
-
174
strbuf_grow(&(msg->buf), strlen(arg) + 2);
175
if (msg->buf.len)
179
- strbuf_addstr(&(msg->buf), "\n");
176
+ strbuf_addch(&(msg->buf), '\n');
177
strbuf_addstr(&(msg->buf), arg);
178
stripspace(&(msg->buf), 0);
179
@@ -188,11 +185,8 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
185
{
186
struct msg_arg *msg = opt->value;
187
191
- if (!arg)
192
- return -1;
193
-
188
if (msg->buf.len)
195
- strbuf_addstr(&(msg->buf), "\n");
189
+ strbuf_addch(&(msg->buf), '\n');
190
if (!strcmp(arg, "-")) {
191
if (strbuf_read(&(msg->buf), 0, 1024) < 0)
192
die_errno("cannot read '%s'", arg);
@@ -212,11 +206,8 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
206
enum object_type type;
207
unsigned long len;
208
215
- if (!arg)
216
- return -1;
217
-
209
if (msg->buf.len)
219
- strbuf_addstr(&(msg->buf), "\n");
210
+ strbuf_addch(&(msg->buf), '\n');
211
212
if (get_sha1(arg, object))
213
die("Failed to resolve '%s' as a valid ref.", arg);
@@ -291,15 +282,20 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
282
int given_object = 0, i = 1, retval = 0;
283
struct msg_arg msg = { 0, 0, STRBUF_INIT };
284
struct option options[] = {
294
- OPT_GROUP("Notes options"),
295
- OPT_CALLBACK('m', "message", &msg, "MSG",
296
- "note contents as a string", parse_msg_arg),
297
- OPT_CALLBACK('F', "file", &msg, "FILE",
298
- "note contents in a file", parse_file_arg),
299
- OPT_CALLBACK('c', "reedit-message", &msg, "OBJECT",
300
- "reuse and edit specified note object", parse_reedit_arg),
301
- OPT_CALLBACK('C', "reuse-message", &msg, "OBJECT",
302
- "reuse specified note object", parse_reuse_arg),
285
+ OPT_GROUP("Notes contents options"),
286
+ { OPTION_CALLBACK, 'm', "message", &msg, "MSG",
287
+ "note contents as a string", PARSE_OPT_NONEG,
288
+ parse_msg_arg},
289
+ { OPTION_CALLBACK, 'F', "file", &msg, "FILE",
290
+ "note contents in a file", PARSE_OPT_NONEG,
291
+ parse_file_arg},
292
+ { OPTION_CALLBACK, 'c', "reedit-message", &msg, "OBJECT",
293
+ "reuse and edit specified note object", PARSE_OPT_NONEG,
294
+ parse_reedit_arg},
295
+ { OPTION_CALLBACK, 'C', "reuse-message", &msg, "OBJECT",
296
+ "reuse specified note object", PARSE_OPT_NONEG,
297
+ parse_reuse_arg},
298
+ OPT_GROUP("Other options"),
299
OPT_BOOLEAN('f', "force", &force, "replace existing notes"),
300
OPT_END()
301
};