Commit dfea0713e04fa4f9f129b85c2262e7d0420aaaf3

Patrick Steinhardt 2019-11-28T13:51:40

Merge pull request #5272 from tiennou/examples/cli-ification Various examples shape-ups

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 05e344d..8cc72b3 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,7 +1,7 @@
 INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
 INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
 
-FILE(GLOB LG2_SOURCES *.c)
+FILE(GLOB LG2_SOURCES *.c *.h)
 ADD_EXECUTABLE(lg2 ${LG2_SOURCES})
 SET_TARGET_PROPERTIES(lg2 PROPERTIES C_STANDARD 90)
 
diff --git a/examples/add.c b/examples/add.c
index 14e69e9..6e3c239 100644
--- a/examples/add.c
+++ b/examples/add.c
@@ -13,7 +13,6 @@
  */
 
 #include "common.h"
-#include <assert.h>
 
 /**
  * The following example demonstrates how to add files with libgit2.
@@ -27,48 +26,50 @@
  *   -u/--update: update the index instead of adding to it.
  */
 
-enum print_options {
-	SKIP = 1,
-	VERBOSE = 2,
-	UPDATE = 4,
+enum index_mode {
+	INDEX_NONE,
+	INDEX_ADD,
 };
 
-struct print_payload {
-	enum print_options options;
+struct index_options {
+	int dry_run;
+	int verbose;
 	git_repository *repo;
+	enum index_mode mode;
+	int add_update;
 };
 
 /* Forward declarations for helpers */
-static void parse_opts(int *options, int *count, int argc, char *argv[]);
-void init_array(git_strarray *array, int argc, char **argv);
+static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args);
 int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
 
-int lg2_add(git_repository *repo, int argc, char** argv)
+int lg2_add(git_repository *repo, int argc, char **argv)
 {
 	git_index_matched_path_cb matched_cb = NULL;
 	git_index *index;
 	git_strarray array = {0};
-	int options = 0, count = 0;
-	struct print_payload payload = {0};
+	struct index_options options;
+	struct args_info args = ARGS_INFO_INIT;
 
-	parse_opts(&options, &count, argc, argv);
-	init_array(&array, argc-count, argv+count);
+	/* Parse the options & arguments. */
+	parse_opts(NULL, &options, &args);
+	strarray_from_args(&array, &args);
 
+	/* Grab the repository's index. */
 	check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
 
 	/* Setup a callback if the requested options need it */
-	if ((options & VERBOSE) || (options & SKIP)) {
+	if (options.verbose || options.dry_run) {
 		matched_cb = &print_matched_cb;
 	}
 
-	/* Perform the requested action with the index and files */
-	payload.options = options;
-	payload.repo = repo;
+	options.repo = repo;
 
-	if (options & UPDATE) {
-		git_index_update_all(index, &array, matched_cb, &payload);
+	/* Perform the requested action with the index and files */
+	if (options.add_update) {
+		git_index_update_all(index, &array, matched_cb, &options);
 	} else {
-		git_index_add_all(index, &array, 0, matched_cb, &payload);
+		git_index_add_all(index, &array, 0, matched_cb, &options);
 	}
 
 	/* Cleanup memory */
@@ -85,15 +86,14 @@ int lg2_add(git_repository *repo, int argc, char** argv)
  */
 int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
 {
-	struct print_payload p = *(struct print_payload*)(payload);
+	struct index_options *opts = (struct index_options *)(payload);
 	int ret;
 	unsigned status;
 	(void)matched_pathspec;
 
 	/* Get the file status */
-	if (git_status_file(&status, p.repo, path)) {
+	if (git_status_file(&status, opts->repo, path) < 0)
 		return -1;
-	}
 
 	if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) {
 		printf("add '%s'\n", path);
@@ -102,9 +102,8 @@ int print_matched_cb(const char *path, const char *matched_pathspec, void *paylo
 		ret = 1;
 	}
 
-	if ((p.options & SKIP)) {
+	if (opts->dry_run)
 		ret = 1;
-	}
 
 	return ret;
 }
@@ -133,33 +132,39 @@ void print_usage(void)
 	exit(1);
 }
 
-static void parse_opts(int *options, int *count, int argc, char *argv[])
+static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args)
 {
-	int i;
+	if (args->argc <= 1)
+		print_usage();
 
-	for (i = 1; i < argc; ++i) {
-		if (argv[i][0] != '-')
-			break;
-		else if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v"))
-			*options |= VERBOSE;
-		else if (!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n"))
-			*options |= SKIP;
-		else if (!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u"))
-			*options |= UPDATE;
-		else if (!strcmp(argv[i], "-h")) {
+	for (args->pos = 1; args->pos < args->argc; ++args->pos) {
+		const char *curr = args->argv[args->pos];
+
+		if (curr[0] != '-') {
+			if (!strcmp("add", curr)) {
+				opts->mode = INDEX_ADD;
+				continue;
+			} else if (opts->mode == INDEX_NONE) {
+				fprintf(stderr, "missing command: %s", curr);
+				print_usage();
+				break;
+			} else {
+				/* We might be looking at a filename */
+				break;
+			}
+		} else if (match_bool_arg(&opts->verbose, args, "--verbose") ||
+				   match_bool_arg(&opts->dry_run, args, "--dry-run") ||
+				   match_str_arg(repo_path, args, "--git-dir") ||
+				   (opts->mode == INDEX_ADD && match_bool_arg(&opts->add_update, args, "--update"))) {
+			continue;
+		} else if (match_bool_arg(NULL, args, "--help")) {
 			print_usage();
 			break;
-		} else if (!strcmp(argv[i], "--")) {
-			i++;
+		} else if (match_arg_separator(args)) {
 			break;
 		} else {
-			fprintf(stderr, "Unsupported option %s.\n", argv[i]);
+			fprintf(stderr, "Unsupported option %s.\n", curr);
 			print_usage();
 		}
 	}
-
-	if (argc <= i)
-		print_usage();
-
-	*count = i;
 }
diff --git a/examples/args.c b/examples/args.c
new file mode 100644
index 0000000..533e157
--- /dev/null
+++ b/examples/args.c
@@ -0,0 +1,197 @@
+#include "common.h"
+#include "args.h"
+
+size_t is_prefixed(const char *str, const char *pfx)
+{
+	size_t len = strlen(pfx);
+	return strncmp(str, pfx, len) ? 0 : len;
+}
+
+int optional_str_arg(
+	const char **out, struct args_info *args, const char *opt, const char *def)
+{
+	const char *found = args->argv[args->pos];
+	size_t len = is_prefixed(found, opt);
+
+	if (!len)
+		return 0;
+
+	if (!found[len]) {
+		if (args->pos + 1 == args->argc) {
+			*out = def;
+			return 1;
+		}
+		args->pos += 1;
+		*out = args->argv[args->pos];
+		return 1;
+	}
+
+	if (found[len] == '=') {
+		*out = found + len + 1;
+		return 1;
+	}
+
+	return 0;
+}
+
+int match_str_arg(
+	const char **out, struct args_info *args, const char *opt)
+{
+	const char *found = args->argv[args->pos];
+	size_t len = is_prefixed(found, opt);
+
+	if (!len)
+		return 0;
+
+	if (!found[len]) {
+		if (args->pos + 1 == args->argc)
+			fatal("expected value following argument", opt);
+		args->pos += 1;
+		*out = args->argv[args->pos];
+		return 1;
+	}
+
+	if (found[len] == '=') {
+		*out = found + len + 1;
+		return 1;
+	}
+
+	return 0;
+}
+
+static const char *match_numeric_arg(struct args_info *args, const char *opt)
+{
+	const char *found = args->argv[args->pos];
+	size_t len = is_prefixed(found, opt);
+
+	if (!len)
+		return NULL;
+
+	if (!found[len]) {
+		if (args->pos + 1 == args->argc)
+			fatal("expected numeric value following argument", opt);
+		args->pos += 1;
+		found = args->argv[args->pos];
+	} else {
+		found = found + len;
+		if (*found == '=')
+			found++;
+	}
+
+	return found;
+}
+
+int match_uint16_arg(
+	uint16_t *out, struct args_info *args, const char *opt)
+{
+	const char *found = match_numeric_arg(args, opt);
+	uint16_t val;
+	char *endptr = NULL;
+
+	if (!found)
+		return 0;
+
+	val = (uint16_t)strtoul(found, &endptr, 0);
+	if (!endptr || *endptr != '\0')
+		fatal("expected number after argument", opt);
+
+	if (out)
+		*out = val;
+	return 1;
+}
+
+int match_uint32_arg(
+	uint32_t *out, struct args_info *args, const char *opt)
+{
+	const char *found = match_numeric_arg(args, opt);
+	uint16_t val;
+	char *endptr = NULL;
+
+	if (!found)
+		return 0;
+
+	val = (uint32_t)strtoul(found, &endptr, 0);
+	if (!endptr || *endptr != '\0')
+		fatal("expected number after argument", opt);
+
+	if (out)
+		*out = val;
+	return 1;
+}
+
+static int match_int_internal(
+	int *out, const char *str, int allow_negative, const char *opt)
+{
+	char *endptr = NULL;
+	int	  val = (int)strtol(str, &endptr, 10);
+
+	if (!endptr || *endptr != '\0')
+		fatal("expected number", opt);
+	else if (val < 0 && !allow_negative)
+		fatal("negative values are not allowed", opt);
+
+	if (out)
+		*out = val;
+
+	return 1;
+}
+
+int match_bool_arg(int *out, struct args_info *args, const char *opt)
+{
+	const char *found = args->argv[args->pos];
+
+	if (!strcmp(found, opt)) {
+		*out = 1;
+		return 1;
+	}
+
+	if (!strncmp(found, "--no-", strlen("--no-")) &&
+	    !strcmp(found + strlen("--no-"), opt + 2)) {
+		*out = 0;
+		return 1;
+	}
+
+	*out = -1;
+	return 0;
+}
+
+int is_integer(int *out, const char *str, int allow_negative)
+{
+	return match_int_internal(out, str, allow_negative, NULL);
+}
+
+int match_int_arg(
+	int *out, struct args_info *args, const char *opt, int allow_negative)
+{
+	const char *found = match_numeric_arg(args, opt);
+	if (!found)
+		return 0;
+	return match_int_internal(out, found, allow_negative, opt);
+}
+
+int match_arg_separator(struct args_info *args)
+{
+	if (args->opts_done)
+		return 1;
+
+	if (strcmp(args->argv[args->pos], "--") != 0)
+		return 0;
+
+	args->opts_done = 1;
+	args->pos++;
+	return 1;
+}
+
+void strarray_from_args(git_strarray *array, struct args_info *args)
+{
+	size_t i;
+
+	array->count = args->argc - args->pos;
+	array->strings = calloc(array->count, sizeof(char *));
+	assert(array->strings != NULL);
+
+	for (i = 0; args->pos < args->argc; ++args->pos) {
+		array->strings[i++] = args->argv[args->pos];
+	}
+	args->pos = args->argc;
+}
diff --git a/examples/args.h b/examples/args.h
new file mode 100644
index 0000000..d626f98
--- /dev/null
+++ b/examples/args.h
@@ -0,0 +1,90 @@
+#ifndef INCLUDE_examples_args_h__
+#define INCLUDE_examples_args_h__
+
+/**
+ * Argument-processing helper structure
+ */
+struct args_info {
+	int    argc;
+	char **argv;
+	int    pos;
+	int    opts_done : 1; /**< Did we see a -- separator */
+};
+#define ARGS_INFO_INIT { argc, argv, 0, 0 }
+#define ARGS_CURRENT(args) args->argv[args->pos]
+
+/**
+ * Check if a string has the given prefix.  Returns 0 if not prefixed
+ * or the length of the prefix if it is.
+ */
+extern size_t is_prefixed(const char *str, const char *pfx);
+
+/**
+ * Match an integer string, returning 1 if matched, 0 if not.
+ */
+extern int is_integer(int *out, const char *str, int allow_negative);
+
+/**
+ * Check current `args` entry against `opt` string.  If it matches
+ * exactly, take the next arg as a string; if it matches as a prefix with
+ * an equal sign, take the remainder as a string; if value not supplied,
+ * default value `def` will be given. otherwise return 0.
+ */
+extern int optional_str_arg(
+	const char **out, struct args_info *args, const char *opt, const char *def);
+
+/**
+ * Check current `args` entry against `opt` string.  If it matches
+ * exactly, take the next arg as a string; if it matches as a prefix with
+ * an equal sign, take the remainder as a string; otherwise return 0.
+ */
+extern int match_str_arg(
+	const char **out, struct args_info *args, const char *opt);
+
+/**
+ * Check current `args` entry against `opt` string parsing as uint16.  If
+ * `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
+ * is a prefix (equal sign optional), take the remainder of the arg as a
+ * uint16_t value; otherwise return 0.
+ */
+extern int match_uint16_arg(
+	uint16_t *out, struct args_info *args, const char *opt);
+
+/**
+ * Check current `args` entry against `opt` string parsing as uint32.  If
+ * `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
+ * is a prefix (equal sign optional), take the remainder of the arg as a
+ * uint32_t value; otherwise return 0.
+ */
+extern int match_uint32_arg(
+	uint32_t *out, struct args_info *args, const char *opt);
+
+/**
+ * Check current `args` entry against `opt` string parsing as int.  If
+ * `opt` matches exactly, take the next arg as an int value; if it matches
+ * as a prefix (equal sign optional), take the remainder of the arg as a
+ * int value; otherwise return 0.
+ */
+extern int match_int_arg(
+	int *out, struct args_info *args, const char *opt, int allow_negative);
+
+/**
+ * Check current `args` entry against a "bool" `opt` (ie. --[no-]progress).
+ * If `opt` matches positively, out will be set to 1, or if `opt` matches
+ * negatively, out will be set to 0, and in both cases 1 will be returned.
+ * If neither the positive or the negative form of opt matched, out will be -1,
+ * and 0 will be returned.
+ */
+extern int match_bool_arg(int *out, struct args_info *args, const char *opt);
+
+/**
+ * Check if we're processing past the single -- separator
+ */
+extern int match_arg_separator(struct args_info *args);
+
+/**
+ * Consume all remaining arguments in a git_strarray
+ */
+extern void strarray_from_args(git_strarray *array, struct args_info *args);
+
+#endif
diff --git a/examples/blame.c b/examples/blame.c
index 469dbd1..49350fc 100644
--- a/examples/blame.c
+++ b/examples/blame.c
@@ -19,7 +19,7 @@
  * simulate the output of `git blame` and a few of its command line arguments.
  */
 
-struct opts {
+struct blame_opts {
 	char *path;
 	char *commitspec;
 	int C;
@@ -28,14 +28,14 @@ struct opts {
 	int end_line;
 	int F;
 };
-static void parse_opts(struct opts *o, int argc, char *argv[]);
+static void parse_opts(struct blame_opts *o, int argc, char *argv[]);
 
 int lg2_blame(git_repository *repo, int argc, char *argv[])
 {
 	int line, break_on_null_hunk;
 	git_object_size_t i, rawsize;
 	char spec[1024] = {0};
-	struct opts o = {0};
+	struct blame_opts o = {0};
 	const char *rawdata;
 	git_revspec revspec = {0};
 	git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT;
@@ -143,7 +143,7 @@ static void usage(const char *msg, const char *arg)
 }
 
 /** Parse the arguments. */
-static void parse_opts(struct opts *o, int argc, char *argv[])
+static void parse_opts(struct blame_opts *o, int argc, char *argv[])
 {
 	int i;
 	char *bare_args[3] = {0};
diff --git a/examples/cat-file.c b/examples/cat-file.c
index 9b14a70..b81e9a8 100644
--- a/examples/cat-file.c
+++ b/examples/cat-file.c
@@ -102,27 +102,28 @@ static void show_tag(const git_tag *tag)
 		printf("\n%s\n", git_tag_message(tag));
 }
 
-enum {
+typedef enum {
 	SHOW_TYPE = 1,
 	SHOW_SIZE = 2,
 	SHOW_NONE = 3,
 	SHOW_PRETTY = 4
-};
+} catfile_mode;
 
 /* Forward declarations for option-parsing helper */
-struct opts {
+struct catfile_options {
 	const char *dir;
 	const char *rev;
-	int action;
+	catfile_mode action;
 	int verbose;
 };
-static void parse_opts(struct opts *o, int argc, char *argv[]);
+
+static void parse_opts(struct catfile_options *o, int argc, char *argv[]);
 
 
 /** Entry point for this command */
 int lg2_cat_file(git_repository *repo, int argc, char *argv[])
 {
-	struct opts o = { ".", NULL, 0, 0 };
+	struct catfile_options o = { ".", NULL, 0, 0 };
 	git_object *obj = NULL;
 	char oidstr[GIT_OID_HEXSZ + 1];
 
@@ -201,7 +202,7 @@ static void usage(const char *message, const char *arg)
 }
 
 /** Parse the command-line options taken from git */
-static void parse_opts(struct opts *o, int argc, char *argv[])
+static void parse_opts(struct catfile_options *o, int argc, char *argv[])
 {
 	struct args_info args = ARGS_INFO_INIT;
 
diff --git a/examples/checkout.c b/examples/checkout.c
index d552eec..70b5185 100644
--- a/examples/checkout.c
+++ b/examples/checkout.c
@@ -13,7 +13,6 @@
  */
 
 #include "common.h"
-#include <assert.h>
 
 /* Define the printf format specifer to use for size_t output */
 #if defined(_MSC_VER) || defined(__MINGW32__)
@@ -66,7 +65,7 @@ static void parse_options(const char **repo_path, checkout_options *opts, struct
 		const char *curr = args->argv[args->pos];
 		int bool_arg;
 
-		if (strcmp(curr, "--") == 0) {
+		if (match_arg_separator(args)) {
 			break;
 		} else if (!strcmp(curr, "--force")) {
 			opts->force = 1;
@@ -191,11 +190,7 @@ int lg2_checkout(git_repository *repo, int argc, char **argv)
 		goto cleanup;
 	}
 
-	if (args.pos >= args.argc) {
-		fprintf(stderr, "unhandled\n");
-		err = -1;
-		goto cleanup;
-	} else if (!strcmp("--", args.argv[args.pos])) {
+	if (match_arg_separator(&args)) {
 		/**
 		 * Try to checkout the given path
 		 */
diff --git a/examples/common.c b/examples/common.c
index 22807e1..30f6cdc 100644
--- a/examples/common.c
+++ b/examples/common.c
@@ -12,21 +12,14 @@
  * <http://creativecommons.org/publicdomain/zero/1.0/>.
  */
 
-#include <assert.h>
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#ifdef _WIN32
-# include <io.h>
-#else
-# include <fcntl.h>
+
+#include "common.h"
+
+#ifndef _WIN32
 # include <unistd.h>
 #endif
-#include <string.h>
 #include <errno.h>
 
-#include "common.h"
-
 void check_lg2(int error, const char *message, const char *extra)
 {
 	const git_error *lg2err;
@@ -60,174 +53,6 @@ void fatal(const char *message, const char *extra)
 	exit(1);
 }
 
-size_t is_prefixed(const char *str, const char *pfx)
-{
-	size_t len = strlen(pfx);
-	return strncmp(str, pfx, len) ? 0 : len;
-}
-
-int optional_str_arg(
-	const char **out, struct args_info *args, const char *opt, const char *def)
-{
-	const char *found = args->argv[args->pos];
-	size_t len = is_prefixed(found, opt);
-
-	if (!len)
-		return 0;
-
-	if (!found[len]) {
-		if (args->pos + 1 == args->argc) {
-			*out = def;
-			return 1;
-		}
-		args->pos += 1;
-		*out = args->argv[args->pos];
-		return 1;
-	}
-
-	if (found[len] == '=') {
-		*out = found + len + 1;
-		return 1;
-	}
-
-	return 0;
-}
-
-int match_str_arg(
-	const char **out, struct args_info *args, const char *opt)
-{
-	const char *found = args->argv[args->pos];
-	size_t len = is_prefixed(found, opt);
-
-	if (!len)
-		return 0;
-
-	if (!found[len]) {
-		if (args->pos + 1 == args->argc)
-			fatal("expected value following argument", opt);
-		args->pos += 1;
-		*out = args->argv[args->pos];
-		return 1;
-	}
-
-	if (found[len] == '=') {
-		*out = found + len + 1;
-		return 1;
-	}
-
-	return 0;
-}
-
-static const char *match_numeric_arg(struct args_info *args, const char *opt)
-{
-	const char *found = args->argv[args->pos];
-	size_t len = is_prefixed(found, opt);
-
-	if (!len)
-		return NULL;
-
-	if (!found[len]) {
-		if (args->pos + 1 == args->argc)
-			fatal("expected numeric value following argument", opt);
-		args->pos += 1;
-		found = args->argv[args->pos];
-	} else {
-		found = found + len;
-		if (*found == '=')
-			found++;
-	}
-
-	return found;
-}
-
-int match_uint16_arg(
-	uint16_t *out, struct args_info *args, const char *opt)
-{
-	const char *found = match_numeric_arg(args, opt);
-	uint16_t val;
-	char *endptr = NULL;
-
-	if (!found)
-		return 0;
-
-	val = (uint16_t)strtoul(found, &endptr, 0);
-	if (!endptr || *endptr != '\0')
-		fatal("expected number after argument", opt);
-
-	if (out)
-		*out = val;
-	return 1;
-}
-
-int match_uint32_arg(
-	uint32_t *out, struct args_info *args, const char *opt)
-{
-	const char *found = match_numeric_arg(args, opt);
-	uint16_t val;
-	char *endptr = NULL;
-
-	if (!found)
-		return 0;
-
-	val = (uint32_t)strtoul(found, &endptr, 0);
-	if (!endptr || *endptr != '\0')
-		fatal("expected number after argument", opt);
-
-	if (out)
-		*out = val;
-	return 1;
-}
-
-static int match_int_internal(
-	int *out, const char *str, int allow_negative, const char *opt)
-{
-	char *endptr = NULL;
-	int	  val = (int)strtol(str, &endptr, 10);
-
-	if (!endptr || *endptr != '\0')
-		fatal("expected number", opt);
-	else if (val < 0 && !allow_negative)
-		fatal("negative values are not allowed", opt);
-
-	if (out)
-		*out = val;
-
-	return 1;
-}
-
-int match_bool_arg(int *out, struct args_info *args, const char *opt)
-{
-	const char *found = args->argv[args->pos];
-
-	if (!strcmp(found, opt)) {
-		*out = 1;
-		return 1;
-	}
-
-	if (!strncmp(found, "--no-", strlen("--no-")) &&
-	    !strcmp(found + strlen("--no-"), opt + 2)) {
-		*out = 0;
-		return 1;
-	}
-
-	*out = -1;
-	return 0;
-}
-
-int is_integer(int *out, const char *str, int allow_negative)
-{
-	return match_int_internal(out, str, allow_negative, NULL);
-}
-
-int match_int_arg(
-	int *out, struct args_info *args, const char *opt, int allow_negative)
-{
-	const char *found = match_numeric_arg(args, opt);
-	if (!found)
-		return 0;
-	return match_int_internal(out, found, allow_negative, opt);
-}
-
 int diff_output(
 	const git_diff_delta *d,
 	const git_diff_hunk *h,
diff --git a/examples/common.h b/examples/common.h
index 2ad897b..5a029b4 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -11,7 +11,10 @@
  * with this software. If not, see
  * <http://creativecommons.org/publicdomain/zero/1.0/>.
  */
+#ifndef INCLUDE_examples_common_h__
+#define INCLUDE_examples_common_h__
 
+#include <assert.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdio.h>
@@ -49,6 +52,8 @@
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
 #define UNUSED(x) (void)(x)
 
+#include "args.h"
+
 extern int lg2_add(git_repository *repo, int argc, char **argv);
 extern int lg2_blame(git_repository *repo, int argc, char **argv);
 extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
@@ -94,77 +99,6 @@ extern char *read_file(const char *path);
 extern void fatal(const char *message, const char *extra);
 
 /**
- * Check if a string has the given prefix.  Returns 0 if not prefixed
- * or the length of the prefix if it is.
- */
-extern size_t is_prefixed(const char *str, const char *pfx);
-
-/**
- * Match an integer string, returning 1 if matched, 0 if not.
- */
-extern int is_integer(int *out, const char *str, int allow_negative);
-
-struct args_info {
-	int    argc;
-	char **argv;
-	int    pos;
-};
-#define ARGS_INFO_INIT { argc, argv, 0 }
-
-/**
- * Check current `args` entry against `opt` string.  If it matches
- * exactly, take the next arg as a string; if it matches as a prefix with
- * an equal sign, take the remainder as a string; if value not supplied,
- * default value `def` will be given. otherwise return 0.
- */
-extern int optional_str_arg(
-	const char **out, struct args_info *args, const char *opt, const char *def);
-
-/**
- * Check current `args` entry against `opt` string.  If it matches
- * exactly, take the next arg as a string; if it matches as a prefix with
- * an equal sign, take the remainder as a string; otherwise return 0.
- */
-extern int match_str_arg(
-	const char **out, struct args_info *args, const char *opt);
-
-/**
- * Check current `args` entry against `opt` string parsing as uint16.  If
- * `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
- * is a prefix (equal sign optional), take the remainder of the arg as a
- * uint16_t value; otherwise return 0.
- */
-extern int match_uint16_arg(
-	uint16_t *out, struct args_info *args, const char *opt);
-
-/**
- * Check current `args` entry against `opt` string parsing as uint32.  If
- * `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
- * is a prefix (equal sign optional), take the remainder of the arg as a
- * uint32_t value; otherwise return 0.
- */
-extern int match_uint32_arg(
-	uint32_t *out, struct args_info *args, const char *opt);
-
-/**
- * Check current `args` entry against `opt` string parsing as int.  If
- * `opt` matches exactly, take the next arg as an int value; if it matches
- * as a prefix (equal sign optional), take the remainder of the arg as a
- * int value; otherwise return 0.
- */
-extern int match_int_arg(
-	int *out, struct args_info *args, const char *opt, int allow_negative);
-
-/**
- * Check current `args` entry against a "bool" `opt` (ie. --[no-]progress).
- * If `opt` matches positively, out will be set to 1, or if `opt` matches
- * negatively, out will be set to 0, and in both cases 1 will be returned.
- * If neither the positive or the negative form of opt matched, out will be -1,
- * and 0 will be returned.
- */
-extern int match_bool_arg(int *out, struct args_info *args, const char *opt);
-
-/**
  * Basic output function for plain text diff output
  * Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`)
  */
@@ -196,3 +130,5 @@ extern int cred_acquire_cb(git_cred **out,
 		const char *username_from_url,
 		unsigned int allowed_types,
 		void *payload);
+
+#endif
diff --git a/examples/describe.c b/examples/describe.c
index 53f548c..1236272 100644
--- a/examples/describe.c
+++ b/examples/describe.c
@@ -13,7 +13,6 @@
  */
 
 #include "common.h"
-#include <assert.h>
 
 /**
  * The following example partially reimplements the `git describe` command
@@ -38,16 +37,14 @@
  */
 
 /** describe_options represents the parsed command line options */
-typedef struct {
+struct describe_options {
 	const char **commits;
 	size_t commit_count;
 	git_describe_options describe_options;
 	git_describe_format_options format_options;
-} describe_options;
+};
 
-typedef struct args_info args_info;
-
-static void opts_add_commit(describe_options *opts, const char *commit)
+static void opts_add_commit(struct describe_options *opts, const char *commit)
 {
 	size_t sz;
 
@@ -58,7 +55,7 @@ static void opts_add_commit(describe_options *opts, const char *commit)
 	opts->commits[opts->commit_count - 1] = commit;
 }
 
-static void do_describe_single(git_repository *repo, describe_options *opts, const char *rev)
+static void do_describe_single(git_repository *repo, struct describe_options *opts, const char *rev)
 {
 	git_object *commit;
 	git_describe_result *describe_result;
@@ -81,7 +78,7 @@ static void do_describe_single(git_repository *repo, describe_options *opts, con
 	printf("%s\n", buf.ptr);
 }
 
-static void do_describe(git_repository *repo, describe_options *opts)
+static void do_describe(git_repository *repo, struct describe_options *opts)
 {
 	if (opts->commit_count == 0)
 		do_describe_single(repo, opts, NULL);
@@ -100,9 +97,9 @@ static void print_usage(void)
 }
 
 /** Parse command line arguments */
-static void parse_options(describe_options *opts, int argc, char **argv)
+static void parse_options(struct describe_options *opts, int argc, char **argv)
 {
-	args_info args = ARGS_INFO_INIT;
+	struct args_info args = ARGS_INFO_INIT;
 
 	for (args.pos = 1; args.pos < argc; ++args.pos) {
 		const char *curr = argv[args.pos];
@@ -142,7 +139,7 @@ static void parse_options(describe_options *opts, int argc, char **argv)
 }
 
 /** Initialize describe_options struct */
-static void describe_options_init(describe_options *opts)
+static void describe_options_init(struct describe_options *opts)
 {
 	memset(opts, 0, sizeof(*opts));
 
@@ -154,7 +151,7 @@ static void describe_options_init(describe_options *opts)
 
 int lg2_describe(git_repository *repo, int argc, char **argv)
 {
-	describe_options opts;
+	struct describe_options opts;
 
 	describe_options_init(&opts);
 	parse_options(&opts, argc, argv);
diff --git a/examples/diff.c b/examples/diff.c
index 9e2aa9c..2305c86 100644
--- a/examples/diff.c
+++ b/examples/diff.c
@@ -47,8 +47,8 @@ enum {
 	CACHE_NONE = 2
 };
 
-/** The 'opts' struct captures all the various parsed command line options. */
-struct opts {
+/** The 'diff_options' struct captures all the various parsed command line options. */
+struct diff_options {
 	git_diff_options diffopts;
 	git_diff_find_options findopts;
 	int color;
@@ -63,18 +63,17 @@ struct opts {
 
 /** These functions are implemented at the end */
 static void usage(const char *message, const char *arg);
-static void parse_opts(struct opts *o, int argc, char *argv[]);
+static void parse_opts(struct diff_options *o, int argc, char *argv[]);
 static int color_printer(
 	const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
-static void diff_print_stats(git_diff *diff, struct opts *o);
-static void compute_diff_no_index(git_diff **diff, struct opts *o);
+static void diff_print_stats(git_diff *diff, struct diff_options *o);
+static void compute_diff_no_index(git_diff **diff, struct diff_options *o);
 
 int lg2_diff(git_repository *repo, int argc, char *argv[])
 {
 	git_tree *t1 = NULL, *t2 = NULL;
 	git_diff *diff;
-
-	struct opts o = {
+	struct diff_options o = {
 		GIT_DIFF_OPTIONS_INIT, GIT_DIFF_FIND_OPTIONS_INIT,
 		-1, -1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
 	};
@@ -166,7 +165,7 @@ int lg2_diff(git_repository *repo, int argc, char *argv[])
 	return 0;
 }
 
-static void compute_diff_no_index(git_diff **diff, struct opts *o) {
+static void compute_diff_no_index(git_diff **diff, struct diff_options *o) {
 	git_patch *patch = NULL;
 	char *file1_str = NULL;
 	char *file2_str = NULL;
@@ -242,11 +241,10 @@ static int color_printer(
 }
 
 /** Parse arguments as copied from git-diff. */
-static void parse_opts(struct opts *o, int argc, char *argv[])
+static void parse_opts(struct diff_options *o, int argc, char *argv[])
 {
 	struct args_info args = ARGS_INFO_INIT;
 
-
 	for (args.pos = 1; args.pos < argc; ++args.pos) {
 		const char *a = argv[args.pos];
 
@@ -343,7 +341,7 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
 }
 
 /** Display diff output with "--stat", "--numstat", or "--shortstat" */
-static void diff_print_stats(git_diff *diff, struct opts *o)
+static void diff_print_stats(git_diff *diff, struct diff_options *o)
 {
 	git_diff_stats *stats;
 	git_buf b = GIT_BUF_INIT_CONST(NULL, 0);
diff --git a/examples/for-each-ref.c b/examples/for-each-ref.c
index 900792c..020dab4 100644
--- a/examples/for-each-ref.c
+++ b/examples/for-each-ref.c
@@ -3,42 +3,42 @@
 
 static int show_ref(git_reference *ref, void *data)
 {
-        git_repository *repo = data;
-        git_reference *resolved = NULL;
-        char hex[GIT_OID_HEXSZ+1];
-        const git_oid *oid;
-        git_object *obj;
-
-        if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC)
-                check_lg2(git_reference_resolve(&resolved, ref),
-                          "Unable to resolve symbolic reference",
-                          git_reference_name(ref));
-
-        oid = git_reference_target(resolved ? resolved : ref);
-        git_oid_fmt(hex, oid);
-        hex[GIT_OID_HEXSZ] = 0;
-        check_lg2(git_object_lookup(&obj, repo, oid, GIT_OBJECT_ANY),
-                  "Unable to lookup object", hex);
-
-        printf("%s %-6s\t%s\n",
-               hex,
-               git_object_type2string(git_object_type(obj)),
-               git_reference_name(ref));
-
-        if (resolved)
-                git_reference_free(resolved);
-        return 0;
+	git_repository *repo = data;
+	git_reference *resolved = NULL;
+	char hex[GIT_OID_HEXSZ+1];
+	const git_oid *oid;
+	git_object *obj;
+	
+	if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC)
+		check_lg2(git_reference_resolve(&resolved, ref),
+				  "Unable to resolve symbolic reference",
+				  git_reference_name(ref));
+	
+	oid = git_reference_target(resolved ? resolved : ref);
+	git_oid_fmt(hex, oid);
+	hex[GIT_OID_HEXSZ] = 0;
+	check_lg2(git_object_lookup(&obj, repo, oid, GIT_OBJECT_ANY),
+			  "Unable to lookup object", hex);
+	
+	printf("%s %-6s\t%s\n",
+		   hex,
+		   git_object_type2string(git_object_type(obj)),
+		   git_reference_name(ref));
+	
+	if (resolved)
+		git_reference_free(resolved);
+	return 0;
 }
 
 int lg2_for_each_ref(git_repository *repo, int argc, char **argv)
 {
 	UNUSED(argv);
-
-        if (argc != 1)
-                fatal("Sorry, no for-each-ref options supported yet", NULL);
-
-        check_lg2(git_reference_foreach(repo, show_ref, repo),
-                  "Could not iterate over references", NULL);
-
-        return 0;
+	
+	if (argc != 1)
+		fatal("Sorry, no for-each-ref options supported yet", NULL);
+	
+	check_lg2(git_reference_foreach(repo, show_ref, repo),
+			  "Could not iterate over references", NULL);
+	
+	return 0;
 }
diff --git a/examples/init.c b/examples/init.c
index e9841bc..2f681c5 100644
--- a/examples/init.c
+++ b/examples/init.c
@@ -27,7 +27,7 @@
  */
 
 /** Forward declarations of helpers */
-struct opts {
+struct init_opts {
 	int no_options;
 	int quiet;
 	int bare;
@@ -38,11 +38,11 @@ struct opts {
 	const char *dir;
 };
 static void create_initial_commit(git_repository *repo);
-static void parse_opts(struct opts *o, int argc, char *argv[]);
+static void parse_opts(struct init_opts *o, int argc, char *argv[]);
 
 int lg2_init(git_repository *repo, int argc, char *argv[])
 {
-	struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
+	struct init_opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
 
 	parse_opts(&o, argc, argv);
 
@@ -210,7 +210,7 @@ static uint32_t parse_shared(const char *shared)
 	return 0;
 }
 
-static void parse_opts(struct opts *o, int argc, char *argv[])
+static void parse_opts(struct init_opts *o, int argc, char *argv[])
 {
 	struct args_info args = ARGS_INFO_INIT;
 	const char *sharedarg;
diff --git a/examples/lg2.c b/examples/lg2.c
index f1a8843..a3987c3 100644
--- a/examples/lg2.c
+++ b/examples/lg2.c
@@ -84,8 +84,7 @@ int main(int argc, char **argv)
 			break;
 		} else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
 			continue;
-		} else if (!strcmp(a, "--")) {
-			/* arg separator */
+		} else if (match_arg_separator(&args)) {
 			break;
 		}
 	}
diff --git a/examples/log.c b/examples/log.c
index a6bd957..3eee7d4 100644
--- a/examples/log.c
+++ b/examples/log.c
@@ -424,8 +424,7 @@ static int parse_options(
 			else
 				/** Try failed revision parse as filename. */
 				break;
-		} else if (!strcmp(a, "--")) {
-			++args.pos;
+		} else if (!match_arg_separator(&args)) {
 			break;
 		}
 		else if (!strcmp(a, "--date-order"))
diff --git a/examples/ls-files.c b/examples/ls-files.c
index b82af01..a235069 100644
--- a/examples/ls-files.c
+++ b/examples/ls-files.c
@@ -25,11 +25,11 @@
  * This currently supports the default behavior and the `--error-unmatch` option.
  */
 
-typedef struct {
+struct ls_options {
 	int error_unmatch;
 	char *files[1024];
 	size_t file_count;
-} ls_options;
+};
 
 static void usage(const char *message, const char *arg)
 {
@@ -41,12 +41,12 @@ static void usage(const char *message, const char *arg)
 	exit(1);
 }
 
-static int parse_options(ls_options *opts, int argc, char *argv[])
+static int parse_options(struct ls_options *opts, int argc, char *argv[])
 {
 	int parsing_files = 0;
 	int i;
 
-	memset(opts, 0, sizeof(ls_options));
+	memset(opts, 0, sizeof(struct ls_options));
 
 	if (argc < 2)
 		return 0;
@@ -78,7 +78,7 @@ static int parse_options(ls_options *opts, int argc, char *argv[])
 	return 0;
 }
 
-static int print_paths(ls_options *opts, git_index *index)
+static int print_paths(struct ls_options *opts, git_index *index)
 {
 	size_t i;
 	const git_index_entry *entry;
@@ -113,7 +113,7 @@ static int print_paths(ls_options *opts, git_index *index)
 int lg2_ls_files(git_repository *repo, int argc, char *argv[])
 {
 	git_index *index = NULL;
-	ls_options opts;
+	struct ls_options opts;
 	int error;
 
 	if ((error = parse_options(&opts, argc, argv)) < 0)
diff --git a/examples/merge.c b/examples/merge.c
index 995ac02..460c06a 100644
--- a/examples/merge.c
+++ b/examples/merge.c
@@ -13,7 +13,6 @@
  */
 
 #include "common.h"
-#include <assert.h>
 
 /** The following example demonstrates how to do merges with libgit2.
  *
@@ -24,7 +23,7 @@
  *
  */
 
-typedef struct {
+struct merge_options {
 	const char **heads;
 	size_t heads_count;
 
@@ -32,7 +31,7 @@ typedef struct {
 	size_t annotated_count;
 
 	int no_commit : 1;
-} merge_options;
+};
 
 static void print_usage(void)
 {
@@ -40,7 +39,7 @@ static void print_usage(void)
 	exit(1);
 }
 
-static void merge_options_init(merge_options *opts)
+static void merge_options_init(struct merge_options *opts)
 {
 	memset(opts, 0, sizeof(*opts));
 
@@ -50,7 +49,7 @@ static void merge_options_init(merge_options *opts)
 	opts->annotated_count = 0;
 }
 
-static void opts_add_refish(merge_options *opts, const char *refish)
+static void opts_add_refish(struct merge_options *opts, const char *refish)
 {
 	size_t sz;
 
@@ -61,7 +60,7 @@ static void opts_add_refish(merge_options *opts, const char *refish)
 	opts->heads[opts->heads_count - 1] = refish;
 }
 
-static void parse_options(const char **repo_path, merge_options *opts, int argc, char **argv)
+static void parse_options(const char **repo_path, struct merge_options *opts, int argc, char **argv)
 {
 	struct args_info args = ARGS_INFO_INIT;
 
@@ -83,7 +82,7 @@ static void parse_options(const char **repo_path, merge_options *opts, int argc,
 	}
 }
 
-static int resolve_heads(git_repository *repo, merge_options *opts)
+static int resolve_heads(git_repository *repo, struct merge_options *opts)
 {
 	git_annotated_commit **annotated = calloc(opts->heads_count, sizeof(git_annotated_commit *));
 	size_t annotated_count = 0, i;
@@ -201,7 +200,7 @@ static void output_conflicts(git_index *index)
 	git_index_conflict_iterator_free(conflicts);
 }
 
-static int create_merge_commit(git_repository *repo, git_index *index, merge_options *opts)
+static int create_merge_commit(git_repository *repo, git_index *index, struct merge_options *opts)
 {
 	git_oid tree_oid, commit_oid;
 	git_tree *tree;
@@ -277,7 +276,7 @@ cleanup:
 
 int lg2_merge(git_repository *repo, int argc, char **argv)
 {
-	merge_options opts;
+	struct merge_options opts;
 	git_index *index;
 	git_repository_state_t state;
 	git_merge_analysis_t analysis;
diff --git a/examples/remote.c b/examples/remote.c
index 7345f82..34d8865 100644
--- a/examples/remote.c
+++ b/examples/remote.c
@@ -30,7 +30,7 @@ enum subcmd {
 	subcmd_show,
 };
 
-struct opts {
+struct remote_opts {
 	enum subcmd cmd;
 
 	/* for command-specific args */
@@ -38,20 +38,20 @@ struct opts {
 	char **argv;
 };
 
-static int cmd_add(git_repository *repo, struct opts *o);
-static int cmd_remove(git_repository *repo, struct opts *o);
-static int cmd_rename(git_repository *repo, struct opts *o);
-static int cmd_seturl(git_repository *repo, struct opts *o);
-static int cmd_show(git_repository *repo, struct opts *o);
+static int cmd_add(git_repository *repo, struct remote_opts *o);
+static int cmd_remove(git_repository *repo, struct remote_opts *o);
+static int cmd_rename(git_repository *repo, struct remote_opts *o);
+static int cmd_seturl(git_repository *repo, struct remote_opts *o);
+static int cmd_show(git_repository *repo, struct remote_opts *o);
 
 static void parse_subcmd(
-	struct opts *opt, int argc, char **argv);
+	struct remote_opts *opt, int argc, char **argv);
 static void usage(const char *msg, const char *arg);
 
 int lg2_remote(git_repository *repo, int argc, char *argv[])
 {
 	int retval = 0;
-	struct opts opt = {0};
+	struct remote_opts opt = {0};
 
 	parse_subcmd(&opt, argc, argv);
 
@@ -77,7 +77,7 @@ int lg2_remote(git_repository *repo, int argc, char *argv[])
 	return retval;
 }
 
-static int cmd_add(git_repository *repo, struct opts *o)
+static int cmd_add(git_repository *repo, struct remote_opts *o)
 {
 	char *name, *url;
 	git_remote *remote = {0};
@@ -94,7 +94,7 @@ static int cmd_add(git_repository *repo, struct opts *o)
 	return 0;
 }
 
-static int cmd_remove(git_repository *repo, struct opts *o)
+static int cmd_remove(git_repository *repo, struct remote_opts *o)
 {
 	char *name;
 
@@ -109,7 +109,7 @@ static int cmd_remove(git_repository *repo, struct opts *o)
 	return 0;
 }
 
-static int cmd_rename(git_repository *repo, struct opts *o)
+static int cmd_rename(git_repository *repo, struct remote_opts *o)
 {
 	int i, retval;
 	char *old, *new;
@@ -134,7 +134,7 @@ static int cmd_rename(git_repository *repo, struct opts *o)
 	return retval;
 }
 
-static int cmd_seturl(git_repository *repo, struct opts *o)
+static int cmd_seturl(git_repository *repo, struct remote_opts *o)
 {
 	int i, retval, push = 0;
 	char *name = NULL, *url = NULL;
@@ -166,7 +166,7 @@ static int cmd_seturl(git_repository *repo, struct opts *o)
 	return 0;
 }
 
-static int cmd_show(git_repository *repo, struct opts *o)
+static int cmd_show(git_repository *repo, struct remote_opts *o)
 {
 	int i;
 	const char *arg, *name, *fetch, *push;
@@ -213,7 +213,7 @@ static int cmd_show(git_repository *repo, struct opts *o)
 }
 
 static void parse_subcmd(
-	struct opts *opt, int argc, char **argv)
+	struct remote_opts *opt, int argc, char **argv)
 {
 	char *arg = argv[1];
 	enum subcmd cmd = 0;
diff --git a/examples/rev-list.c b/examples/rev-list.c
index c366ece..75fb19e 100644
--- a/examples/rev-list.c
+++ b/examples/rev-list.c
@@ -15,16 +15,24 @@
 
 #include "common.h"
 
-static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts);
+#include <assert.h>
+
+static int revwalk_parse_options(git_sort_t *sort, struct args_info *args);
+static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args);
 
 int lg2_rev_list(git_repository *repo, int argc, char **argv)
 {
+	struct args_info args = ARGS_INFO_INIT;
 	git_revwalk *walk;
 	git_oid oid;
+	git_sort_t sort;
 	char buf[GIT_OID_HEXSZ+1];
 
+	check_lg2(revwalk_parse_options(&sort, &args), "parsing options", NULL);
+
 	check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL);
-	check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL);
+	git_revwalk_sorting(walk, sort);
+	check_lg2(revwalk_parse_revs(repo, walk, &args), "parsing revs", NULL);
 
 	while (!git_revwalk_next(&oid, walk)) {
 		git_oid_fmt(buf, &oid);
@@ -32,6 +40,7 @@ int lg2_rev_list(git_repository *repo, int argc, char **argv)
 		printf("%s\n", buf);
 	}
 
+	git_revwalk_free(walk);
 	return 0;
 }
 
@@ -80,33 +89,60 @@ out:
 	return error;
 }
 
-static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts)
+static void print_usage(void)
+{
+	fprintf(stderr, "rev-list [--git-dir=dir] [--topo-order|--date-order] [--reverse] <revspec>\n");
+	exit(-1);
+}
+
+static int revwalk_parse_options(git_sort_t *sort, struct args_info *args)
 {
-	int hide, i, error;
-	unsigned int sorting = GIT_SORT_NONE;
+	assert(sort && args);
+	*sort = GIT_SORT_NONE;
+
+	if (args->argc < 1)
+		print_usage();
+
+	for (args->pos = 1; args->pos < args->argc; ++args->pos) {
+		const char *curr = args->argv[args->pos];
+
+		if (!strcmp(curr, "--topo-order")) {
+			*sort |= GIT_SORT_TOPOLOGICAL;
+		} else if (!strcmp(curr, "--date-order")) {
+			*sort |= GIT_SORT_TIME;
+		} else if (!strcmp(curr, "--reverse")) {
+			*sort |= (*sort & ~GIT_SORT_REVERSE) ^ GIT_SORT_REVERSE;
+		} else {
+			break;
+		}
+	}
+	return 0;
+}
+
+static int revwalk_parse_revs(git_repository *repo, git_revwalk *walk, struct args_info *args)
+{
+	int hide, error;
+	git_oid oid;
 
 	hide = 0;
-	for (i = 0; i < nopts; i++) {
-		if (!strcmp(opts[i], "--topo-order")) {
-			sorting = GIT_SORT_TOPOLOGICAL | (sorting & GIT_SORT_REVERSE);
-			git_revwalk_sorting(walk, sorting);
-		} else if (!strcmp(opts[i], "--date-order")) {
-			sorting = GIT_SORT_TIME | (sorting & GIT_SORT_REVERSE);
-			git_revwalk_sorting(walk, sorting);
-		} else if (!strcmp(opts[i], "--reverse")) {
-			sorting = (sorting & ~GIT_SORT_REVERSE)
-			    | ((sorting & GIT_SORT_REVERSE) ? 0 : GIT_SORT_REVERSE);
-			git_revwalk_sorting(walk, sorting);
-		} else if (!strcmp(opts[i], "--not")) {
+	for (; args->pos < args->argc; ++args->pos) {
+		const char *curr = args->argv[args->pos];
+
+		if (!strcmp(curr, "--not")) {
 			hide = !hide;
-		} else if (opts[i][0] == '^') {
-			if ((error = push_spec(repo, walk, opts[i] + 1, !hide)))
+		} else if (curr[0] == '^') {
+			if ((error = push_spec(repo, walk, curr + 1, !hide)))
 				return error;
-		} else if (strstr(opts[i], "..")) {
-			if ((error = push_range(repo, walk, opts[i], hide)))
+		} else if (strstr(curr, "..")) {
+			if ((error = push_range(repo, walk, curr, hide)))
 				return error;
 		} else {
-			if ((error = push_spec(repo, walk, opts[i], hide)))
+			if (push_spec(repo, walk, curr, hide) == 0)
+				continue;
+
+			if ((error = git_oid_fromstr(&oid, curr)))
+				return error;
+			if ((error = push_commit(walk, &oid, hide)))
 				return error;
 		}
 	}
diff --git a/examples/show-index.c b/examples/show-index.c
index 34eb41c..7aaa45e 100644
--- a/examples/show-index.c
+++ b/examples/show-index.c
@@ -14,7 +14,7 @@
 
 #include "common.h"
 
-int lg2_show_index(git_repository *repo, int argc, char** argv)
+int lg2_show_index(git_repository *repo, int argc, char **argv)
 {
 	git_index *index;
 	size_t i, ecount;
diff --git a/examples/status.c b/examples/status.c
index 979ab7c..8cf9221 100644
--- a/examples/status.c
+++ b/examples/status.c
@@ -43,7 +43,7 @@ enum {
 
 #define MAX_PATHSPEC 8
 
-struct opts {
+struct status_opts {
 	git_status_options statusopt;
 	char *repodir;
 	char *pathspec[MAX_PATHSPEC];
@@ -55,7 +55,7 @@ struct opts {
 	int repeat;
 };
 
-static void parse_opts(struct opts *o, int argc, char *argv[]);
+static void parse_opts(struct status_opts *o, int argc, char *argv[]);
 static void show_branch(git_repository *repo, int format);
 static void print_long(git_status_list *status);
 static void print_short(git_repository *repo, git_status_list *status);
@@ -64,7 +64,7 @@ static int print_submod(git_submodule *sm, const char *name, void *payload);
 int lg2_status(git_repository *repo, int argc, char *argv[])
 {
 	git_status_list *status;
-	struct opts o = { GIT_STATUS_OPTIONS_INIT, "." };
+	struct status_opts o = { GIT_STATUS_OPTIONS_INIT, "." };
 
 	o.statusopt.show  = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
 	o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
@@ -435,7 +435,7 @@ static int print_submod(git_submodule *sm, const char *name, void *payload)
 /**
  * Parse options that git's status command supports.
  */
-static void parse_opts(struct opts *o, int argc, char *argv[])
+static void parse_opts(struct status_opts *o, int argc, char *argv[])
 {
 	struct args_info args = ARGS_INFO_INIT;
 
diff --git a/examples/tag.c b/examples/tag.c
index 7bfb92a..4408294 100644
--- a/examples/tag.c
+++ b/examples/tag.c
@@ -31,19 +31,19 @@
  */
 
 /** tag_options represents the parsed command line options */
-typedef struct {
+struct tag_options {
 	const char *message;
 	const char *pattern;
 	const char *tag_name;
 	const char *target;
 	int num_lines;
 	int force;
-} tag_options;
+};
 
 /** tag_state represents the current program state for dragging around */
 typedef struct {
 	git_repository *repo;
-	tag_options *opts;
+	struct tag_options *opts;
 } tag_state;
 
 /** An action to execute based on the command line arguments */
@@ -167,7 +167,7 @@ static void action_list_tags(tag_state *state)
 
 static void action_delete_tag(tag_state *state)
 {
-	tag_options *opts = state->opts;
+	struct tag_options *opts = state->opts;
 	git_object *obj;
 	git_buf abbrev_oid = {0};
 
@@ -191,7 +191,7 @@ static void action_delete_tag(tag_state *state)
 static void action_create_lighweight_tag(tag_state *state)
 {
 	git_repository *repo = state->repo;
-	tag_options *opts = state->opts;
+	struct tag_options *opts = state->opts;
 	git_oid oid;
 	git_object *target;
 
@@ -213,7 +213,7 @@ static void action_create_lighweight_tag(tag_state *state)
 static void action_create_tag(tag_state *state)
 {
 	git_repository *repo = state->repo;
-	tag_options *opts = state->opts;
+	struct tag_options *opts = state->opts;
 	git_signature *tagger;
 	git_oid oid;
 	git_object *target;
@@ -243,7 +243,7 @@ static void print_usage(void)
 }
 
 /** Parse command line arguments and choose action to run when done */
-static void parse_options(tag_action *action, tag_options *opts, int argc, char **argv)
+static void parse_options(tag_action *action, struct tag_options *opts, int argc, char **argv)
 {
 	args_info args = ARGS_INFO_INIT;
 	*action = &action_list_tags;
@@ -281,7 +281,7 @@ static void parse_options(tag_action *action, tag_options *opts, int argc, char 
 }
 
 /** Initialize tag_options struct */
-static void tag_options_init(tag_options *opts)
+static void tag_options_init(struct tag_options *opts)
 {
 	memset(opts, 0, sizeof(*opts));
 
@@ -295,7 +295,7 @@ static void tag_options_init(tag_options *opts)
 
 int lg2_tag(git_repository *repo, int argc, char **argv)
 {
-	tag_options opts;
+	struct tag_options opts;
 	tag_action action;
 	tag_state state;
 
diff --git a/src/apply.c b/src/apply.c
index 65b057c..fc60e14 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -7,8 +7,6 @@
 
 #include "apply.h"
 
-#include <assert.h>
-
 #include "git2/apply.h"
 #include "git2/patch.h"
 #include "git2/filter.h"
diff --git a/src/checkout.c b/src/checkout.c
index d618e3d..5cfa728 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -7,8 +7,6 @@
 
 #include "checkout.h"
 
-#include <assert.h>
-
 #include "git2/repository.h"
 #include "git2/refs.h"
 #include "git2/tree.h"
diff --git a/src/clone.c b/src/clone.c
index e8972b4..3d749c3 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -7,8 +7,6 @@
 
 #include "clone.h"
 
-#include <assert.h>
-
 #include "git2/clone.h"
 #include "git2/remote.h"
 #include "git2/revparse.h"
diff --git a/src/revparse.c b/src/revparse.c
index 2618b38..c627de6 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -7,8 +7,6 @@
 
 #include "common.h"
 
-#include <assert.h>
-
 #include "buffer.h"
 #include "tree.h"
 #include "refdb.h"