Knowledge

getopt

Source 📝

120: 32: 73: 418:
Another common advanced extension of getopt is resetting the state of argument parsing; this is useful as a replacement of the options-anyware GNU extension, or as a way to "layer" a set of command-line interface with different options at different levels. This is achieved in BSD systems using an
2641:
command for Unix SVR3 Bourne Shell instead. The advantage of building the command into the shell is that it now has access to the shell's variables, so values could be written safely without quoting. It uses the shell's own variables to track the position of current and argument positions,
2819:: has an implementation of getopt_long in its standard library, GetoptLong. Ruby also has modules in its standard library with a more sophisticated and convenient interface. A third party implementation of the original getopt interface is available. 634:
itself returns an integer that is either an option character or -1 for end-of-options. The idiom is to use a while-loop to go through options, and to use a switch-case statement to pick and act on options. See the example section of this article.
281:
so that there would be a common interface on which everyone could depend. As such, the original authors picked out of the variations support for single character options, multiple options specified together, and options with arguments
2694:
is a concise description of the common POSIX command argument structure, and it is replicated widely by programmers seeking to provide a similar interface, both to themselves and to the user on the command-line.
2675:
came out with an enhanced version that fixed all of old getopt's problems by escaping. It also supports GNU's long option names. On the other hand, long options have been implemented rarely in the
730:
and takes an extra option for defining the "short" names of long options and some extra controls. If a short name is not defined, getopt will put an index referring to the option structure in the
300:
at the 1985 UNIFORUM conference in Dallas, Texas, with the intent for it to be available in the public domain. Versions of it were subsequently picked up by other flavors of Unix (
2623:
Shell script programmers commonly want to provide a consistent way of providing options. To achieve this goal, they turn to getopts and seek to port it to their own language.
2771:: There is no implementation of getopt in the Java standard library. Several open source modules exist, including gnu.getopt.Getopt, which is ported from GNU getopt, and 387:, allows parsing of more readable, multicharacter options, which are introduced by two dashes instead of one. The choice of two dashes allows multicharacter options ( 2850: 2952: 2923: 2665:
Portability Guidelines Issue 4. Now a part of the POSIX Shell standard, getopts have spread far and wide in many other shells trying to be POSIX-compliant.
3115: 2634:(USL). This version was unable to deal with quoting and shell metacharacters, as it shows no attempts at quoting. It has been inherited to FreeBSD. 372:
implicitly permutes the argument vector so it still leaves the non-options in the end. Since POSIX already has the convention of returning -1 on
2813:
based on C's getopt and GNU extensions. Python's standard library also contains other modules to parse options that are more convenient to use.
2781:: has many different dialects with no common standard library. There are some third party implementations of getopt for some dialects of Lisp. 94: 81: 2711:(both accept GNU-style), as well as some more minimal libraries, can be used to provide the functionality. Alternative interfaces also exist: 2894: 177: 159: 59: 2637:
In 1986, USL decided that being unsafe around metacharacters and whitespace was no longer acceptable, and they created the builtin
349:
is a system dependent function, and its behavior depends on the implementation in the C library. Some custom implementations like
2947:(The Open Group Base Specifications Issue 7, 2018 edition; IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008) ed.). 2918:(The Open Group Base Specifications Issue 7, 2018 edition; IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008) ed.). 356:
The conventional (POSIX and BSD) handling is that the options end when the first non-option argument is encountered, and that
3505: 2762: 2806: 2842: 2944: 2816: 2778: 2768: 2915: 2871: 696:
A pointer to the argument of the current option, if present. Can be used to control where to start parsing (again).
197: 141: 399:. This interface proved popular, and has been taken up (sans the permutation) by many BSD distributions including 2744: 2658: 440:
The command-line syntaxes for getopt-based programs is the POSIX-recommended Utility Argument Syntax. In short:
3500: 2631: 194: 3122: 130: 86: 45: 3071: 227:
was how to specify options; early programs used many ways of doing so, including single character options (
3192: 3089: 2791:: has its own implementation as one of its standard units named GetOpts. It is supported on all platforms. 2738: 2723: 224: 3308: 3475: 2825:: does not have getopt functionality in its standard library. Third-party implementations are available. 278: 204: 461:
When an option takes an argument, this can be in the same token or in the next one. In other words, if
3294: 3031: 324: 2974: 215:
systems. It is also the name of a Unix program for parsing command line arguments in shell scripts.
3005: 2719: 476:
Multiple options can be chained together, as long as the non-last ones are not argument taking. If
3053: 2765:: comes with System.Console.GetOpt, which is essentially a Haskell port of the GNU getopt library. 3480: 3170: 2886: 2797:: has two separate derivatives of getopt in its standard library: Getopt::Long and Getopt::Std. 2865: 2810: 2846: 3266: 2948: 2919: 2822: 2772: 404: 2733:
family of functions in glibc and gnulib provides some more convenience and modularity.
600:
function prototype; i.e., argc indicates the length of the argv array-of-strings. The
3494: 3397:"16.5. getopt — C-style parser for command line options — Python 3.6.0 documentation" 3485: 3433: 3486:
Full getopt port for Unicode and Multibyte Microsoft Visual C, C++, or MFC projects
3382: 395:). The GNU extension also allows an alternative format for options with arguments: 338:. It parses a string of comma-separated sub-options. It appeared in 4.4BSD (1995). 3410: 604:
contains a specification of what options to look for (normal alphanumerals except
3328: 2788: 2782: 727: 376:
and skipping it, one can always portably use it as an end-of-options signifier.
316: 51: 3234: 3396: 2849:, released June 1980, linked here from Warren Toomey's The Unix Tree project. 2672: 274: 200: 2755:
package supports processing closer to the C function. There is also another
3453: 507:
All options precede non-option arguments (except for in the GNU extension).
408: 277:
was written to be a standard mechanism that all programs could use to parse
212: 3354: 718:
If an unrecognized option occurs, the value of that unrecognized character.
72: 3368: 3221: 3180: 3157: 3041: 2984: 391:) to be differentiated from single character options specified together ( 313: 712:
A boolean switch controlling whether getopt should print error messages.
3176: 3037: 2980: 2638: 2618: 400: 297: 20: 3458: 3280: 2704: 2662: 350: 301: 407:. An alternative way to support long options is seen in Solaris and 3147: 2759:
package providing interface much closer to the original POSIX one.
638:
To communicate extra information back to the program, a few global
3217: 3153: 2708: 2680: 642:
variables are referenced by the program to fetch information from
361: 309: 305: 208: 2794: 3252: 608:), and what options to accept arguments (colons). For example, 3211: 2800: 527:
The getopt manual from GNU specifies such a usage for getopt:
515:
Extensions on the syntax include the GNU convention and Sun's
454:
In order to specify that an option takes an argument, include
380: 113: 66: 25: 3434:"Parser for command-line options, arguments and sub-commands" 3235:"visual studio - getopt.h: Compiling Linux C-Code in Windows" 451:
Options can take an argument, mandatory or optional, or none.
3114:
David-John, Burrowes; Kowalski III, Joseph E. (2003-01-22).
1587:/* for getopt_long; POSIX standard getopt is in unistd.h */ 726:
interface is similar, although it belongs to a different
458:
after the option name (only during initial specification)
444:
Options are single-character alphanumerics preceded by a
516: 296:
dates back to at least 1980 and was first published by
137: 1725:/* NAME ARGUMENT FLAG SHORTNAME */ 129:
may contain excessive or inappropriate references to
2756: 2752: 2748: 2730: 2715: 2700: 2676: 2650:, and returns the option name in a shell variable. 2475:"?? getopt returned character code 0%o ?? 1416:"?? getopt returned character code 0%o ?? 643: 639: 373: 335: 331: 16:
C library function for parsing command-line options
2891:linux.co.cr (originally in mod.std.unix newsgroup) 2199:"digits occur in two different argv-elements. 1140:"digits occur in two different argv-elements. 3116:"CLIP Specification, Version 1.0, PSARC 1999/645" 3335:. Apache Software Foundation. February 27, 2013 2741:: has getopt module in the D standard library. 2626:The first attempt at porting was the program 207:of the Unix/POSIX style. It is a part of the 8: 2785:has a prominent third party implementation. 2751:package, which allows long flag names. The 596:are defined exactly like they are in the C 60:Learn how and when to remove these messages 612:refers to three options: an argumentless 290:), all controllable by an option string. 178:Learn how and when to remove this message 160:Learn how and when to remove this message 3141: 3139: 3137: 3135: 3000: 2998: 2996: 2994: 2992: 2722:, has the additional advantage of being 702:Where getopt is currently looking at in 423:variable, and on GNU systems by setting 364:extension, however, options are allowed 231:), multiple options specified together ( 97:of all important aspects of the article. 3058:Oracle Solaris 11.2 Information Library 2834: 360:would return -1 to signal that. In the 330:A POSIX-standard companion function to 2887:"public domain AT&T getopt source" 2863: 2526:"non-option ARGV-elements: " 2412:"option d with value '%s' 2361:"option c with value '%s' 1467:"non-option ARGV-elements: " 1353:"option d with value '%s' 1302:"option c with value '%s' 93:Please consider expanding the lead to 2843:"usr/src/lib/libc/pdp11/gen/getopt.c" 140:by removing references to unreliable 7: 628:extension for long option synonyms. 255:), and different prefix characters ( 144:where they are used inappropriately. 211:specification, and is universal to 14: 3411:"Parser for command line options" 3355:"Getopt::Long - perldoc.perl.org" 2699:C: non-POSIX systems do not ship 41:This article has multiple issues. 3369:"Getopt::Std - perldoc.perl.org" 511:always marks the end of options. 308:, etc.). It is specified in the 118: 71: 30: 2955:from the original on 2023-12-02 2926:from the original on 2024-03-24 2897:from the original on 2023-05-12 2885:Quarterman, John (1985-11-03). 2853:from the original on 2023-05-12 327:to parse command-line options. 85:may be too short to adequately 49:or discuss these issues on the 2671:was basically forgotten until 415:), but it was not as popular. 95:provide an accessible overview 1: 3422:Deprecated since version 2.7 488:takes an optional argument, 3197:The Open Group (POSIX 2018) 2809:: contains a module in its 620:, and a mandatory-argument 323:have been created for many 243:), options with arguments ( 239:), multicharacter options ( 223:A long-standing issue with 3522: 2616: 504:due to the preceding rule. 18: 3309:"Class gnu.getopt.Getopt" 2795:Perl programming language 2679:command in other shells, 2659:Single UNIX Specification 448:(hyphen-minus) character. 2803:: has a getopt function. 2632:Unix System Laboratories 2094:" with arg %s" 1560: 837: 736: 648: 624:. GNU here implements a 529: 484:take no arguments while 353:are available, however. 312:standard as part of the 19:Not to be confused with 3295:"System.Console.GetOpt" 616:, an optional-argument 3383:"PHP: getopt - Manual" 2870:: CS1 maint: others ( 2739:D programming language 2703:in the C library, but 131:self-published sources 3506:Command-line software 3072:"Utility Conventions" 2055:"option %s" 1965:"abc:d:012" 1014:"abc:d:012" 831:Using POSIX standard 325:programming languages 225:command line programs 2683:being an exception. 2657:was included in the 1554:Using GNU extension 279:command-line options 205:command-line options 3476:POSIX specification 2720:RPM package manager 1812:"verbose" 500:is not the same as 465:takes an argument, 3156:Library Functions 3040:Library Functions 2983:Library Functions 2687:In other languages 2220:this_option_optind 2184:this_option_optind 2001:this_option_optind 1839:"create" 1785:"delete" 1758:"append" 1161:this_option_optind 1125:this_option_optind 1035:this_option_optind 722:The GNU extension 3481:GNU getopt manual 3454:"GNU Getopt .NET" 3179:General Commands 3094:The GNU C Library 3090:"Argument Syntax" 2747:: comes with the 2718:library, used by 2630:, implemented by 1872:required_argument 1845:required_argument 1791:required_argument 1737:required_argument 734:pointer instead. 368:for ease of use; 319:. Derivatives of 235:is equivalent to 188: 187: 180: 170: 169: 162: 112: 111: 64: 3513: 3464: 3463: 3450: 3444: 3443: 3441: 3440: 3430: 3424: 3420: 3418: 3417: 3407: 3401: 3400: 3393: 3387: 3386: 3379: 3373: 3372: 3365: 3359: 3358: 3351: 3345: 3344: 3342: 3340: 3325: 3319: 3318: 3316: 3315: 3305: 3299: 3298: 3291: 3285: 3284: 3281:"Package getopt" 3277: 3271: 3270: 3267:"Package getopt" 3263: 3257: 3256: 3249: 3243: 3242: 3231: 3225: 3215: 3214: 3207: 3201: 3200: 3189: 3183: 3174: 3173: 3166: 3160: 3151: 3150: 3143: 3130: 3129: 3127: 3121:. Archived from 3120: 3111: 3105: 3104: 3102: 3100: 3086: 3080: 3079: 3068: 3062: 3061: 3050: 3044: 3035: 3034: 3027: 3021: 3020: 3018: 3016: 3002: 2987: 2978: 2977: 2970: 2964: 2963: 2961: 2960: 2941: 2935: 2934: 2932: 2931: 2912: 2906: 2905: 2903: 2902: 2882: 2876: 2875: 2869: 2861: 2859: 2858: 2839: 2811:standard library 2758: 2754: 2750: 2732: 2717: 2702: 2678: 2656: 2649: 2645: 2608: 2605: 2602: 2599: 2596: 2593: 2590: 2587: 2584: 2581: 2578: 2575: 2572: 2569: 2566: 2563: 2560: 2557: 2554: 2551: 2548: 2545: 2542: 2539: 2536: 2533: 2530: 2527: 2524: 2521: 2518: 2515: 2512: 2509: 2506: 2503: 2500: 2497: 2494: 2491: 2488: 2485: 2482: 2479: 2476: 2473: 2470: 2467: 2464: 2461: 2458: 2455: 2452: 2449: 2446: 2443: 2440: 2437: 2434: 2431: 2428: 2425: 2422: 2419: 2416: 2413: 2410: 2407: 2404: 2401: 2398: 2395: 2392: 2389: 2386: 2383: 2380: 2377: 2374: 2371: 2368: 2365: 2362: 2359: 2356: 2353: 2350: 2347: 2344: 2341: 2338: 2335: 2332: 2329: 2326: 2323: 2320: 2317: 2314: 2311: 2308: 2305: 2302: 2299: 2296: 2293: 2290: 2287: 2284: 2281: 2278: 2275: 2272: 2269: 2266: 2263: 2260: 2257: 2254: 2251: 2248: 2245: 2242: 2239: 2236: 2233: 2230: 2227: 2224: 2221: 2218: 2215: 2212: 2209: 2206: 2203: 2200: 2197: 2194: 2191: 2188: 2185: 2182: 2179: 2176: 2173: 2170: 2167: 2164: 2161: 2158: 2155: 2152: 2149: 2146: 2143: 2140: 2137: 2134: 2131: 2128: 2125: 2122: 2119: 2116: 2113: 2110: 2107: 2104: 2101: 2098: 2095: 2092: 2089: 2086: 2083: 2080: 2077: 2074: 2071: 2068: 2065: 2062: 2059: 2056: 2053: 2050: 2047: 2044: 2041: 2038: 2035: 2032: 2029: 2026: 2023: 2020: 2017: 2014: 2011: 2008: 2005: 2002: 1999: 1996: 1993: 1990: 1987: 1984: 1981: 1978: 1975: 1972: 1969: 1966: 1963: 1960: 1957: 1954: 1951: 1948: 1945: 1942: 1939: 1936: 1933: 1930: 1927: 1924: 1921: 1918: 1915: 1912: 1909: 1906: 1903: 1900: 1897: 1894: 1891: 1888: 1885: 1882: 1879: 1876: 1873: 1870: 1867: 1866:"file" 1864: 1861: 1858: 1855: 1852: 1849: 1846: 1843: 1840: 1837: 1834: 1831: 1828: 1825: 1822: 1819: 1816: 1813: 1810: 1807: 1804: 1801: 1798: 1795: 1792: 1789: 1786: 1783: 1780: 1777: 1774: 1771: 1768: 1765: 1762: 1759: 1756: 1753: 1750: 1747: 1744: 1741: 1738: 1735: 1732: 1729: 1726: 1723: 1720: 1717: 1714: 1711: 1708: 1705: 1702: 1699: 1696: 1693: 1690: 1687: 1684: 1681: 1678: 1675: 1672: 1669: 1666: 1663: 1660: 1657: 1654: 1651: 1648: 1645: 1642: 1639: 1636: 1633: 1630: 1627: 1624: 1621: 1618: 1615: 1612: 1609: 1606: 1603: 1600: 1597: 1594: 1591: 1588: 1585: 1584:<getopt.h> 1582: 1579: 1576: 1575:<stdlib.h> 1573: 1570: 1569:/* for printf */ 1567: 1564: 1549: 1546: 1543: 1540: 1537: 1534: 1531: 1528: 1525: 1522: 1519: 1516: 1513: 1510: 1507: 1504: 1501: 1498: 1495: 1492: 1489: 1486: 1483: 1480: 1477: 1474: 1471: 1468: 1465: 1462: 1459: 1456: 1453: 1450: 1447: 1444: 1441: 1438: 1435: 1432: 1429: 1426: 1423: 1420: 1417: 1414: 1411: 1408: 1405: 1402: 1399: 1396: 1393: 1390: 1387: 1384: 1381: 1378: 1375: 1372: 1369: 1366: 1363: 1360: 1357: 1354: 1351: 1348: 1345: 1342: 1339: 1336: 1333: 1330: 1327: 1324: 1321: 1318: 1315: 1312: 1309: 1306: 1303: 1300: 1297: 1294: 1291: 1288: 1285: 1282: 1279: 1276: 1273: 1270: 1267: 1264: 1261: 1258: 1255: 1252: 1249: 1246: 1243: 1240: 1237: 1234: 1231: 1228: 1225: 1222: 1219: 1216: 1213: 1210: 1207: 1204: 1201: 1198: 1195: 1192: 1189: 1186: 1183: 1180: 1177: 1174: 1171: 1168: 1165: 1162: 1159: 1156: 1153: 1150: 1147: 1144: 1141: 1138: 1135: 1132: 1129: 1126: 1123: 1120: 1117: 1114: 1111: 1108: 1105: 1102: 1099: 1096: 1093: 1090: 1087: 1084: 1081: 1078: 1075: 1072: 1069: 1066: 1063: 1060: 1057: 1054: 1051: 1048: 1045: 1042: 1039: 1036: 1033: 1030: 1027: 1024: 1021: 1018: 1015: 1012: 1009: 1006: 1003: 1000: 997: 994: 991: 988: 985: 982: 979: 976: 973: 970: 967: 964: 961: 958: 955: 952: 949: 946: 943: 940: 937: 934: 931: 928: 925: 922: 919: 916: 913: 910: 907: 904: 901: 898: 895: 892: 889: 886: 883: 880: 877: 874: 871: 868: 865: 864:/* for getopt */ 862: 861:<unistd.h> 859: 856: 853: 852:<stdlib.h> 850: 847: 846:/* for printf */ 844: 841: 821: 818: 815: 812: 809: 806: 803: 800: 797: 794: 791: 788: 785: 782: 779: 776: 773: 770: 767: 764: 761: 758: 755: 752: 749: 746: 743: 742:<getopt.h> 740: 733: 725: 705: 688: 685: 682: 679: 676: 673: 670: 667: 664: 661: 658: 655: 652: 645: 641: 633: 627: 623: 619: 615: 611: 607: 603: 599: 595: 591: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 535:<unistd.h> 533: 518: 510: 503: 499: 495: 491: 487: 483: 479: 472: 468: 464: 457: 447: 426: 422: 398: 394: 390: 375: 371: 359: 348: 337: 333: 322: 295: 289: 285: 273: 266: 262: 258: 254: 250: 246: 242: 238: 234: 230: 183: 176: 165: 158: 154: 151: 145: 122: 121: 114: 107: 104: 98: 75: 67: 56: 34: 33: 26: 3521: 3520: 3516: 3515: 3514: 3512: 3511: 3510: 3501:C POSIX library 3491: 3490: 3472: 3467: 3452: 3451: 3447: 3438: 3436: 3432: 3431: 3427: 3415: 3413: 3409: 3408: 3404: 3395: 3394: 3390: 3381: 3380: 3376: 3367: 3366: 3362: 3353: 3352: 3348: 3338: 3336: 3327: 3326: 3322: 3313: 3311: 3307: 3306: 3302: 3293: 3292: 3288: 3279: 3278: 3274: 3265: 3264: 3260: 3251: 3250: 3246: 3233: 3232: 3228: 3224:– User Commands 3210: 3209: 3208: 3204: 3191: 3190: 3186: 3169: 3168: 3167: 3163: 3146: 3145: 3144: 3133: 3125: 3118: 3113: 3112: 3108: 3098: 3096: 3088: 3087: 3083: 3070: 3069: 3065: 3052: 3051: 3047: 3030: 3029: 3028: 3024: 3014: 3012: 3004: 3003: 2990: 2973: 2972: 2971: 2967: 2958: 2956: 2943: 2942: 2938: 2929: 2927: 2914: 2913: 2909: 2900: 2898: 2884: 2883: 2879: 2862: 2856: 2854: 2841: 2840: 2836: 2832: 2689: 2654: 2647: 2643: 2621: 2615: 2610: 2609: 2606: 2603: 2600: 2597: 2594: 2591: 2588: 2585: 2582: 2579: 2576: 2573: 2570: 2567: 2564: 2561: 2559:"%s " 2558: 2555: 2552: 2549: 2546: 2543: 2540: 2537: 2534: 2531: 2528: 2525: 2522: 2519: 2516: 2513: 2510: 2507: 2504: 2501: 2498: 2495: 2492: 2489: 2486: 2483: 2480: 2477: 2474: 2471: 2468: 2465: 2462: 2459: 2456: 2453: 2450: 2447: 2444: 2441: 2438: 2435: 2432: 2429: 2426: 2423: 2420: 2417: 2414: 2411: 2408: 2405: 2402: 2399: 2396: 2393: 2390: 2387: 2384: 2381: 2378: 2375: 2372: 2369: 2366: 2363: 2360: 2357: 2354: 2351: 2348: 2345: 2342: 2339: 2336: 2333: 2330: 2327: 2324: 2321: 2318: 2315: 2312: 2309: 2306: 2303: 2300: 2297: 2294: 2291: 2288: 2285: 2282: 2279: 2276: 2273: 2270: 2267: 2264: 2261: 2258: 2255: 2252: 2249: 2246: 2243: 2240: 2237: 2234: 2232:"option %c 2231: 2228: 2225: 2222: 2219: 2216: 2213: 2210: 2207: 2204: 2201: 2198: 2195: 2192: 2189: 2186: 2183: 2180: 2177: 2174: 2171: 2168: 2165: 2162: 2159: 2156: 2153: 2150: 2147: 2144: 2141: 2138: 2135: 2132: 2129: 2126: 2123: 2120: 2117: 2114: 2111: 2108: 2105: 2102: 2099: 2096: 2093: 2090: 2087: 2084: 2081: 2078: 2075: 2072: 2069: 2066: 2063: 2060: 2057: 2054: 2051: 2048: 2045: 2042: 2039: 2036: 2033: 2030: 2027: 2024: 2021: 2018: 2015: 2012: 2009: 2006: 2003: 2000: 1997: 1994: 1991: 1988: 1985: 1982: 1979: 1976: 1973: 1970: 1967: 1964: 1961: 1958: 1955: 1952: 1949: 1946: 1943: 1940: 1937: 1934: 1931: 1928: 1925: 1922: 1919: 1916: 1913: 1910: 1907: 1904: 1901: 1898: 1895: 1892: 1889: 1886: 1883: 1880: 1877: 1874: 1871: 1868: 1865: 1862: 1859: 1856: 1853: 1850: 1847: 1844: 1841: 1838: 1835: 1832: 1829: 1826: 1823: 1820: 1817: 1814: 1811: 1808: 1805: 1802: 1799: 1796: 1793: 1790: 1787: 1784: 1781: 1778: 1775: 1772: 1769: 1766: 1763: 1760: 1757: 1754: 1751: 1748: 1745: 1742: 1739: 1736: 1733: 1731:"add" 1730: 1727: 1724: 1721: 1718: 1715: 1712: 1709: 1706: 1703: 1700: 1697: 1694: 1691: 1688: 1685: 1682: 1679: 1676: 1673: 1670: 1667: 1664: 1661: 1658: 1655: 1652: 1649: 1646: 1643: 1640: 1637: 1634: 1631: 1628: 1625: 1622: 1619: 1616: 1613: 1610: 1607: 1604: 1601: 1598: 1595: 1592: 1589: 1586: 1583: 1580: 1577: 1574: 1571: 1568: 1566:<stdio.h> 1565: 1562: 1559: 1551: 1550: 1547: 1544: 1541: 1538: 1535: 1532: 1529: 1526: 1523: 1520: 1517: 1514: 1511: 1508: 1505: 1502: 1500:"%s " 1499: 1496: 1493: 1490: 1487: 1484: 1481: 1478: 1475: 1472: 1469: 1466: 1463: 1460: 1457: 1454: 1451: 1448: 1445: 1442: 1439: 1436: 1433: 1430: 1427: 1424: 1421: 1418: 1415: 1412: 1409: 1406: 1403: 1400: 1397: 1394: 1391: 1388: 1385: 1382: 1379: 1376: 1373: 1370: 1367: 1364: 1361: 1358: 1355: 1352: 1349: 1346: 1343: 1340: 1337: 1334: 1331: 1328: 1325: 1322: 1319: 1316: 1313: 1310: 1307: 1304: 1301: 1298: 1295: 1292: 1289: 1286: 1283: 1280: 1277: 1274: 1271: 1268: 1265: 1262: 1259: 1256: 1253: 1250: 1247: 1244: 1241: 1238: 1235: 1232: 1229: 1226: 1223: 1220: 1217: 1214: 1211: 1208: 1205: 1202: 1199: 1196: 1193: 1190: 1187: 1184: 1181: 1178: 1175: 1173:"option %c 1172: 1169: 1166: 1163: 1160: 1157: 1154: 1151: 1148: 1145: 1142: 1139: 1136: 1133: 1130: 1127: 1124: 1121: 1118: 1115: 1112: 1109: 1106: 1103: 1100: 1097: 1094: 1091: 1088: 1085: 1082: 1079: 1076: 1073: 1070: 1067: 1064: 1061: 1058: 1055: 1052: 1049: 1046: 1043: 1040: 1037: 1034: 1031: 1028: 1025: 1022: 1019: 1016: 1013: 1010: 1007: 1004: 1001: 998: 995: 992: 989: 986: 983: 980: 977: 974: 971: 968: 965: 962: 959: 956: 953: 950: 947: 944: 941: 938: 935: 932: 929: 926: 923: 920: 917: 914: 911: 908: 905: 902: 899: 896: 893: 890: 887: 884: 881: 878: 875: 872: 869: 866: 863: 860: 857: 854: 851: 848: 845: 843:<stdio.h> 842: 839: 836: 828: 823: 822: 819: 816: 813: 810: 807: 804: 801: 798: 795: 792: 789: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 741: 738: 731: 723: 703: 690: 689: 686: 683: 680: 677: 674: 671: 668: 665: 662: 659: 656: 653: 650: 631: 625: 621: 617: 613: 609: 605: 601: 597: 593: 589: 586: 585: 582: 579: 576: 573: 570: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 534: 531: 525: 523:For programmers 519:specification. 508: 501: 497: 493: 492:is the same as 489: 485: 481: 477: 470: 469:is the same as 466: 462: 455: 445: 438: 433: 424: 420: 396: 392: 388: 369: 357: 346: 344: 320: 293: 287: 283: 271: 264: 260: 256: 252: 248: 244: 240: 236: 232: 228: 221: 184: 173: 172: 171: 166: 155: 149: 146: 135: 123: 119: 108: 102: 99: 92: 80:This article's 76: 35: 31: 24: 17: 12: 11: 5: 3519: 3517: 3509: 3508: 3503: 3493: 3492: 3489: 3488: 3483: 3478: 3471: 3470:External links 3468: 3466: 3465: 3445: 3425: 3402: 3388: 3374: 3360: 3346: 3333:Apache Commons 3320: 3300: 3286: 3272: 3258: 3253:"Package flag" 3244: 3239:Stack Overflow 3226: 3202: 3184: 3161: 3131: 3128:on 2020-06-27. 3106: 3081: 3063: 3045: 3032:getopt_long(3) 3022: 2988: 2965: 2949:The Open Group 2936: 2920:The Open Group 2907: 2877: 2833: 2831: 2828: 2827: 2826: 2823:.NET Framework 2820: 2814: 2804: 2798: 2792: 2786: 2776: 2773:Apache Commons 2766: 2760: 2742: 2736: 2735: 2734: 2727: 2688: 2685: 2617:Main article: 2614: 2611: 2316:"option b 2271:"option a 1578:/* for exit */ 1561: 1558: 1552: 1257:"option b 1212:"option a 855:/* for exit */ 838: 835: 829: 827: 824: 737: 720: 719: 716: 713: 710: 707: 700: 697: 694: 649: 530: 524: 521: 513: 512: 505: 474: 459: 452: 449: 437: 434: 432: 429: 343: 340: 220: 217: 203:used to parse 186: 185: 168: 167: 126: 124: 117: 110: 109: 89:the key points 79: 77: 70: 65: 39: 38: 36: 29: 15: 13: 10: 9: 6: 4: 3: 2: 3518: 3507: 3504: 3502: 3499: 3498: 3496: 3487: 3484: 3482: 3479: 3477: 3474: 3473: 3469: 3461: 3460: 3455: 3449: 3446: 3435: 3429: 3426: 3423: 3412: 3406: 3403: 3398: 3392: 3389: 3384: 3378: 3375: 3370: 3364: 3361: 3356: 3350: 3347: 3334: 3330: 3329:"Commons CLI" 3324: 3321: 3310: 3304: 3301: 3296: 3290: 3287: 3282: 3276: 3273: 3268: 3262: 3259: 3254: 3248: 3245: 3240: 3236: 3230: 3227: 3223: 3219: 3216: –  3213: 3206: 3203: 3198: 3194: 3188: 3185: 3182: 3178: 3175: –  3172: 3165: 3162: 3159: 3155: 3152: –  3149: 3142: 3140: 3138: 3136: 3132: 3124: 3117: 3110: 3107: 3095: 3091: 3085: 3082: 3077: 3073: 3067: 3064: 3059: 3055: 3049: 3046: 3043: 3039: 3036: –  3033: 3026: 3023: 3011: 3007: 3001: 2999: 2997: 2995: 2993: 2989: 2986: 2982: 2979: –  2976: 2969: 2966: 2954: 2950: 2946: 2940: 2937: 2925: 2921: 2917: 2911: 2908: 2896: 2892: 2888: 2881: 2878: 2873: 2867: 2852: 2848: 2844: 2838: 2835: 2829: 2824: 2821: 2818: 2815: 2812: 2808: 2805: 2802: 2799: 2796: 2793: 2790: 2787: 2784: 2780: 2777: 2774: 2770: 2767: 2764: 2761: 2746: 2743: 2740: 2737: 2728: 2725: 2721: 2713: 2712: 2710: 2706: 2698: 2697: 2696: 2693: 2686: 2684: 2682: 2674: 2670: 2666: 2664: 2660: 2651: 2640: 2635: 2633: 2629: 2624: 2620: 2612: 1557: 1553: 834: 830: 825: 735: 729: 717: 714: 711: 708: 701: 698: 695: 692: 691: 647: 636: 629: 528: 522: 520: 506: 475: 460: 453: 450: 443: 442: 441: 435: 430: 428: 416: 414: 410: 406: 402: 386: 382: 377: 367: 363: 354: 352: 341: 339: 328: 326: 318: 315: 311: 307: 303: 299: 291: 280: 276: 268: 226: 218: 216: 214: 210: 206: 202: 199: 196: 192: 182: 179: 164: 161: 153: 143: 139: 133: 132: 127:This article 125: 116: 115: 106: 96: 90: 88: 83: 78: 74: 69: 68: 63: 61: 54: 53: 48: 47: 42: 37: 28: 27: 22: 3457: 3448: 3437:. Retrieved 3428: 3421: 3414:. Retrieved 3405: 3391: 3377: 3363: 3349: 3337:. Retrieved 3332: 3323: 3312:. Retrieved 3303: 3289: 3275: 3261: 3247: 3238: 3229: 3205: 3196: 3187: 3164: 3123:the original 3109: 3097:. Retrieved 3093: 3084: 3076:POSIX.1-2018 3075: 3066: 3057: 3048: 3025: 3013:. Retrieved 3009: 2975:getsubopt(3) 2968: 2957:. Retrieved 2939: 2928:. Retrieved 2910: 2899:. Retrieved 2890: 2880: 2855:. Retrieved 2837: 2691: 2690: 2668: 2667: 2661:version 1 / 2652: 2636: 2627: 2625: 2622: 2214:digit_optind 2178:digit_optind 2166:digit_optind 2061:long_options 1980:option_index 1971:long_options 1923:option_index 1716:long_options 1635:digit_optind 1555: 1155:digit_optind 1119:digit_optind 1107:digit_optind 912:digit_optind 832: 721: 637: 630: 587: 526: 514: 439: 417: 412: 384: 378: 365: 355: 345: 329: 292: 269: 222: 190: 189: 174: 156: 147: 136:Please help 128: 100: 84: 82:lead section 57: 50: 44: 43:Please help 40: 3054:"getopt(3)" 2945:"getsubopt" 2789:Free Pascal 2783:Common Lisp 2451:'?' 2400:'d' 2349:'c' 2304:'b' 2259:'a' 2154:'2' 2145:'1' 2136:'0' 1947:getopt_long 1857:'c' 1818:no_argument 1764:no_argument 1556:getopt_long 1392:'?' 1341:'d' 1290:'c' 1245:'b' 1200:'a' 1095:'2' 1086:'1' 1077:'0' 748:getopt_long 728:header file 724:getopt_long 411:(extending 403:as well as 385:getopt_long 383:extension, 317:header file 3495:Categories 3439:2013-04-30 3416:2013-04-30 3314:2013-06-24 3099:24 January 3015:23 January 3010:GNU Gnulib 2959:2024-04-22 2930:2024-04-22 2901:2024-04-22 2857:2024-04-22 2847:System III 2830:References 2673:util-linux 2175:&& 1116:&& 409:Korn Shell 397:--name=arg 342:Extensions 150:March 2012 138:improve it 103:March 2012 46:improve it 3212:getopt(1) 3193:"getopts" 3171:getopt(1) 3148:getopt(3) 2724:reentrant 2653:In 1995, 817:longindex 787:optstring 732:longindex 602:optstring 588:Here the 580:optstring 436:For users 413:optstring 336:getsubopt 213:Unix-like 87:summarize 52:talk page 3339:June 24, 3006:"getopt" 2953:Archived 2951:. 2018. 2924:Archived 2922:. 2018. 2916:"getopt" 2895:Archived 2866:cite web 2851:Archived 2613:In Shell 1581:#include 1572:#include 1563:#include 858:#include 849:#include 840:#include 826:Examples 805:longopts 739:#include 610:"vf::o:" 532:#include 494:-a -b -e 421:optreset 366:anywhere 314:unistd.h 298:AT&T 275:function 237:-a -b -c 201:function 3177:FreeBSD 3038:FreeBSD 2981:FreeBSD 2845:. From 2763:Haskell 2677:getopts 2655:getopts 2639:getopts 2619:getopts 2463:default 1404:default 502:-b -e a 405:Solaris 401:FreeBSD 310:POSIX.2 249:-inum 3 219:History 198:library 142:sources 21:getopts 3459:GitHub 3222:Manual 3181:Manual 3158:Manual 3042:Manual 2985:Manual 2807:Python 2757:getopt 2753:getopt 2705:gnulib 2701:getopt 2692:getopt 2669:getopt 2663:X/Open 2648:OPTARG 2644:OPTIND 2628:getopt 2586:" 2580:" 2574:printf 2553:printf 2538:optind 2520:printf 2505:optind 2481:" 2469:printf 2436:optarg 2424:optarg 2418:" 2406:printf 2385:optarg 2373:optarg 2367:" 2355:printf 2322:" 2310:printf 2277:" 2265:printf 2238:" 2226:printf 2205:" 2193:printf 2121:" 2115:" 2109:printf 2100:optarg 2088:printf 2079:optarg 2049:printf 2025:switch 2013:optind 2007:optind 1713:option 1710:struct 1707:static 1527:" 1521:" 1515:printf 1494:printf 1479:optind 1461:printf 1446:optind 1422:" 1410:printf 1377:optarg 1365:optarg 1359:" 1347:printf 1326:optarg 1314:optarg 1308:" 1296:printf 1263:" 1251:printf 1218:" 1206:printf 1179:" 1167:printf 1146:" 1134:printf 1059:switch 1047:optind 1041:optind 996:getopt 833:getopt 799:option 796:struct 715:optopt 709:opterr 699:optind 693:optarg 684:optopt 678:opterr 672:optind 666:extern 660:optarg 651:extern 644:getopt 640:extern 632:getopt 541:getopt 496:, but 471:-o foo 427:to 0. 425:optind 389:--inum 370:getopt 358:getopt 351:gnulib 347:getopt 332:getopt 321:getopt 302:4.3BSD 294:getopt 284:-a arg 272:getopt 253:-a=arg 245:-a arg 191:Getopt 3220:User 3218:Linux 3154:Linux 3126:(PDF) 3119:(PDF) 2709:MinGW 2681:ksh93 2532:while 2457:break 2442:break 2391:break 2340:break 2295:break 2250:break 2127:break 1977:& 1935:while 1473:while 1398:break 1383:break 1332:break 1281:break 1236:break 1191:break 984:while 793:const 778:const 769:const 571:const 562:const 467:-ofoo 431:Usage 362:glibc 306:Linux 288:-aarg 241:-inum 209:POSIX 193:is a 3341:2013 3101:2020 3017:2020 2872:link 2817:Ruby 2779:Lisp 2775:CLI. 2769:Java 2749:flag 2731:argp 2729:The 2716:popt 2714:The 2707:and 2646:and 2595:exit 2565:argv 2544:argc 2541:< 2511:argc 2508:< 2448:case 2430:dopt 2397:case 2379:copt 2346:case 2328:bopt 2301:case 2283:aopt 2256:case 2151:case 2142:case 2133:case 2067:name 2040:case 1959:argv 1953:argc 1905:NULL 1893:NULL 1878:NULL 1851:NULL 1824:NULL 1797:NULL 1770:NULL 1743:NULL 1695:dopt 1680:copt 1674:char 1662:bopt 1650:aopt 1614:argv 1608:char 1602:argc 1593:main 1536:exit 1506:argv 1485:argc 1482:< 1452:argc 1449:< 1389:case 1371:dopt 1338:case 1320:copt 1287:case 1269:bopt 1242:case 1224:aopt 1197:case 1092:case 1083:case 1074:case 1008:argv 1002:argc 972:dopt 957:copt 951:char 939:bopt 927:aopt 891:argv 885:char 879:argc 870:main 781:char 772:argv 763:char 757:argc 704:argv 654:char 598:main 594:argv 592:and 590:argc 574:char 565:argv 556:char 550:argc 517:CLIP 498:-bea 490:-abe 480:and 393:-abc 270:The 233:-abc 2801:PHP 1998:int 1920:int 1647:int 1632:int 1623:int 1599:int 1590:int 1032:int 924:int 909:int 900:int 876:int 867:int 811:int 754:int 745:int 669:int 547:int 538:int 381:GNU 334:is 286:or 267:). 3497:: 3456:. 3331:. 3237:. 3195:. 3134:^ 3092:. 3074:. 3056:. 3008:. 2991:^ 2893:. 2889:. 2868:}} 2864:{{ 2745:Go 2604:); 2589:); 2583:\n 2568:); 2529:); 2499:if 2490:); 2478:\n 2427:); 2415:\n 2376:); 2364:\n 2325:); 2319:\n 2280:); 2274:\n 2247:); 2235:\n 2208:); 2202:\n 2181:!= 2169:!= 2160:if 2124:); 2118:\n 2103:); 2073:if 2070:); 1989:-1 1986:!= 1983:)) 1938:(( 1917:}; 1887:}, 1860:}, 1833:}, 1806:}, 1779:}, 1752:}, 1611:** 1545:); 1530:); 1524:\n 1509:); 1470:); 1440:if 1431:); 1419:\n 1368:); 1356:\n 1317:); 1305:\n 1266:); 1260:\n 1221:); 1215:\n 1188:); 1176:\n 1149:); 1143:\n 1122:!= 1110:!= 1101:if 1023:-1 1020:!= 1017:)) 987:(( 888:** 820:); 646:: 583:); 509:-- 379:A 374:-- 304:, 265:/c 263:, 261:+b 259:, 257:-a 251:, 247:, 229:-a 55:. 3462:. 3442:. 3419:. 3399:. 3385:. 3371:. 3357:. 3343:. 3317:. 3297:. 3283:. 3269:. 3255:. 3241:. 3199:. 3103:. 3078:. 3060:. 3019:. 2962:. 2933:. 2904:. 2874:) 2860:. 2726:. 2607:} 2601:0 2598:( 2592:} 2577:( 2571:} 2562:, 2556:( 2550:{ 2547:) 2535:( 2523:( 2517:{ 2514:) 2502:( 2496:} 2493:} 2487:c 2484:, 2472:( 2466:: 2460:; 2454:: 2445:; 2439:; 2433:= 2421:, 2409:( 2403:: 2394:; 2388:; 2382:= 2370:, 2358:( 2352:: 2343:; 2337:; 2334:1 2331:= 2313:( 2307:: 2298:; 2292:; 2289:1 2286:= 2268:( 2262:: 2253:; 2244:c 2241:, 2229:( 2223:; 2217:= 2211:} 2196:( 2190:{ 2187:) 2172:0 2163:( 2157:: 2148:: 2139:: 2130:; 2112:( 2106:} 2097:, 2091:( 2085:{ 2082:) 2076:( 2064:. 2058:, 2052:( 2046:: 2043:0 2037:{ 2034:) 2031:c 2028:( 2022:; 2019:1 2016:: 2010:? 2004:= 1995:{ 1992:) 1974:, 1968:, 1962:, 1956:, 1950:( 1944:= 1941:c 1932:; 1929:0 1926:= 1914:} 1911:0 1908:, 1902:, 1899:0 1896:, 1890:{ 1884:0 1881:, 1875:, 1869:, 1863:{ 1854:, 1848:, 1842:, 1836:{ 1830:0 1827:, 1821:, 1815:, 1809:{ 1803:0 1800:, 1794:, 1788:, 1782:{ 1776:0 1773:, 1767:, 1761:, 1755:{ 1749:0 1746:, 1740:, 1734:, 1728:{ 1722:{ 1719:= 1704:; 1701:0 1698:= 1692:* 1689:, 1686:0 1683:= 1677:* 1671:; 1668:0 1665:= 1659:, 1656:0 1653:= 1644:; 1641:0 1638:= 1629:; 1626:c 1620:{ 1617:) 1605:, 1596:( 1548:} 1542:0 1539:( 1533:} 1518:( 1512:} 1503:, 1497:( 1491:{ 1488:) 1476:( 1464:( 1458:{ 1455:) 1443:( 1437:} 1434:} 1428:c 1425:, 1413:( 1407:: 1401:; 1395:: 1386:; 1380:; 1374:= 1362:, 1350:( 1344:: 1335:; 1329:; 1323:= 1311:, 1299:( 1293:: 1284:; 1278:; 1275:1 1272:= 1254:( 1248:: 1239:; 1233:; 1230:1 1227:= 1209:( 1203:: 1194:; 1185:c 1182:, 1170:( 1164:; 1158:= 1152:} 1137:( 1131:{ 1128:) 1113:0 1104:( 1098:: 1089:: 1080:: 1071:{ 1068:) 1065:c 1062:( 1056:; 1053:1 1050:: 1044:? 1038:= 1029:{ 1026:) 1011:, 1005:, 999:( 993:= 990:c 981:; 978:0 975:= 969:* 966:, 963:0 960:= 954:* 948:; 945:0 942:= 936:, 933:0 930:= 921:; 918:0 915:= 906:; 903:c 897:{ 894:) 882:, 873:( 814:* 808:, 802:* 790:, 784:* 775:, 766:* 760:, 751:( 706:. 687:; 681:, 675:, 663:; 657:* 626:W 622:o 618:f 614:v 606:W 577:* 568:, 559:* 553:, 544:( 486:e 482:b 478:a 473:. 463:o 456:: 446:- 282:( 195:C 181:) 175:( 163:) 157:( 152:) 148:( 134:. 105:) 101:( 91:. 62:) 58:( 23:.

Index

getopts
improve it
talk page
Learn how and when to remove these messages

lead section
summarize
provide an accessible overview
self-published sources
improve it
sources
Learn how and when to remove this message
Learn how and when to remove this message
C
library
function
command-line options
POSIX
Unix-like
command line programs
function
command-line options
AT&T
4.3BSD
Linux
POSIX.2
unistd.h
header file
programming languages
gnulib

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.