/usr/share/doc/libghc-options-doc/html/src/Options.html is in libghc-options-doc 1.2.1.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>lib/Options.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{-# LANGUAGE FlexibleInstances #-}</span>
<a name="line-2"></a><span class='hs-comment'>{-# LANGUAGE TypeSynonymInstances #-}</span>
<a name="line-3"></a>
<a name="line-4"></a><span class='hs-comment'>-- |</span>
<a name="line-5"></a><span class='hs-comment'>-- Module: Options</span>
<a name="line-6"></a><span class='hs-comment'>-- License: MIT</span>
<a name="line-7"></a><span class='hs-comment'>--</span>
<a name="line-8"></a><span class='hs-comment'>-- The @options@ package lets library and application developers easily work</span>
<a name="line-9"></a><span class='hs-comment'>-- with command-line options.</span>
<a name="line-10"></a><span class='hs-comment'>--</span>
<a name="line-11"></a><span class='hs-comment'>-- The following example is a full program that can accept two options,</span>
<a name="line-12"></a><span class='hs-comment'>-- @--message@ and @--quiet@:</span>
<a name="line-13"></a><span class='hs-comment'>--</span>
<a name="line-14"></a><span class='hs-comment'>-- @</span>
<a name="line-15"></a><span class='hs-comment'>--import Control.Applicative</span>
<a name="line-16"></a><span class='hs-comment'>--import Options</span>
<a name="line-17"></a><span class='hs-comment'>--</span>
<a name="line-18"></a><span class='hs-comment'>--data MainOptions = MainOptions</span>
<a name="line-19"></a><span class='hs-comment'>-- { optMessage :: String</span>
<a name="line-20"></a><span class='hs-comment'>-- , optQuiet :: Bool</span>
<a name="line-21"></a><span class='hs-comment'>-- }</span>
<a name="line-22"></a><span class='hs-comment'>--</span>
<a name="line-23"></a><span class='hs-comment'>--instance 'Options' MainOptions where</span>
<a name="line-24"></a><span class='hs-comment'>-- 'defineOptions' = pure MainOptions</span>
<a name="line-25"></a><span class='hs-comment'>-- \<*\> 'simpleOption' \"message\" \"Hello world!\"</span>
<a name="line-26"></a><span class='hs-comment'>-- \"A message to show the user.\"</span>
<a name="line-27"></a><span class='hs-comment'>-- \<*\> 'simpleOption' \"quiet\" False</span>
<a name="line-28"></a><span class='hs-comment'>-- \"Whether to be quiet.\"</span>
<a name="line-29"></a><span class='hs-comment'>--</span>
<a name="line-30"></a><span class='hs-comment'>--main :: IO ()</span>
<a name="line-31"></a><span class='hs-comment'>--main = 'runCommand' $ \\opts args -> do</span>
<a name="line-32"></a><span class='hs-comment'>-- if optQuiet opts</span>
<a name="line-33"></a><span class='hs-comment'>-- then return ()</span>
<a name="line-34"></a><span class='hs-comment'>-- else putStrLn (optMessage opts)</span>
<a name="line-35"></a><span class='hs-comment'>-- @</span>
<a name="line-36"></a><span class='hs-comment'>--</span>
<a name="line-37"></a><span class='hs-comment'>-- >$ ./hello</span>
<a name="line-38"></a><span class='hs-comment'>-- >Hello world!</span>
<a name="line-39"></a><span class='hs-comment'>-- >$ ./hello --message='ciao mondo'</span>
<a name="line-40"></a><span class='hs-comment'>-- >ciao mondo</span>
<a name="line-41"></a><span class='hs-comment'>-- >$ ./hello --quiet</span>
<a name="line-42"></a><span class='hs-comment'>-- >$</span>
<a name="line-43"></a><span class='hs-comment'>--</span>
<a name="line-44"></a><span class='hs-comment'>-- In addition, this library will automatically create documentation options</span>
<a name="line-45"></a><span class='hs-comment'>-- such as @--help@ and @--help-all@:</span>
<a name="line-46"></a><span class='hs-comment'>--</span>
<a name="line-47"></a><span class='hs-comment'>-- >$ ./hello --help</span>
<a name="line-48"></a><span class='hs-comment'>-- >Help Options:</span>
<a name="line-49"></a><span class='hs-comment'>-- > -h, --help</span>
<a name="line-50"></a><span class='hs-comment'>-- > Show option summary.</span>
<a name="line-51"></a><span class='hs-comment'>-- > --help-all</span>
<a name="line-52"></a><span class='hs-comment'>-- > Show all help options.</span>
<a name="line-53"></a><span class='hs-comment'>-- ></span>
<a name="line-54"></a><span class='hs-comment'>-- >Application Options:</span>
<a name="line-55"></a><span class='hs-comment'>-- > --message :: text</span>
<a name="line-56"></a><span class='hs-comment'>-- > A message to show the user.</span>
<a name="line-57"></a><span class='hs-comment'>-- > default: "Hello world!"</span>
<a name="line-58"></a><span class='hs-comment'>-- > --quiet :: bool</span>
<a name="line-59"></a><span class='hs-comment'>-- > Whether to be quiet.</span>
<a name="line-60"></a><span class='hs-comment'>-- > default: false</span>
<a name="line-61"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Options</span>
<a name="line-62"></a> <span class='hs-layout'>(</span>
<a name="line-63"></a> <span class='hs-comment'>-- * Defining options</span>
<a name="line-64"></a> <span class='hs-conid'>Options</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span>
<a name="line-65"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>defaultOptions</span>
<a name="line-66"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>simpleOption</span>
<a name="line-67"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>DefineOptions</span>
<a name="line-68"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>SimpleOptionType</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span>
<a name="line-69"></a>
<a name="line-70"></a> <span class='hs-comment'>-- * Defining subcommands</span>
<a name="line-71"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>Subcommand</span>
<a name="line-72"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>subcommand</span>
<a name="line-73"></a>
<a name="line-74"></a> <span class='hs-comment'>-- * Running main with options</span>
<a name="line-75"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>runCommand</span>
<a name="line-76"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>runSubcommand</span>
<a name="line-77"></a>
<a name="line-78"></a> <span class='hs-comment'>-- * Parsing argument lists</span>
<a name="line-79"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>Parsed</span>
<a name="line-80"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>parsedError</span>
<a name="line-81"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>parsedHelp</span>
<a name="line-82"></a>
<a name="line-83"></a> <span class='hs-comment'>-- ** Parsing options</span>
<a name="line-84"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>ParsedOptions</span>
<a name="line-85"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>parsedOptions</span>
<a name="line-86"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>parsedArguments</span>
<a name="line-87"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>parseOptions</span>
<a name="line-88"></a>
<a name="line-89"></a> <span class='hs-comment'>-- ** Parsing sub-commands</span>
<a name="line-90"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>ParsedSubcommand</span>
<a name="line-91"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>parsedSubcommand</span>
<a name="line-92"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>parseSubcommand</span>
<a name="line-93"></a>
<a name="line-94"></a> <span class='hs-comment'>-- * Advanced option definitions</span>
<a name="line-95"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>OptionType</span>
<a name="line-96"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>defineOption</span>
<a name="line-97"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>Option</span>
<a name="line-98"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionShortFlags</span>
<a name="line-99"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionLongFlags</span>
<a name="line-100"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionDefault</span>
<a name="line-101"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionDescription</span>
<a name="line-102"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionGroup</span>
<a name="line-103"></a>
<a name="line-104"></a> <span class='hs-comment'>-- ** Option groups</span>
<a name="line-105"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>Group</span>
<a name="line-106"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>group</span>
<a name="line-107"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>groupName</span>
<a name="line-108"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>groupTitle</span>
<a name="line-109"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>groupDescription</span>
<a name="line-110"></a>
<a name="line-111"></a> <span class='hs-comment'>-- * Option types</span>
<a name="line-112"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_bool</span>
<a name="line-113"></a>
<a name="line-114"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_string</span>
<a name="line-115"></a>
<a name="line-116"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_int</span>
<a name="line-117"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_int8</span>
<a name="line-118"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_int16</span>
<a name="line-119"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_int32</span>
<a name="line-120"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_int64</span>
<a name="line-121"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_word</span>
<a name="line-122"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_word8</span>
<a name="line-123"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_word16</span>
<a name="line-124"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_word32</span>
<a name="line-125"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_word64</span>
<a name="line-126"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_integer</span>
<a name="line-127"></a>
<a name="line-128"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_float</span>
<a name="line-129"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_double</span>
<a name="line-130"></a>
<a name="line-131"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_maybe</span>
<a name="line-132"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_list</span>
<a name="line-133"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_set</span>
<a name="line-134"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_map</span>
<a name="line-135"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType_enum</span>
<a name="line-136"></a>
<a name="line-137"></a> <span class='hs-comment'>-- ** Custom option types</span>
<a name="line-138"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionType</span>
<a name="line-139"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeName</span>
<a name="line-140"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeDefault</span>
<a name="line-141"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeParse</span>
<a name="line-142"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeShow</span>
<a name="line-143"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeUnary</span>
<a name="line-144"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeMerge</span>
<a name="line-145"></a> <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-146"></a>
<a name="line-147"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Applicative</span>
<a name="line-148"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span> <span class='hs-layout'>(</span><span class='hs-varid'>forM_</span><span class='hs-layout'>)</span>
<a name="line-149"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Error</span> <span class='hs-layout'>(</span><span class='hs-conid'>ErrorT</span><span class='hs-layout'>,</span> <span class='hs-varid'>runErrorT</span><span class='hs-layout'>,</span> <span class='hs-varid'>throwError</span><span class='hs-layout'>)</span>
<a name="line-150"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>IO</span><span class='hs-varop'>.</span><span class='hs-conid'>Class</span> <span class='hs-layout'>(</span><span class='hs-varid'>liftIO</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadIO</span><span class='hs-layout'>)</span>
<a name="line-151"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Functor</span><span class='hs-varop'>.</span><span class='hs-conid'>Identity</span>
<a name="line-152"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Int</span>
<a name="line-153"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>List</span> <span class='hs-layout'>(</span><span class='hs-varid'>intercalate</span><span class='hs-layout'>)</span>
<a name="line-154"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Map</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>Map</span>
<a name="line-155"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-varid'>isJust</span><span class='hs-layout'>)</span>
<a name="line-156"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Set</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>Set</span>
<a name="line-157"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Word</span>
<a name="line-158"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Environment</span>
<a name="line-159"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Exit</span> <span class='hs-layout'>(</span><span class='hs-varid'>exitFailure</span><span class='hs-layout'>,</span> <span class='hs-varid'>exitSuccess</span><span class='hs-layout'>)</span>
<a name="line-160"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>IO</span> <span class='hs-layout'>(</span><span class='hs-varid'>hPutStr</span><span class='hs-layout'>,</span> <span class='hs-varid'>hPutStrLn</span><span class='hs-layout'>,</span> <span class='hs-varid'>stderr</span><span class='hs-layout'>,</span> <span class='hs-varid'>stdout</span><span class='hs-layout'>)</span>
<a name="line-161"></a>
<a name="line-162"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Options</span><span class='hs-varop'>.</span><span class='hs-conid'>Help</span>
<a name="line-163"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Options</span><span class='hs-varop'>.</span><span class='hs-conid'>Tokenize</span>
<a name="line-164"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Options</span><span class='hs-varop'>.</span><span class='hs-conid'>Types</span>
<a name="line-165"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Options</span><span class='hs-varop'>.</span><span class='hs-conid'>Util</span> <span class='hs-layout'>(</span><span class='hs-varid'>mapEither</span><span class='hs-layout'>)</span>
<a name="line-166"></a>
<a name="line-167"></a><a name="Options"></a><span class='hs-comment'>-- | Options are defined together in a single data type, which will be an</span>
<a name="line-168"></a><a name="Options"></a><span class='hs-comment'>-- instance of 'Options'.</span>
<a name="line-169"></a><a name="Options"></a><span class='hs-comment'>--</span>
<a name="line-170"></a><a name="Options"></a><span class='hs-comment'>-- See 'defineOptions' for details on defining instances of 'Options'.</span>
<a name="line-171"></a><a name="Options"></a><span class='hs-keyword'>class</span> <span class='hs-conid'>Options</span> <span class='hs-varid'>opts</span> <span class='hs-keyword'>where</span>
<a name="line-172"></a> <span class='hs-comment'>-- | Defines the structure and metadata of the options in this type,</span>
<a name="line-173"></a> <span class='hs-comment'>-- including their types, flag names, and documentation.</span>
<a name="line-174"></a> <span class='hs-comment'>--</span>
<a name="line-175"></a> <span class='hs-comment'>-- Options with a basic type and a single flag name may be defined</span>
<a name="line-176"></a> <span class='hs-comment'>-- with 'simpleOption'. Options with more complex requirements may</span>
<a name="line-177"></a> <span class='hs-comment'>-- be defined with 'defineOption'.</span>
<a name="line-178"></a> <span class='hs-comment'>--</span>
<a name="line-179"></a> <span class='hs-comment'>-- Non-option fields in the type may be set using applicative functions</span>
<a name="line-180"></a> <span class='hs-comment'>-- such as 'pure'.</span>
<a name="line-181"></a> <span class='hs-comment'>--</span>
<a name="line-182"></a> <span class='hs-comment'>-- Options may be included from another type by using a nested call to</span>
<a name="line-183"></a> <span class='hs-comment'>-- 'defineOptions'.</span>
<a name="line-184"></a> <span class='hs-comment'>--</span>
<a name="line-185"></a> <span class='hs-comment'>-- Library authors are encouraged to aggregate their options into a</span>
<a name="line-186"></a> <span class='hs-comment'>-- few top-level types, so application authors can include it</span>
<a name="line-187"></a> <span class='hs-comment'>-- easily in their own option definitions.</span>
<a name="line-188"></a> <span class='hs-varid'>defineOptions</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>opts</span>
<a name="line-189"></a>
<a name="line-190"></a><a name="DefineOptions"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>a</span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span><span class='hs-layout'>,</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>OptionInfo</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-conid'>Map</span> <span class='hs-conid'>OptionKey</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Token</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span><span class='hs-layout'>,</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-191"></a>
<a name="line-192"></a><a name="instance%20Functor%20DefineOptions"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Functor</span> <span class='hs-conid'>DefineOptions</span> <span class='hs-keyword'>where</span>
<a name="line-193"></a> <span class='hs-varid'>fmap</span> <span class='hs-varid'>fn</span> <span class='hs-layout'>(</span><span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>defaultValue</span> <span class='hs-varid'>getInfo</span> <span class='hs-varid'>parse</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>DefineOptions</span> <span class='hs-layout'>(</span><span class='hs-varid'>fn</span> <span class='hs-varid'>defaultValue</span><span class='hs-layout'>)</span> <span class='hs-varid'>getInfo</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>key</span> <span class='hs-varid'>tokens</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parse</span> <span class='hs-varid'>key</span> <span class='hs-varid'>tokens</span> <span class='hs-keyword'>of</span>
<a name="line-194"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-195"></a> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key'</span><span class='hs-layout'>,</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key'</span><span class='hs-layout'>,</span> <span class='hs-varid'>fn</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-196"></a>
<a name="line-197"></a><a name="instance%20Applicative%20DefineOptions"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Applicative</span> <span class='hs-conid'>DefineOptions</span> <span class='hs-keyword'>where</span>
<a name="line-198"></a> <span class='hs-varid'>pure</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>a</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>key</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>key</span><span class='hs-layout'>,</span> <span class='hs-conid'>[]</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>key</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key</span><span class='hs-layout'>,</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-199"></a> <span class='hs-layout'>(</span><span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>acc_default</span> <span class='hs-varid'>acc_getInfo</span> <span class='hs-varid'>acc_parse</span><span class='hs-layout'>)</span> <span class='hs-varop'><*></span> <span class='hs-layout'>(</span><span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>defaultValue</span> <span class='hs-varid'>getInfo</span> <span class='hs-varid'>parse</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>DefineOptions</span>
<a name="line-200"></a> <span class='hs-layout'>(</span><span class='hs-varid'>acc_default</span> <span class='hs-varid'>defaultValue</span><span class='hs-layout'>)</span>
<a name="line-201"></a> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>key</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>acc_getInfo</span> <span class='hs-varid'>key</span> <span class='hs-keyword'>of</span>
<a name="line-202"></a> <span class='hs-layout'>(</span><span class='hs-varid'>key'</span><span class='hs-layout'>,</span> <span class='hs-varid'>infos</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>getInfo</span> <span class='hs-varid'>key'</span> <span class='hs-keyword'>of</span>
<a name="line-203"></a> <span class='hs-layout'>(</span><span class='hs-varid'>key''</span><span class='hs-layout'>,</span> <span class='hs-varid'>infos'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>key''</span><span class='hs-layout'>,</span> <span class='hs-varid'>infos</span> <span class='hs-varop'>++</span> <span class='hs-varid'>infos'</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-204"></a> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>key</span> <span class='hs-varid'>tokens</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>acc_parse</span> <span class='hs-varid'>key</span> <span class='hs-varid'>tokens</span> <span class='hs-keyword'>of</span>
<a name="line-205"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-206"></a> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key'</span><span class='hs-layout'>,</span> <span class='hs-varid'>fn</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parse</span> <span class='hs-varid'>key'</span> <span class='hs-varid'>tokens</span> <span class='hs-keyword'>of</span>
<a name="line-207"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-208"></a> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key''</span><span class='hs-layout'>,</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key''</span><span class='hs-layout'>,</span> <span class='hs-varid'>fn</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-209"></a>
<a name="line-210"></a><a name="defaultOptions"></a><span class='hs-comment'>-- | An options value containing only the default values for each option.</span>
<a name="line-211"></a><span class='hs-comment'>-- This is equivalent to the options value when parsing an empty argument</span>
<a name="line-212"></a><span class='hs-comment'>-- list.</span>
<a name="line-213"></a><span class='hs-definition'>defaultOptions</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Options</span> <span class='hs-varid'>opts</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>opts</span>
<a name="line-214"></a><span class='hs-definition'>defaultOptions</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>defineOptions</span> <span class='hs-keyword'>of</span>
<a name="line-215"></a> <span class='hs-layout'>(</span><span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>def</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>def</span>
<a name="line-216"></a>
<a name="line-217"></a><a name="OptionType"></a><span class='hs-comment'>-- | An option's type determines how the option will be parsed, and which</span>
<a name="line-218"></a><a name="OptionType"></a><span class='hs-comment'>-- Haskell type the parsed value will be stored as. There are many types</span>
<a name="line-219"></a><a name="OptionType"></a><span class='hs-comment'>-- available, covering most basic types and a few more advanced types.</span>
<a name="line-220"></a><a name="OptionType"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>OptionType</span>
<a name="line-221"></a> <span class='hs-layout'>{</span>
<a name="line-222"></a> <span class='hs-comment'>-- | The name of this option type; used in @--help@ output.</span>
<a name="line-223"></a> <span class='hs-varid'>optionTypeName</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span>
<a name="line-224"></a>
<a name="line-225"></a> <span class='hs-comment'>-- | The default value for options of this type. This will be used</span>
<a name="line-226"></a> <span class='hs-comment'>-- if 'optionDefault' is not set when defining the option.</span>
<a name="line-227"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeDefault</span> <span class='hs-keyglyph'>::</span> <span class='hs-varid'>val</span>
<a name="line-228"></a>
<a name="line-229"></a> <span class='hs-comment'>-- | Try to parse the given string to an option value. If parsing</span>
<a name="line-230"></a> <span class='hs-comment'>-- fails, an error message will be returned.</span>
<a name="line-231"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeParse</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-varid'>val</span>
<a name="line-232"></a>
<a name="line-233"></a> <span class='hs-comment'>-- | Format the value for display; used in @--help@ output.</span>
<a name="line-234"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeShow</span> <span class='hs-keyglyph'>::</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-235"></a>
<a name="line-236"></a> <span class='hs-comment'>-- | If not Nothing, then options of this type may be set by a unary</span>
<a name="line-237"></a> <span class='hs-comment'>-- flag. The option will be parsed as if the given value were set.</span>
<a name="line-238"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeUnary</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Maybe</span> <span class='hs-varid'>val</span>
<a name="line-239"></a>
<a name="line-240"></a> <span class='hs-comment'>-- | If not Nothing, then options of this type may be set with repeated</span>
<a name="line-241"></a> <span class='hs-comment'>-- flags. Each flag will be parsed with 'optionTypeParse', and the</span>
<a name="line-242"></a> <span class='hs-comment'>-- resulting parsed values will be passed to this function for merger</span>
<a name="line-243"></a> <span class='hs-comment'>-- into the final value.</span>
<a name="line-244"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionTypeMerge</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>[</span><span class='hs-varid'>val</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>val</span><span class='hs-layout'>)</span>
<a name="line-245"></a> <span class='hs-layout'>}</span>
<a name="line-246"></a>
<a name="line-247"></a><a name="group"></a><span class='hs-comment'>-- | Define an option group with the given name and title. Use</span>
<a name="line-248"></a><span class='hs-comment'>-- 'groupDescription' to add additional descriptive text, if needed.</span>
<a name="line-249"></a><span class='hs-definition'>group</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-comment'>-- ^ Name</span>
<a name="line-250"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-comment'>-- ^ Title; see 'groupTitle'.</span>
<a name="line-251"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-comment'>-- ^ Description; see 'groupDescription'.</span>
<a name="line-252"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Group</span>
<a name="line-253"></a><span class='hs-definition'>group</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Group</span>
<a name="line-254"></a>
<a name="line-255"></a><a name="optionType"></a><span class='hs-comment'>-- | Define a new option type with the given name, default, and behavior.</span>
<a name="line-256"></a><span class='hs-definition'>optionType</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-comment'>-- ^ Name</span>
<a name="line-257"></a> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>val</span> <span class='hs-comment'>-- ^ Default value</span>
<a name="line-258"></a> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-varid'>val</span><span class='hs-layout'>)</span> <span class='hs-comment'>-- ^ Parser</span>
<a name="line-259"></a> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span><span class='hs-layout'>)</span> <span class='hs-comment'>-- ^ Formatter</span>
<a name="line-260"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>val</span>
<a name="line-261"></a><span class='hs-definition'>optionType</span> <span class='hs-varid'>name</span> <span class='hs-varid'>def</span> <span class='hs-varid'>parse</span> <span class='hs-varid'>show'</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>name</span> <span class='hs-varid'>def</span> <span class='hs-varid'>parse</span> <span class='hs-varid'>show'</span> <span class='hs-conid'>Nothing</span> <span class='hs-conid'>Nothing</span>
<a name="line-262"></a>
<a name="line-263"></a><a name="SimpleOptionType"></a><span class='hs-keyword'>class</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-varid'>a</span> <span class='hs-keyword'>where</span>
<a name="line-264"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>a</span>
<a name="line-265"></a>
<a name="line-266"></a><a name="instance%20SimpleOptionType%20Bool"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Bool</span> <span class='hs-keyword'>where</span>
<a name="line-267"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_bool</span>
<a name="line-268"></a>
<a name="line-269"></a><a name="optionType_bool"></a><span class='hs-comment'>-- | Store an option as a @'Bool'@. The option's value must be either</span>
<a name="line-270"></a><span class='hs-comment'>-- @\"true\"@ or @\"false\"@.</span>
<a name="line-271"></a><span class='hs-comment'>--</span>
<a name="line-272"></a><span class='hs-comment'>-- Boolean options are unary, which means that their value is optional when</span>
<a name="line-273"></a><span class='hs-comment'>-- specified on the command line. If a flag is present, the option is set to</span>
<a name="line-274"></a><span class='hs-comment'>-- True.</span>
<a name="line-275"></a><span class='hs-comment'>--</span>
<a name="line-276"></a><span class='hs-comment'>-- >$ ./app -q</span>
<a name="line-277"></a><span class='hs-comment'>-- >$ ./app --quiet</span>
<a name="line-278"></a><span class='hs-comment'>--</span>
<a name="line-279"></a><span class='hs-comment'>-- Boolean options may still be specified explicitly by using long flags with</span>
<a name="line-280"></a><span class='hs-comment'>-- the @--flag=value@ format. This is the only way to set a unary flag to</span>
<a name="line-281"></a><span class='hs-comment'>-- @\"false\"@.</span>
<a name="line-282"></a><span class='hs-comment'>--</span>
<a name="line-283"></a><span class='hs-comment'>-- >$ ./app --quiet=true</span>
<a name="line-284"></a><span class='hs-comment'>-- >$ ./app --quiet=false</span>
<a name="line-285"></a><span class='hs-definition'>optionType_bool</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Bool</span>
<a name="line-286"></a><span class='hs-definition'>optionType_bool</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionType</span> <span class='hs-str'>"bool"</span> <span class='hs-conid'>False</span> <span class='hs-varid'>parseBool</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>x</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>if</span> <span class='hs-varid'>x</span> <span class='hs-keyword'>then</span> <span class='hs-str'>"true"</span> <span class='hs-keyword'>else</span> <span class='hs-str'>"false"</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-287"></a> <span class='hs-layout'>{</span> <span class='hs-varid'>optionTypeUnary</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-conid'>True</span>
<a name="line-288"></a> <span class='hs-layout'>}</span>
<a name="line-289"></a>
<a name="line-290"></a><a name="parseBool"></a><span class='hs-definition'>parseBool</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-conid'>Bool</span>
<a name="line-291"></a><span class='hs-definition'>parseBool</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>of</span>
<a name="line-292"></a> <span class='hs-str'>"true"</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-conid'>True</span>
<a name="line-293"></a> <span class='hs-str'>"false"</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-conid'>False</span>
<a name="line-294"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-varid'>show</span> <span class='hs-varid'>s</span> <span class='hs-varop'>++</span> <span class='hs-str'>" is not in {\"true\", \"false\"}."</span><span class='hs-layout'>)</span>
<a name="line-295"></a>
<a name="line-296"></a><a name="instance%20SimpleOptionType%20String"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>String</span> <span class='hs-keyword'>where</span>
<a name="line-297"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_string</span>
<a name="line-298"></a>
<a name="line-299"></a><a name="optionType_string"></a><span class='hs-comment'>-- | Store an option value as a @'String'@. The value is decoded to Unicode</span>
<a name="line-300"></a><span class='hs-comment'>-- first, if needed. The value may contain non-Unicode bytes, in which case</span>
<a name="line-301"></a><span class='hs-comment'>-- they will be stored using GHC 7.4's encoding for mixed-use strings.</span>
<a name="line-302"></a><span class='hs-definition'>optionType_string</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>String</span>
<a name="line-303"></a><span class='hs-definition'>optionType_string</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-str'>"text"</span> <span class='hs-str'>""</span> <span class='hs-conid'>Right</span> <span class='hs-varid'>show</span>
<a name="line-304"></a>
<a name="line-305"></a><a name="instance%20SimpleOptionType%20Integer"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Integer</span> <span class='hs-keyword'>where</span>
<a name="line-306"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_integer</span>
<a name="line-307"></a>
<a name="line-308"></a><a name="optionType_integer"></a><span class='hs-comment'>-- | Store an option as an @'Integer'@. The option value must be an integer.</span>
<a name="line-309"></a><span class='hs-comment'>-- There is no minimum or maximum value.</span>
<a name="line-310"></a><span class='hs-definition'>optionType_integer</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Integer</span>
<a name="line-311"></a><span class='hs-definition'>optionType_integer</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-str'>"integer"</span> <span class='hs-num'>0</span> <span class='hs-varid'>parseInteger</span> <span class='hs-varid'>show</span>
<a name="line-312"></a>
<a name="line-313"></a><a name="parseInteger"></a><span class='hs-definition'>parseInteger</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-conid'>Integer</span>
<a name="line-314"></a><span class='hs-definition'>parseInteger</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parsed</span> <span class='hs-keyword'>where</span>
<a name="line-315"></a> <span class='hs-varid'>parsed</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>valid</span>
<a name="line-316"></a> <span class='hs-keyword'>then</span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>read</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span>
<a name="line-317"></a> <span class='hs-keyword'>else</span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-varid'>show</span> <span class='hs-varid'>s</span> <span class='hs-varop'>++</span> <span class='hs-str'>" is not an integer."</span><span class='hs-layout'>)</span>
<a name="line-318"></a> <span class='hs-varid'>valid</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>of</span>
<a name="line-319"></a> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>False</span>
<a name="line-320"></a> <span class='hs-chr'>'-'</span><span class='hs-conop'>:</span><span class='hs-varid'>s'</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>allDigits</span> <span class='hs-varid'>s'</span>
<a name="line-321"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>allDigits</span> <span class='hs-varid'>s</span>
<a name="line-322"></a> <span class='hs-varid'>allDigits</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>all</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>c</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>c</span> <span class='hs-varop'>>=</span> <span class='hs-chr'>'0'</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>c</span> <span class='hs-varop'><=</span> <span class='hs-chr'>'9'</span><span class='hs-layout'>)</span>
<a name="line-323"></a>
<a name="line-324"></a><a name="parseBoundedIntegral"></a><span class='hs-definition'>parseBoundedIntegral</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Bounded</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Integral</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-varid'>a</span>
<a name="line-325"></a><span class='hs-definition'>parseBoundedIntegral</span> <span class='hs-varid'>label</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parse</span> <span class='hs-keyword'>where</span>
<a name="line-326"></a> <span class='hs-varid'>getBounds</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Bounded</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Integral</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-layout'>(</span><span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span><span class='hs-layout'>,</span> <span class='hs-conid'>Integer</span><span class='hs-layout'>)</span>
<a name="line-327"></a> <span class='hs-varid'>getBounds</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>min'</span> <span class='hs-varid'>max'</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>toInteger</span> <span class='hs-varid'>min'</span><span class='hs-layout'>,</span> <span class='hs-varid'>toInteger</span> <span class='hs-varid'>max'</span><span class='hs-layout'>)</span>
<a name="line-328"></a>
<a name="line-329"></a> <span class='hs-layout'>(</span><span class='hs-varid'>minInt</span><span class='hs-layout'>,</span> <span class='hs-varid'>maxInt</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>getBounds</span> <span class='hs-varid'>parse</span> <span class='hs-varid'>minBound</span> <span class='hs-varid'>maxBound</span>
<a name="line-330"></a>
<a name="line-331"></a> <span class='hs-varid'>parse</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parseInteger</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>of</span>
<a name="line-332"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-333"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>int</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>if</span> <span class='hs-varid'>int</span> <span class='hs-varop'><</span> <span class='hs-varid'>minInt</span> <span class='hs-varop'>||</span> <span class='hs-varid'>int</span> <span class='hs-varop'>></span> <span class='hs-varid'>maxInt</span>
<a name="line-334"></a> <span class='hs-keyword'>then</span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-varid'>show</span> <span class='hs-varid'>int</span> <span class='hs-varop'>++</span> <span class='hs-str'>" is not within bounds ["</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>minInt</span> <span class='hs-varop'>++</span> <span class='hs-str'>":"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>maxInt</span> <span class='hs-varop'>++</span> <span class='hs-str'>"] of type "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>label</span> <span class='hs-varop'>++</span> <span class='hs-str'>"."</span><span class='hs-layout'>)</span>
<a name="line-335"></a> <span class='hs-keyword'>else</span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromInteger</span> <span class='hs-varid'>int</span><span class='hs-layout'>)</span>
<a name="line-336"></a>
<a name="line-337"></a><a name="optionTypeBoundedInt"></a><span class='hs-definition'>optionTypeBoundedInt</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Bounded</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Integral</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>a</span>
<a name="line-338"></a><span class='hs-definition'>optionTypeBoundedInt</span> <span class='hs-varid'>tName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-varid'>tName</span> <span class='hs-num'>0</span> <span class='hs-layout'>(</span><span class='hs-varid'>parseBoundedIntegral</span> <span class='hs-varid'>tName</span><span class='hs-layout'>)</span> <span class='hs-varid'>show</span>
<a name="line-339"></a>
<a name="line-340"></a><a name="instance%20SimpleOptionType%20Int"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Int</span> <span class='hs-keyword'>where</span>
<a name="line-341"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_int</span>
<a name="line-342"></a>
<a name="line-343"></a><a name="optionType_int"></a><span class='hs-comment'>-- | Store an option as an @'Int'@. The option value must be an integer /n/</span>
<a name="line-344"></a><span class='hs-comment'>-- such that @'minBound' <= n <= 'maxBound'@.</span>
<a name="line-345"></a><span class='hs-definition'>optionType_int</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Int</span>
<a name="line-346"></a><span class='hs-definition'>optionType_int</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"int"</span>
<a name="line-347"></a>
<a name="line-348"></a><a name="instance%20SimpleOptionType%20Int8"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Int8</span> <span class='hs-keyword'>where</span>
<a name="line-349"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_int8</span>
<a name="line-350"></a>
<a name="line-351"></a><a name="optionType_int8"></a><span class='hs-comment'>-- | Store an option as an @'Int8'@. The option value must be an integer /n/</span>
<a name="line-352"></a><span class='hs-comment'>-- such that @'minBound' <= n <= 'maxBound'@.</span>
<a name="line-353"></a><span class='hs-definition'>optionType_int8</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Int8</span>
<a name="line-354"></a><span class='hs-definition'>optionType_int8</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"int8"</span>
<a name="line-355"></a>
<a name="line-356"></a><a name="instance%20SimpleOptionType%20Int16"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Int16</span> <span class='hs-keyword'>where</span>
<a name="line-357"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_int16</span>
<a name="line-358"></a>
<a name="line-359"></a><a name="optionType_int16"></a><span class='hs-comment'>-- | Store an option as an @'Int16'@. The option value must be an integer /n/</span>
<a name="line-360"></a><span class='hs-comment'>-- such that @'minBound' <= n <= 'maxBound'@.</span>
<a name="line-361"></a><span class='hs-definition'>optionType_int16</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Int16</span>
<a name="line-362"></a><span class='hs-definition'>optionType_int16</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"int16"</span>
<a name="line-363"></a>
<a name="line-364"></a><a name="instance%20SimpleOptionType%20Int32"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Int32</span> <span class='hs-keyword'>where</span>
<a name="line-365"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_int32</span>
<a name="line-366"></a>
<a name="line-367"></a><a name="optionType_int32"></a><span class='hs-comment'>-- | Store an option as an @'Int32'@. The option value must be an integer /n/</span>
<a name="line-368"></a><span class='hs-comment'>-- such that @'minBound' <= n <= 'maxBound'@.</span>
<a name="line-369"></a><span class='hs-definition'>optionType_int32</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Int32</span>
<a name="line-370"></a><span class='hs-definition'>optionType_int32</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"int32"</span>
<a name="line-371"></a>
<a name="line-372"></a><a name="instance%20SimpleOptionType%20Int64"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Int64</span> <span class='hs-keyword'>where</span>
<a name="line-373"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_int64</span>
<a name="line-374"></a>
<a name="line-375"></a><a name="optionType_int64"></a><span class='hs-comment'>-- | Store an option as an @'Int64'@. The option value must be an integer /n/</span>
<a name="line-376"></a><span class='hs-comment'>-- such that @'minBound' <= n <= 'maxBound'@.</span>
<a name="line-377"></a><span class='hs-definition'>optionType_int64</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Int64</span>
<a name="line-378"></a><span class='hs-definition'>optionType_int64</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"int64"</span>
<a name="line-379"></a>
<a name="line-380"></a><a name="instance%20SimpleOptionType%20Word"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Word</span> <span class='hs-keyword'>where</span>
<a name="line-381"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_word</span>
<a name="line-382"></a>
<a name="line-383"></a><a name="optionType_word"></a><span class='hs-comment'>-- | Store an option as a @'Word'@. The option value must be a positive</span>
<a name="line-384"></a><span class='hs-comment'>-- integer /n/ such that @0 <= n <= 'maxBound'@.</span>
<a name="line-385"></a><span class='hs-definition'>optionType_word</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Word</span>
<a name="line-386"></a><span class='hs-definition'>optionType_word</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"uint"</span>
<a name="line-387"></a>
<a name="line-388"></a><a name="instance%20SimpleOptionType%20Word8"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Word8</span> <span class='hs-keyword'>where</span>
<a name="line-389"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_word8</span>
<a name="line-390"></a>
<a name="line-391"></a><a name="optionType_word8"></a><span class='hs-comment'>-- | Store an option as a @'Word8'@. The option value must be a positive</span>
<a name="line-392"></a><span class='hs-comment'>-- integer /n/ such that @0 <= n <= 'maxBound'@.</span>
<a name="line-393"></a><span class='hs-definition'>optionType_word8</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Word8</span>
<a name="line-394"></a><span class='hs-definition'>optionType_word8</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"uint8"</span>
<a name="line-395"></a>
<a name="line-396"></a><a name="instance%20SimpleOptionType%20Word16"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Word16</span> <span class='hs-keyword'>where</span>
<a name="line-397"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_word16</span>
<a name="line-398"></a>
<a name="line-399"></a><a name="optionType_word16"></a><span class='hs-comment'>-- | Store an option as a @'Word16'@. The option value must be a positive</span>
<a name="line-400"></a><span class='hs-comment'>-- integer /n/ such that @0 <= n <= 'maxBound'@.</span>
<a name="line-401"></a><span class='hs-definition'>optionType_word16</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Word16</span>
<a name="line-402"></a><span class='hs-definition'>optionType_word16</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"uint16"</span>
<a name="line-403"></a>
<a name="line-404"></a><a name="instance%20SimpleOptionType%20Word32"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Word32</span> <span class='hs-keyword'>where</span>
<a name="line-405"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_word32</span>
<a name="line-406"></a>
<a name="line-407"></a><a name="optionType_word32"></a><span class='hs-comment'>-- | Store an option as a @'Word32'@. The option value must be a positive</span>
<a name="line-408"></a><span class='hs-comment'>-- integer /n/ such that @0 <= n <= 'maxBound'@.</span>
<a name="line-409"></a><span class='hs-definition'>optionType_word32</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Word32</span>
<a name="line-410"></a><span class='hs-definition'>optionType_word32</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"uint32"</span>
<a name="line-411"></a>
<a name="line-412"></a><a name="instance%20SimpleOptionType%20Word64"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Word64</span> <span class='hs-keyword'>where</span>
<a name="line-413"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_word64</span>
<a name="line-414"></a>
<a name="line-415"></a><a name="optionType_word64"></a><span class='hs-comment'>-- | Store an option as a @'Word64'@. The option value must be a positive</span>
<a name="line-416"></a><span class='hs-comment'>-- integer /n/ such that @0 <= n <= 'maxBound'@.</span>
<a name="line-417"></a><span class='hs-definition'>optionType_word64</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Word64</span>
<a name="line-418"></a><span class='hs-definition'>optionType_word64</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeBoundedInt</span> <span class='hs-str'>"uint64"</span>
<a name="line-419"></a>
<a name="line-420"></a><a name="instance%20SimpleOptionType%20Float"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Float</span> <span class='hs-keyword'>where</span>
<a name="line-421"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_float</span>
<a name="line-422"></a>
<a name="line-423"></a><a name="optionType_float"></a><span class='hs-comment'>-- | Store an option as a @'Float'@. The option value must be a number. Due to</span>
<a name="line-424"></a><span class='hs-comment'>-- the imprecision of floating-point math, the stored value might not exactly</span>
<a name="line-425"></a><span class='hs-comment'>-- match the user's input. If the user's input is out of range for the</span>
<a name="line-426"></a><span class='hs-comment'>-- @'Float'@ type, it will be stored as @Infinity@ or @-Infinity@.</span>
<a name="line-427"></a><span class='hs-definition'>optionType_float</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Float</span>
<a name="line-428"></a><span class='hs-definition'>optionType_float</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-str'>"float32"</span> <span class='hs-num'>0</span> <span class='hs-varid'>parseFloat</span> <span class='hs-varid'>show</span>
<a name="line-429"></a>
<a name="line-430"></a><a name="instance%20SimpleOptionType%20Double"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-conid'>Double</span> <span class='hs-keyword'>where</span>
<a name="line-431"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_double</span>
<a name="line-432"></a>
<a name="line-433"></a><a name="optionType_double"></a><span class='hs-comment'>-- | Store an option as a @'Double'@. The option value must be a number. Due to</span>
<a name="line-434"></a><span class='hs-comment'>-- the imprecision of floating-point math, the stored value might not exactly</span>
<a name="line-435"></a><span class='hs-comment'>-- match the user's input. If the user's input is out of range for the</span>
<a name="line-436"></a><span class='hs-comment'>-- @'Double'@ type, it will be stored as @Infinity@ or @-Infinity@.</span>
<a name="line-437"></a><span class='hs-definition'>optionType_double</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-conid'>Double</span>
<a name="line-438"></a><span class='hs-definition'>optionType_double</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-str'>"float64"</span> <span class='hs-num'>0</span> <span class='hs-varid'>parseFloat</span> <span class='hs-varid'>show</span>
<a name="line-439"></a>
<a name="line-440"></a><a name="parseFloat"></a><span class='hs-definition'>parseFloat</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Read</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-varid'>a</span>
<a name="line-441"></a><span class='hs-definition'>parseFloat</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>reads</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>of</span>
<a name="line-442"></a> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-layout'>,</span> <span class='hs-str'>""</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-varid'>x</span>
<a name="line-443"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-varid'>show</span> <span class='hs-varid'>s</span> <span class='hs-varop'>++</span> <span class='hs-str'>" is not a number."</span><span class='hs-layout'>)</span>
<a name="line-444"></a>
<a name="line-445"></a><a name="instance%20SimpleOptionType%20(Maybe%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-446"></a> <span class='hs-varid'>simpleOptionType</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType_maybe</span> <span class='hs-varid'>simpleOptionType</span>
<a name="line-447"></a>
<a name="line-448"></a><a name="optionType_maybe"></a><span class='hs-comment'>-- | Store an option as a @'Maybe'@ of another type. The value will be</span>
<a name="line-449"></a><span class='hs-comment'>-- @Nothing@ if the option is set to an empty string.</span>
<a name="line-450"></a><span class='hs-definition'>optionType_maybe</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-451"></a><span class='hs-definition'>optionType_maybe</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>maybeT</span> <span class='hs-layout'>{</span> <span class='hs-varid'>optionTypeUnary</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unary</span> <span class='hs-layout'>}</span> <span class='hs-keyword'>where</span>
<a name="line-452"></a> <span class='hs-varid'>maybeT</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-varid'>name</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-varid'>parseMaybe</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>showMaybe</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-453"></a> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"maybe<"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>optionTypeName</span> <span class='hs-varid'>t</span> <span class='hs-varop'>++</span> <span class='hs-str'>">"</span>
<a name="line-454"></a> <span class='hs-varid'>unary</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>optionTypeUnary</span> <span class='hs-varid'>t</span> <span class='hs-keyword'>of</span>
<a name="line-455"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Nothing</span>
<a name="line-456"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>val</span><span class='hs-layout'>)</span>
<a name="line-457"></a>
<a name="line-458"></a><a name="parseMaybe"></a><span class='hs-definition'>parseMaybe</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-varid'>val</span><span class='hs-layout'>)</span>
<a name="line-459"></a><span class='hs-definition'>parseMaybe</span> <span class='hs-varid'>t</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>of</span>
<a name="line-460"></a> <span class='hs-str'>""</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-conid'>Nothing</span>
<a name="line-461"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>optionTypeParse</span> <span class='hs-varid'>t</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>of</span>
<a name="line-462"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-463"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-464"></a>
<a name="line-465"></a><a name="showMaybe"></a><span class='hs-definition'>showMaybe</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-466"></a><span class='hs-definition'>showMaybe</span> <span class='hs-keyword'>_</span> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>""</span>
<a name="line-467"></a><span class='hs-definition'>showMaybe</span> <span class='hs-varid'>t</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeShow</span> <span class='hs-varid'>t</span> <span class='hs-varid'>x</span>
<a name="line-468"></a>
<a name="line-469"></a><a name="optionType_set"></a><span class='hs-comment'>-- | Store an option as a @'Set.Set'@, using another option type for the</span>
<a name="line-470"></a><span class='hs-comment'>-- elements. The separator should be a character that will not occur within</span>
<a name="line-471"></a><span class='hs-comment'>-- the values, such as a comma or semicolon.</span>
<a name="line-472"></a><span class='hs-comment'>--</span>
<a name="line-473"></a><span class='hs-comment'>-- Duplicate elements in the input are permitted.</span>
<a name="line-474"></a><span class='hs-definition'>optionType_set</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>a</span>
<a name="line-475"></a> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Char</span> <span class='hs-comment'>-- ^ Element separator</span>
<a name="line-476"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>a</span> <span class='hs-comment'>-- ^ Element type</span>
<a name="line-477"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-layout'>(</span><span class='hs-conid'>Set</span><span class='hs-varop'>.</span><span class='hs-conid'>Set</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-478"></a><span class='hs-definition'>optionType_set</span> <span class='hs-varid'>sep</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-varid'>name</span> <span class='hs-conid'>Set</span><span class='hs-varop'>.</span><span class='hs-varid'>empty</span> <span class='hs-varid'>parseSet</span> <span class='hs-varid'>showSet</span> <span class='hs-keyword'>where</span>
<a name="line-479"></a> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"set<"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>optionTypeName</span> <span class='hs-varid'>t</span> <span class='hs-varop'>++</span> <span class='hs-str'>">"</span>
<a name="line-480"></a> <span class='hs-varid'>parseSet</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parseList</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionTypeParse</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>split</span> <span class='hs-varid'>sep</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span> <span class='hs-keyword'>of</span>
<a name="line-481"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-482"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-conid'>Set</span><span class='hs-varop'>.</span><span class='hs-varid'>fromList</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-483"></a> <span class='hs-varid'>showSet</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>sep</span><span class='hs-keyglyph'>]</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionTypeShow</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>Set</span><span class='hs-varop'>.</span><span class='hs-varid'>toList</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-484"></a>
<a name="line-485"></a><a name="optionType_map"></a><span class='hs-comment'>-- | Store an option as a 'Map.Map', using other option types for the keys and</span>
<a name="line-486"></a><span class='hs-comment'>-- values.</span>
<a name="line-487"></a><span class='hs-comment'>--</span>
<a name="line-488"></a><span class='hs-comment'>-- The item separator is used to separate key/value pairs from eachother. It</span>
<a name="line-489"></a><span class='hs-comment'>-- should be a character that will not occur within either the keys or values.</span>
<a name="line-490"></a><span class='hs-comment'>--</span>
<a name="line-491"></a><span class='hs-comment'>-- The value separator is used to separate the key from the value. It should</span>
<a name="line-492"></a><span class='hs-comment'>-- be a character that will not occur within the keys. It may occur within the</span>
<a name="line-493"></a><span class='hs-comment'>-- values.</span>
<a name="line-494"></a><span class='hs-comment'>--</span>
<a name="line-495"></a><span class='hs-comment'>-- Duplicate keys in the input are permitted. The final value for each key is</span>
<a name="line-496"></a><span class='hs-comment'>-- stored.</span>
<a name="line-497"></a><span class='hs-definition'>optionType_map</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>k</span>
<a name="line-498"></a> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Char</span> <span class='hs-comment'>-- ^ Item separator</span>
<a name="line-499"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Char</span> <span class='hs-comment'>-- ^ Key/Value separator</span>
<a name="line-500"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>k</span> <span class='hs-comment'>-- ^ Key type</span>
<a name="line-501"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>v</span> <span class='hs-comment'>-- ^ Value type</span>
<a name="line-502"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-layout'>(</span><span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-conid'>Map</span> <span class='hs-varid'>k</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span>
<a name="line-503"></a><span class='hs-definition'>optionType_map</span> <span class='hs-varid'>itemSep</span> <span class='hs-varid'>keySep</span> <span class='hs-varid'>kt</span> <span class='hs-varid'>vt</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-varid'>name</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>empty</span> <span class='hs-varid'>parser</span> <span class='hs-varid'>showMap</span> <span class='hs-keyword'>where</span>
<a name="line-504"></a> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"map<"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>optionTypeName</span> <span class='hs-varid'>kt</span> <span class='hs-varop'>++</span> <span class='hs-str'>","</span> <span class='hs-varop'>++</span> <span class='hs-varid'>optionTypeName</span> <span class='hs-varid'>vt</span> <span class='hs-varop'>++</span> <span class='hs-str'>">"</span>
<a name="line-505"></a> <span class='hs-varid'>parser</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parseMap</span> <span class='hs-varid'>keySep</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionTypeParse</span> <span class='hs-varid'>kt</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionTypeParse</span> <span class='hs-varid'>vt</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>split</span> <span class='hs-varid'>itemSep</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span>
<a name="line-506"></a> <span class='hs-varid'>showMap</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>itemSep</span><span class='hs-keyglyph'>]</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-varid'>showItem</span> <span class='hs-layout'>(</span><span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>toList</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-507"></a> <span class='hs-varid'>showItem</span> <span class='hs-layout'>(</span><span class='hs-varid'>k</span><span class='hs-layout'>,</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeShow</span> <span class='hs-varid'>kt</span> <span class='hs-varid'>k</span> <span class='hs-varop'>++</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>keySep</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>++</span> <span class='hs-varid'>optionTypeShow</span> <span class='hs-varid'>vt</span> <span class='hs-varid'>v</span>
<a name="line-508"></a>
<a name="line-509"></a><a name="parseList"></a><span class='hs-definition'>parseList</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span>
<a name="line-510"></a><span class='hs-definition'>parseList</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>loop</span> <span class='hs-keyword'>where</span>
<a name="line-511"></a> <span class='hs-varid'>loop</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Right</span> <span class='hs-conid'>[]</span>
<a name="line-512"></a> <span class='hs-varid'>loop</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-conop'>:</span><span class='hs-varid'>xs</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>p</span> <span class='hs-varid'>x</span> <span class='hs-keyword'>of</span>
<a name="line-513"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-514"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>loop</span> <span class='hs-varid'>xs</span> <span class='hs-keyword'>of</span>
<a name="line-515"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-516"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>vs</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>v</span><span class='hs-conop'>:</span><span class='hs-varid'>vs</span><span class='hs-layout'>)</span>
<a name="line-517"></a>
<a name="line-518"></a><a name="parseMap"></a><span class='hs-definition'>parseMap</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Ord</span> <span class='hs-varid'>k</span> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Char</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-varid'>k</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-layout'>(</span><span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-conid'>Map</span> <span class='hs-varid'>k</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span>
<a name="line-519"></a><span class='hs-definition'>parseMap</span> <span class='hs-varid'>keySep</span> <span class='hs-varid'>pKey</span> <span class='hs-varid'>pVal</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parsed</span> <span class='hs-keyword'>where</span>
<a name="line-520"></a> <span class='hs-varid'>parsed</span> <span class='hs-varid'>strs</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parseList</span> <span class='hs-varid'>pItem</span> <span class='hs-varid'>strs</span> <span class='hs-keyword'>of</span>
<a name="line-521"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-522"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>fromList</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-523"></a> <span class='hs-varid'>pItem</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>break</span> <span class='hs-layout'>(</span><span class='hs-varop'>==</span> <span class='hs-varid'>keySep</span><span class='hs-layout'>)</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>of</span>
<a name="line-524"></a> <span class='hs-layout'>(</span><span class='hs-varid'>sKey</span><span class='hs-layout'>,</span> <span class='hs-varid'>valAndSep</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>valAndSep</span> <span class='hs-keyword'>of</span>
<a name="line-525"></a> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-str'>"Map item "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>s</span> <span class='hs-varop'>++</span> <span class='hs-str'>" has no value."</span><span class='hs-layout'>)</span>
<a name="line-526"></a> <span class='hs-keyword'>_</span> <span class='hs-conop'>:</span> <span class='hs-varid'>sVal</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>pKey</span> <span class='hs-varid'>sKey</span> <span class='hs-keyword'>of</span>
<a name="line-527"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-528"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>key</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>pVal</span> <span class='hs-varid'>sVal</span> <span class='hs-keyword'>of</span>
<a name="line-529"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-530"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key</span><span class='hs-layout'>,</span> <span class='hs-varid'>val</span><span class='hs-layout'>)</span>
<a name="line-531"></a>
<a name="line-532"></a><a name="split"></a><span class='hs-definition'>split</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Char</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span>
<a name="line-533"></a><span class='hs-definition'>split</span> <span class='hs-keyword'>_</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-534"></a><span class='hs-definition'>split</span> <span class='hs-varid'>sep</span> <span class='hs-varid'>s0</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>loop</span> <span class='hs-varid'>s0</span> <span class='hs-keyword'>where</span>
<a name="line-535"></a> <span class='hs-varid'>loop</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span>
<a name="line-536"></a> <span class='hs-layout'>(</span><span class='hs-varid'>chunk</span><span class='hs-layout'>,</span> <span class='hs-varid'>rest</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>break</span> <span class='hs-layout'>(</span><span class='hs-varop'>==</span> <span class='hs-varid'>sep</span><span class='hs-layout'>)</span> <span class='hs-varid'>s</span>
<a name="line-537"></a> <span class='hs-varid'>cont</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>chunk</span> <span class='hs-conop'>:</span> <span class='hs-varid'>loop</span> <span class='hs-layout'>(</span><span class='hs-varid'>tail</span> <span class='hs-varid'>rest</span><span class='hs-layout'>)</span>
<a name="line-538"></a> <span class='hs-keyword'>in</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-varid'>rest</span> <span class='hs-keyword'>then</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>chunk</span><span class='hs-keyglyph'>]</span> <span class='hs-keyword'>else</span> <span class='hs-varid'>cont</span>
<a name="line-539"></a>
<a name="line-540"></a><a name="optionType_list"></a><span class='hs-comment'>-- | Store an option as a list, using another option type for the elements.</span>
<a name="line-541"></a><span class='hs-comment'>-- The separator should be a character that will not occur within the values,</span>
<a name="line-542"></a><span class='hs-comment'>-- such as a comma or semicolon.</span>
<a name="line-543"></a><span class='hs-definition'>optionType_list</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Char</span> <span class='hs-comment'>-- ^ Element separator</span>
<a name="line-544"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>a</span> <span class='hs-comment'>-- ^ Element type</span>
<a name="line-545"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span>
<a name="line-546"></a><span class='hs-definition'>optionType_list</span> <span class='hs-varid'>sep</span> <span class='hs-varid'>t</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-varid'>name</span> <span class='hs-conid'>[]</span> <span class='hs-varid'>parser</span> <span class='hs-varid'>shower</span> <span class='hs-keyword'>where</span>
<a name="line-547"></a> <span class='hs-varid'>name</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"list<"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>optionTypeName</span> <span class='hs-varid'>t</span> <span class='hs-varop'>++</span> <span class='hs-str'>">"</span>
<a name="line-548"></a> <span class='hs-varid'>parser</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parseList</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionTypeParse</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>split</span> <span class='hs-varid'>sep</span> <span class='hs-varid'>s</span><span class='hs-layout'>)</span>
<a name="line-549"></a> <span class='hs-varid'>shower</span> <span class='hs-varid'>xs</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>sep</span><span class='hs-keyglyph'>]</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionTypeShow</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span> <span class='hs-varid'>xs</span><span class='hs-layout'>)</span>
<a name="line-550"></a>
<a name="line-551"></a><a name="optionType_enum"></a><span class='hs-comment'>-- | Store an option as one of a set of possible values. The type must be a</span>
<a name="line-552"></a><span class='hs-comment'>-- bounded enumeration, and the type's 'Show' instance will be used to</span>
<a name="line-553"></a><span class='hs-comment'>-- implement the parser.</span>
<a name="line-554"></a><span class='hs-comment'>--</span>
<a name="line-555"></a><span class='hs-comment'>-- This is a simplistic implementation, useful for quick scripts. Users with</span>
<a name="line-556"></a><span class='hs-comment'>-- more complex requirements for enum parsing are encouraged to define their</span>
<a name="line-557"></a><span class='hs-comment'>-- own option types using 'optionType'.</span>
<a name="line-558"></a><span class='hs-comment'>--</span>
<a name="line-559"></a><span class='hs-comment'>-- @</span>
<a name="line-560"></a><span class='hs-comment'>--data Action = Hello | Goodbye</span>
<a name="line-561"></a><span class='hs-comment'>-- deriving (Bounded, Enum, Show)</span>
<a name="line-562"></a><span class='hs-comment'>--</span>
<a name="line-563"></a><span class='hs-comment'>--data MainOptions = MainOptions { optAction :: Action }</span>
<a name="line-564"></a><span class='hs-comment'>--</span>
<a name="line-565"></a><span class='hs-comment'>--instance 'Options' MainOptions where</span>
<a name="line-566"></a><span class='hs-comment'>-- 'defineOptions' = pure MainOptions</span>
<a name="line-567"></a><span class='hs-comment'>-- \<*\> 'defineOption' (optionType_enum \"action\") (\\o -> o</span>
<a name="line-568"></a><span class='hs-comment'>-- { 'optionLongFlags' = [\"action\"]</span>
<a name="line-569"></a><span class='hs-comment'>-- , 'optionDefault' = Hello</span>
<a name="line-570"></a><span class='hs-comment'>-- })</span>
<a name="line-571"></a><span class='hs-comment'>--</span>
<a name="line-572"></a><span class='hs-comment'>--main = 'runCommand' $ \\opts args -> do</span>
<a name="line-573"></a><span class='hs-comment'>-- putStrLn (\"Running action \" ++ show (optAction opts))</span>
<a name="line-574"></a><span class='hs-comment'>-- @</span>
<a name="line-575"></a><span class='hs-comment'>--</span>
<a name="line-576"></a><span class='hs-comment'>-- >$ ./app</span>
<a name="line-577"></a><span class='hs-comment'>-- >Running action Hello</span>
<a name="line-578"></a><span class='hs-comment'>-- >$ ./app --action=Goodbye</span>
<a name="line-579"></a><span class='hs-comment'>-- >Running action Goodbye</span>
<a name="line-580"></a><span class='hs-definition'>optionType_enum</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Bounded</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Enum</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-581"></a> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>String</span> <span class='hs-comment'>-- ^ Option type name</span>
<a name="line-582"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>a</span>
<a name="line-583"></a><span class='hs-definition'>optionType_enum</span> <span class='hs-varid'>tName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionType</span> <span class='hs-varid'>tName</span> <span class='hs-varid'>minBound</span> <span class='hs-varid'>parseEnum</span> <span class='hs-varid'>show</span> <span class='hs-keyword'>where</span>
<a name="line-584"></a> <span class='hs-varid'>values</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>fromList</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>show</span> <span class='hs-varid'>x</span><span class='hs-layout'>,</span> <span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>enumFrom</span> <span class='hs-varid'>minBound</span><span class='hs-keyglyph'>]</span>
<a name="line-585"></a> <span class='hs-varid'>setString</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>"{"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>", "</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>keys</span> <span class='hs-varid'>values</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>++</span> <span class='hs-str'>"}"</span>
<a name="line-586"></a> <span class='hs-varid'>parseEnum</span> <span class='hs-varid'>s</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>lookup</span> <span class='hs-varid'>s</span> <span class='hs-varid'>values</span> <span class='hs-keyword'>of</span>
<a name="line-587"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-varid'>show</span> <span class='hs-varid'>s</span> <span class='hs-varop'>++</span> <span class='hs-str'>" is not in "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>setString</span> <span class='hs-varop'>++</span> <span class='hs-str'>"."</span><span class='hs-layout'>)</span>
<a name="line-588"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-varid'>x</span>
<a name="line-589"></a>
<a name="line-590"></a><a name="simpleOption"></a><span class='hs-comment'>-- | Defines a new option in the current options type.</span>
<a name="line-591"></a><span class='hs-comment'>--</span>
<a name="line-592"></a><span class='hs-definition'>simpleOption</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>SimpleOptionType</span> <span class='hs-varid'>a</span>
<a name="line-593"></a> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>String</span> <span class='hs-comment'>-- long flag</span>
<a name="line-594"></a> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>a</span> <span class='hs-comment'>-- default value</span>
<a name="line-595"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span> <span class='hs-comment'>-- description</span>
<a name="line-596"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>a</span>
<a name="line-597"></a><span class='hs-definition'>simpleOption</span> <span class='hs-varid'>flag</span> <span class='hs-varid'>def</span> <span class='hs-varid'>desc</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>defineOption</span> <span class='hs-varid'>simpleOptionType</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>o</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>o</span>
<a name="line-598"></a> <span class='hs-layout'>{</span> <span class='hs-varid'>optionLongFlags</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>flag</span><span class='hs-keyglyph'>]</span>
<a name="line-599"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionDefault</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>def</span>
<a name="line-600"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionDescription</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>desc</span>
<a name="line-601"></a> <span class='hs-layout'>}</span><span class='hs-layout'>)</span>
<a name="line-602"></a>
<a name="line-603"></a><a name="defineOption"></a><span class='hs-comment'>-- | Defines a new option in the current options type.</span>
<a name="line-604"></a><span class='hs-comment'>--</span>
<a name="line-605"></a><span class='hs-comment'>-- All options must have one or more /flags/. Options may also have a</span>
<a name="line-606"></a><span class='hs-comment'>-- default value, a description, and a group.</span>
<a name="line-607"></a><span class='hs-comment'>--</span>
<a name="line-608"></a><span class='hs-comment'>-- The /flags/ are how the user specifies an option on the command line. Flags</span>
<a name="line-609"></a><span class='hs-comment'>-- may be /short/ or /long/. See 'optionShortFlags' and 'optionLongFlags' for</span>
<a name="line-610"></a><span class='hs-comment'>-- details.</span>
<a name="line-611"></a><span class='hs-comment'>--</span>
<a name="line-612"></a><span class='hs-comment'>-- @</span>
<a name="line-613"></a><span class='hs-comment'>--'defineOption' 'optionType_word16' (\\o -> o</span>
<a name="line-614"></a><span class='hs-comment'>-- { 'optionLongFlags' = [\"port\"]</span>
<a name="line-615"></a><span class='hs-comment'>-- , 'optionDefault' = 80</span>
<a name="line-616"></a><span class='hs-comment'>-- })</span>
<a name="line-617"></a><span class='hs-comment'>-- @</span>
<a name="line-618"></a><span class='hs-definition'>defineOption</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionType</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Option</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Option</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>DefineOptions</span> <span class='hs-varid'>a</span>
<a name="line-619"></a><span class='hs-definition'>defineOption</span> <span class='hs-varid'>t</span> <span class='hs-varid'>fn</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>DefineOptions</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionDefault</span> <span class='hs-varid'>opt</span><span class='hs-layout'>)</span> <span class='hs-varid'>getInfo</span> <span class='hs-varid'>parser</span> <span class='hs-keyword'>where</span>
<a name="line-620"></a> <span class='hs-varid'>opt</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fn</span> <span class='hs-layout'>(</span><span class='hs-conid'>Option</span>
<a name="line-621"></a> <span class='hs-layout'>{</span> <span class='hs-varid'>optionShortFlags</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-622"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionLongFlags</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>[]</span>
<a name="line-623"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionDefault</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeDefault</span> <span class='hs-varid'>t</span>
<a name="line-624"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionDescription</span> <span class='hs-keyglyph'>=</span> <span class='hs-str'>""</span>
<a name="line-625"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionGroup</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Nothing</span>
<a name="line-626"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionLocation</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Nothing</span>
<a name="line-627"></a> <span class='hs-layout'>}</span><span class='hs-layout'>)</span>
<a name="line-628"></a>
<a name="line-629"></a> <span class='hs-varid'>getInfo</span> <span class='hs-varid'>key</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>key</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>,</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>OptionInfo</span>
<a name="line-630"></a> <span class='hs-layout'>{</span> <span class='hs-varid'>optionInfoKey</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>OptionKeyGenerated</span> <span class='hs-varid'>key</span>
<a name="line-631"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionInfoShortFlags</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionShortFlags</span> <span class='hs-varid'>opt</span>
<a name="line-632"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionInfoLongFlags</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionLongFlags</span> <span class='hs-varid'>opt</span>
<a name="line-633"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionInfoDefault</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeShow</span> <span class='hs-varid'>t</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionDefault</span> <span class='hs-varid'>opt</span><span class='hs-layout'>)</span>
<a name="line-634"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionInfoDescription</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionDescription</span> <span class='hs-varid'>opt</span>
<a name="line-635"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionInfoGroup</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionGroup</span> <span class='hs-varid'>opt</span>
<a name="line-636"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionInfoLocation</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionLocation</span> <span class='hs-varid'>opt</span>
<a name="line-637"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionInfoTypeName</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>optionTypeName</span> <span class='hs-varid'>t</span>
<a name="line-638"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionInfoUnary</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>isJust</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionTypeUnary</span> <span class='hs-varid'>t</span><span class='hs-layout'>)</span>
<a name="line-639"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionInfoUnaryOnly</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-640"></a> <span class='hs-layout'>}</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span>
<a name="line-641"></a>
<a name="line-642"></a> <span class='hs-comment'>-- parseToken :: Token -> Either String val</span>
<a name="line-643"></a> <span class='hs-varid'>parseToken</span> <span class='hs-varid'>tok</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>tok</span> <span class='hs-keyword'>of</span>
<a name="line-644"></a> <span class='hs-conid'>TokenUnary</span> <span class='hs-varid'>flagName</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>optionTypeUnary</span> <span class='hs-varid'>t</span> <span class='hs-keyword'>of</span>
<a name="line-645"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-str'>"The flag "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>flagName</span> <span class='hs-varop'>++</span> <span class='hs-str'>" requires an argument."</span><span class='hs-layout'>)</span>
<a name="line-646"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-varid'>val</span>
<a name="line-647"></a> <span class='hs-conid'>Token</span> <span class='hs-varid'>flagName</span> <span class='hs-varid'>rawValue</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>optionTypeParse</span> <span class='hs-varid'>t</span> <span class='hs-varid'>rawValue</span> <span class='hs-keyword'>of</span>
<a name="line-648"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-str'>"Value for flag "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>flagName</span> <span class='hs-varop'>++</span> <span class='hs-str'>" is invalid: "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>err</span><span class='hs-layout'>)</span>
<a name="line-649"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-varid'>val</span>
<a name="line-650"></a>
<a name="line-651"></a> <span class='hs-varid'>parser</span> <span class='hs-varid'>key</span> <span class='hs-varid'>tokens</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>lookup</span> <span class='hs-layout'>(</span><span class='hs-conid'>OptionKeyGenerated</span> <span class='hs-varid'>key</span><span class='hs-layout'>)</span> <span class='hs-varid'>tokens</span> <span class='hs-keyword'>of</span>
<a name="line-652"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>,</span> <span class='hs-varid'>optionDefault</span> <span class='hs-varid'>opt</span><span class='hs-layout'>)</span>
<a name="line-653"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>toks</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>toks</span> <span class='hs-keyword'>of</span>
<a name="line-654"></a> <span class='hs-comment'>-- shouldn't happen, but lets do something graceful anyway.</span>
<a name="line-655"></a> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>,</span> <span class='hs-varid'>optionDefault</span> <span class='hs-varid'>opt</span><span class='hs-layout'>)</span>
<a name="line-656"></a> <span class='hs-keyglyph'>[</span><span class='hs-varid'>tok</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parseToken</span> <span class='hs-varid'>tok</span> <span class='hs-keyword'>of</span>
<a name="line-657"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-658"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>val</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>,</span> <span class='hs-varid'>val</span><span class='hs-layout'>)</span>
<a name="line-659"></a> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>optionTypeMerge</span> <span class='hs-varid'>t</span> <span class='hs-keyword'>of</span>
<a name="line-660"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-str'>"Multiple values for flag: "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>showMultipleFlagValues</span> <span class='hs-varid'>toks</span><span class='hs-layout'>)</span>
<a name="line-661"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>appendFn</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>mapEither</span> <span class='hs-varid'>parseToken</span> <span class='hs-varid'>toks</span> <span class='hs-keyword'>of</span>
<a name="line-662"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-663"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>vals</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>key</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>,</span> <span class='hs-varid'>appendFn</span> <span class='hs-varid'>vals</span><span class='hs-layout'>)</span>
<a name="line-664"></a>
<a name="line-665"></a><a name="showMultipleFlagValues"></a><span class='hs-definition'>showMultipleFlagValues</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Token</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-666"></a><span class='hs-definition'>showMultipleFlagValues</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>intercalate</span> <span class='hs-str'>" "</span> <span class='hs-varop'>.</span> <span class='hs-varid'>map</span> <span class='hs-varid'>showToken</span> <span class='hs-keyword'>where</span>
<a name="line-667"></a> <span class='hs-varid'>showToken</span> <span class='hs-layout'>(</span><span class='hs-conid'>TokenUnary</span> <span class='hs-varid'>flagName</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>flagName</span>
<a name="line-668"></a> <span class='hs-varid'>showToken</span> <span class='hs-layout'>(</span><span class='hs-conid'>Token</span> <span class='hs-varid'>flagName</span> <span class='hs-varid'>rawValue</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-varid'>flagName</span> <span class='hs-varop'>++</span> <span class='hs-str'>"="</span> <span class='hs-varop'>++</span> <span class='hs-varid'>rawValue</span><span class='hs-layout'>)</span>
<a name="line-669"></a>
<a name="line-670"></a><a name="Option"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>Option</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Option</span>
<a name="line-671"></a> <span class='hs-layout'>{</span>
<a name="line-672"></a> <span class='hs-comment'>-- | Short flags are a single character. When entered by a user,</span>
<a name="line-673"></a> <span class='hs-comment'>-- they are preceded by a dash and possibly other short flags.</span>
<a name="line-674"></a> <span class='hs-comment'>--</span>
<a name="line-675"></a> <span class='hs-comment'>-- Short flags must be a letter or a number.</span>
<a name="line-676"></a> <span class='hs-comment'>--</span>
<a name="line-677"></a> <span class='hs-comment'>-- Example: An option with @optionShortFlags = [\'p\']@ may be set using:</span>
<a name="line-678"></a> <span class='hs-comment'>--</span>
<a name="line-679"></a> <span class='hs-comment'>-- >$ ./app -p 443</span>
<a name="line-680"></a> <span class='hs-comment'>-- >$ ./app -p443</span>
<a name="line-681"></a> <span class='hs-varid'>optionShortFlags</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Char</span><span class='hs-keyglyph'>]</span>
<a name="line-682"></a>
<a name="line-683"></a> <span class='hs-comment'>-- | Long flags are multiple characters. When entered by a user, they</span>
<a name="line-684"></a> <span class='hs-comment'>-- are preceded by two dashes.</span>
<a name="line-685"></a> <span class='hs-comment'>--</span>
<a name="line-686"></a> <span class='hs-comment'>-- Long flags may contain letters, numbers, @\'-\'@, and @\'_\'@.</span>
<a name="line-687"></a> <span class='hs-comment'>--</span>
<a name="line-688"></a> <span class='hs-comment'>-- Example: An option with @optionLongFlags = [\"port\"]@ may be set using:</span>
<a name="line-689"></a> <span class='hs-comment'>--</span>
<a name="line-690"></a> <span class='hs-comment'>-- >$ ./app --port 443</span>
<a name="line-691"></a> <span class='hs-comment'>-- >$ ./app --port=443</span>
<a name="line-692"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionLongFlags</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span>
<a name="line-693"></a>
<a name="line-694"></a> <span class='hs-comment'>-- | Options may have a default value. This will be parsed as if the</span>
<a name="line-695"></a> <span class='hs-comment'>-- user had entered it on the command line.</span>
<a name="line-696"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionDefault</span> <span class='hs-keyglyph'>::</span> <span class='hs-varid'>a</span>
<a name="line-697"></a>
<a name="line-698"></a> <span class='hs-comment'>-- | An option's description is used with the default implementation</span>
<a name="line-699"></a> <span class='hs-comment'>-- of @--help@. It should be a short string describing what the option</span>
<a name="line-700"></a> <span class='hs-comment'>-- does.</span>
<a name="line-701"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionDescription</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span>
<a name="line-702"></a>
<a name="line-703"></a> <span class='hs-comment'>-- | Which group the option is in. See the \"Option groups\" section</span>
<a name="line-704"></a> <span class='hs-comment'>-- for details.</span>
<a name="line-705"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionGroup</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Maybe</span> <span class='hs-conid'>Group</span>
<a name="line-706"></a>
<a name="line-707"></a> <span class='hs-comment'>-- | TODO docs</span>
<a name="line-708"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>optionLocation</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Maybe</span> <span class='hs-conid'>Location</span>
<a name="line-709"></a> <span class='hs-layout'>}</span>
<a name="line-710"></a>
<a name="line-711"></a><a name="validateOptionDefs"></a><span class='hs-definition'>validateOptionDefs</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>OptionInfo</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-conid'>String</span><span class='hs-layout'>,</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>OptionInfo</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-conid'>OptionDefinitions</span>
<a name="line-712"></a><span class='hs-definition'>validateOptionDefs</span> <span class='hs-varid'>cmdInfos</span> <span class='hs-varid'>subInfos</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>runIdentity</span> <span class='hs-varop'>$</span> <span class='hs-varid'>runErrorT</span> <span class='hs-varop'>$</span> <span class='hs-keyword'>do</span>
<a name="line-713"></a> <span class='hs-comment'>-- All subcommands have unique names.</span>
<a name="line-714"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>subcmdNames</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-varid'>fst</span> <span class='hs-varid'>subInfos</span>
<a name="line-715"></a> <span class='hs-keyword'>if</span> <span class='hs-conid'>Set</span><span class='hs-varop'>.</span><span class='hs-varid'>size</span> <span class='hs-layout'>(</span><span class='hs-conid'>Set</span><span class='hs-varop'>.</span><span class='hs-varid'>fromList</span> <span class='hs-varid'>subcmdNames</span><span class='hs-layout'>)</span> <span class='hs-varop'>/=</span> <span class='hs-varid'>length</span> <span class='hs-varid'>subcmdNames</span>
<a name="line-716"></a> <span class='hs-comment'>-- TODO: the error should mention which subcommand names are duplicated</span>
<a name="line-717"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>throwError</span> <span class='hs-str'>"Multiple subcommands exist with the same name."</span>
<a name="line-718"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>return</span> <span class='hs-conid'>()</span>
<a name="line-719"></a>
<a name="line-720"></a> <span class='hs-comment'>-- Each option defines at least one short or long flag.</span>
<a name="line-721"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>allOptInfos</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>cmdInfos</span> <span class='hs-varop'>++</span> <span class='hs-varid'>concat</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>infos</span> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span> <span class='hs-varid'>infos</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>subInfos</span><span class='hs-keyglyph'>]</span>
<a name="line-722"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>mapEither</span> <span class='hs-varid'>optValidFlags</span> <span class='hs-varid'>allOptInfos</span> <span class='hs-keyword'>of</span>
<a name="line-723"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>throwError</span> <span class='hs-varid'>err</span>
<a name="line-724"></a> <span class='hs-conid'>Right</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-conid'>()</span>
<a name="line-725"></a>
<a name="line-726"></a> <span class='hs-comment'>-- There are no duplicate short or long flags, unless:</span>
<a name="line-727"></a> <span class='hs-comment'>-- The flags are defined in separate subcommands.</span>
<a name="line-728"></a> <span class='hs-comment'>-- The flags have identical OptionInfos (aside from keys)</span>
<a name="line-729"></a> <span class='hs-varid'>cmdDeDupedFlags</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>checkNoDuplicateFlags</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>empty</span> <span class='hs-varid'>cmdInfos</span>
<a name="line-730"></a> <span class='hs-varid'>forM_</span> <span class='hs-varid'>subInfos</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>subInfo</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>checkNoDuplicateFlags</span> <span class='hs-varid'>cmdDeDupedFlags</span> <span class='hs-layout'>(</span><span class='hs-varid'>snd</span> <span class='hs-varid'>subInfo</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-731"></a>
<a name="line-732"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>addHelpFlags</span> <span class='hs-layout'>(</span><span class='hs-conid'>OptionDefinitions</span> <span class='hs-varid'>cmdInfos</span> <span class='hs-varid'>subInfos</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-733"></a>
<a name="line-734"></a><a name="optValidFlags"></a><span class='hs-definition'>optValidFlags</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionInfo</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-conid'>()</span>
<a name="line-735"></a><span class='hs-definition'>optValidFlags</span> <span class='hs-varid'>info</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-varid'>null</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionInfoShortFlags</span> <span class='hs-varid'>info</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span> <span class='hs-varid'>null</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionInfoLongFlags</span> <span class='hs-varid'>info</span><span class='hs-layout'>)</span>
<a name="line-736"></a> <span class='hs-keyword'>then</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>optionInfoLocation</span> <span class='hs-varid'>info</span> <span class='hs-keyword'>of</span>
<a name="line-737"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-str'>"Option with description "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionInfoDescription</span> <span class='hs-varid'>info</span><span class='hs-layout'>)</span> <span class='hs-varop'>++</span> <span class='hs-str'>" has no flags."</span><span class='hs-layout'>)</span>
<a name="line-738"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>loc</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-str'>"Option with description "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionInfoDescription</span> <span class='hs-varid'>info</span><span class='hs-layout'>)</span> <span class='hs-varop'>++</span> <span class='hs-str'>" at "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>locationFilename</span> <span class='hs-varid'>loc</span> <span class='hs-varop'>++</span> <span class='hs-str'>":"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-layout'>(</span><span class='hs-varid'>locationLine</span> <span class='hs-varid'>loc</span><span class='hs-layout'>)</span> <span class='hs-varop'>++</span> <span class='hs-str'>" has no flags."</span><span class='hs-layout'>)</span>
<a name="line-739"></a> <span class='hs-comment'>-- TODO: All short or long flags have a reasonable name.</span>
<a name="line-740"></a> <span class='hs-keyword'>else</span> <span class='hs-conid'>Right</span> <span class='hs-conid'>()</span>
<a name="line-741"></a>
<a name="line-742"></a><a name="DeDupFlag"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>DeDupFlag</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>DeDupShort</span> <span class='hs-conid'>Char</span> <span class='hs-keyglyph'>|</span> <span class='hs-conid'>DeDupLong</span> <span class='hs-conid'>String</span>
<a name="line-743"></a> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span> <span class='hs-conid'>Ord</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span><span class='hs-layout'>)</span>
<a name="line-744"></a>
<a name="line-745"></a><a name="checkNoDuplicateFlags"></a><span class='hs-definition'>checkNoDuplicateFlags</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-conid'>Map</span> <span class='hs-conid'>DeDupFlag</span> <span class='hs-conid'>OptionInfo</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>OptionInfo</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ErrorT</span> <span class='hs-conid'>String</span> <span class='hs-conid'>Identity</span> <span class='hs-layout'>(</span><span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-conid'>Map</span> <span class='hs-conid'>DeDupFlag</span> <span class='hs-conid'>OptionInfo</span><span class='hs-layout'>)</span>
<a name="line-746"></a><span class='hs-definition'>checkNoDuplicateFlags</span> <span class='hs-varid'>checked</span> <span class='hs-conid'>[]</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-varid'>checked</span>
<a name="line-747"></a><span class='hs-definition'>checkNoDuplicateFlags</span> <span class='hs-varid'>checked</span> <span class='hs-layout'>(</span><span class='hs-varid'>info</span><span class='hs-conop'>:</span><span class='hs-varid'>infos</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-748"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>mappedShort</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-conid'>DeDupShort</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionInfoShortFlags</span> <span class='hs-varid'>info</span><span class='hs-layout'>)</span>
<a name="line-749"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>mappedLong</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-conid'>DeDupLong</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionInfoLongFlags</span> <span class='hs-varid'>info</span><span class='hs-layout'>)</span>
<a name="line-750"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>mappedFlags</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>mappedShort</span> <span class='hs-varop'>++</span> <span class='hs-varid'>mappedLong</span>
<a name="line-751"></a> <span class='hs-varid'>forM_</span> <span class='hs-varid'>mappedFlags</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>mapKey</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>lookup</span> <span class='hs-varid'>mapKey</span> <span class='hs-varid'>checked</span> <span class='hs-keyword'>of</span>
<a name="line-752"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>return</span> <span class='hs-conid'>()</span>
<a name="line-753"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>prevInfo</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>if</span> <span class='hs-varid'>eqIgnoringKey</span> <span class='hs-varid'>info</span> <span class='hs-varid'>prevInfo</span>
<a name="line-754"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>return</span> <span class='hs-conid'>()</span>
<a name="line-755"></a> <span class='hs-keyword'>else</span> <span class='hs-keyword'>let</span>
<a name="line-756"></a> <span class='hs-varid'>flagName</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>mapKey</span> <span class='hs-keyword'>of</span>
<a name="line-757"></a> <span class='hs-conid'>DeDupShort</span> <span class='hs-varid'>flag</span> <span class='hs-keyglyph'>-></span> <span class='hs-chr'>'-'</span> <span class='hs-conop'>:</span> <span class='hs-varid'>flag</span> <span class='hs-conop'>:</span> <span class='hs-conid'>[]</span>
<a name="line-758"></a> <span class='hs-conid'>DeDupLong</span> <span class='hs-varid'>long</span> <span class='hs-keyglyph'>-></span> <span class='hs-str'>"--"</span> <span class='hs-varop'>++</span> <span class='hs-varid'>long</span>
<a name="line-759"></a> <span class='hs-keyword'>in</span> <span class='hs-varid'>throwError</span> <span class='hs-layout'>(</span><span class='hs-str'>"Duplicate option flag "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>flagName</span> <span class='hs-varop'>++</span> <span class='hs-str'>"."</span><span class='hs-layout'>)</span>
<a name="line-760"></a>
<a name="line-761"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>infoMap</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>fromList</span> <span class='hs-keyglyph'>[</span><span class='hs-layout'>(</span><span class='hs-varid'>f</span><span class='hs-layout'>,</span> <span class='hs-varid'>info</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>f</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>mappedFlags</span><span class='hs-keyglyph'>]</span>
<a name="line-762"></a> <span class='hs-varid'>checkNoDuplicateFlags</span> <span class='hs-layout'>(</span><span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>union</span> <span class='hs-varid'>checked</span> <span class='hs-varid'>infoMap</span><span class='hs-layout'>)</span> <span class='hs-varid'>infos</span>
<a name="line-763"></a>
<a name="line-764"></a><a name="eqIgnoringKey"></a><span class='hs-definition'>eqIgnoringKey</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>OptionInfo</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>OptionInfo</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-765"></a><span class='hs-definition'>eqIgnoringKey</span> <span class='hs-varid'>x</span> <span class='hs-varid'>y</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>normKey</span> <span class='hs-varid'>x</span> <span class='hs-varop'>==</span> <span class='hs-varid'>normKey</span> <span class='hs-varid'>y</span> <span class='hs-keyword'>where</span>
<a name="line-766"></a> <span class='hs-varid'>normKey</span> <span class='hs-varid'>info</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>info</span> <span class='hs-layout'>{</span> <span class='hs-varid'>optionInfoKey</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>OptionKeyIgnored</span> <span class='hs-layout'>}</span>
<a name="line-767"></a>
<a name="line-768"></a><a name="Parsed"></a><span class='hs-comment'>-- | See @'parseOptions'@ and @'parseSubcommand'@.</span>
<a name="line-769"></a><a name="Parsed"></a><span class='hs-keyword'>class</span> <span class='hs-conid'>Parsed</span> <span class='hs-varid'>a</span> <span class='hs-keyword'>where</span>
<a name="line-770"></a> <span class='hs-varid'>parsedError_</span> <span class='hs-keyglyph'>::</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-conid'>String</span>
<a name="line-771"></a> <span class='hs-varid'>parsedHelp_</span> <span class='hs-keyglyph'>::</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-772"></a>
<a name="line-773"></a><a name="ParsedOptions"></a><span class='hs-comment'>-- | See @'parseOptions'@.</span>
<a name="line-774"></a><a name="ParsedOptions"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-varid'>opts</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-varid'>opts</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-conid'>String</span><span class='hs-layout'>)</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span>
<a name="line-775"></a>
<a name="line-776"></a><a name="ParsedSubcommand"></a><span class='hs-comment'>-- | See @'parseSubcommand'@.</span>
<a name="line-777"></a><a name="ParsedSubcommand"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>ParsedSubcommand</span> <span class='hs-varid'>action</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>ParsedSubcommand</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-varid'>action</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>Maybe</span> <span class='hs-conid'>String</span><span class='hs-layout'>)</span> <span class='hs-conid'>String</span>
<a name="line-778"></a>
<a name="line-779"></a><a name="instance%20Parsed%20(ParsedOptions%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Parsed</span> <span class='hs-layout'>(</span><span class='hs-conid'>ParsedOptions</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-780"></a> <span class='hs-varid'>parsedError_</span> <span class='hs-layout'>(</span><span class='hs-conid'>ParsedOptions</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>x</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-781"></a> <span class='hs-varid'>parsedHelp_</span> <span class='hs-layout'>(</span><span class='hs-conid'>ParsedOptions</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>x</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-782"></a>
<a name="line-783"></a><a name="instance%20Parsed%20(ParsedSubcommand%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Parsed</span> <span class='hs-layout'>(</span><span class='hs-conid'>ParsedSubcommand</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-784"></a> <span class='hs-varid'>parsedError_</span> <span class='hs-layout'>(</span><span class='hs-conid'>ParsedSubcommand</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>x</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-785"></a> <span class='hs-varid'>parsedHelp_</span> <span class='hs-layout'>(</span><span class='hs-conid'>ParsedSubcommand</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-786"></a>
<a name="line-787"></a><a name="parsedOptions"></a><span class='hs-comment'>-- | Get the options value that was parsed from argv, or @Nothing@ if the</span>
<a name="line-788"></a><span class='hs-comment'>-- arguments could not be converted into options.</span>
<a name="line-789"></a><span class='hs-comment'>--</span>
<a name="line-790"></a><span class='hs-comment'>-- Note: This function return @Nothing@ if the user provided a help flag. To</span>
<a name="line-791"></a><span class='hs-comment'>-- check whether an error occured during parsing, check the value of</span>
<a name="line-792"></a><span class='hs-comment'>-- @'parsedError'@.</span>
<a name="line-793"></a><span class='hs-definition'>parsedOptions</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-varid'>opts</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-varid'>opts</span>
<a name="line-794"></a><span class='hs-definition'>parsedOptions</span> <span class='hs-layout'>(</span><span class='hs-conid'>ParsedOptions</span> <span class='hs-varid'>x</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-795"></a>
<a name="line-796"></a><a name="parsedArguments"></a><span class='hs-comment'>-- | Get command-line arguments remaining after parsing options. The arguments</span>
<a name="line-797"></a><span class='hs-comment'>-- are unchanged from the original argument list, and have not been decoded</span>
<a name="line-798"></a><span class='hs-comment'>-- or otherwise transformed.</span>
<a name="line-799"></a><span class='hs-definition'>parsedArguments</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-varid'>opts</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span>
<a name="line-800"></a><span class='hs-definition'>parsedArguments</span> <span class='hs-layout'>(</span><span class='hs-conid'>ParsedOptions</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-801"></a>
<a name="line-802"></a><a name="parsedSubcommand"></a><span class='hs-comment'>-- | Get the subcommand action that was parsed from argv, or @Nothing@ if the</span>
<a name="line-803"></a><span class='hs-comment'>-- arguments could not be converted into a valid action.</span>
<a name="line-804"></a><span class='hs-comment'>--</span>
<a name="line-805"></a><span class='hs-comment'>-- Note: This function return @Nothing@ if the user provided a help flag. To</span>
<a name="line-806"></a><span class='hs-comment'>-- check whether an error occured during parsing, check the value of</span>
<a name="line-807"></a><span class='hs-comment'>-- @'parsedError'@.</span>
<a name="line-808"></a><span class='hs-definition'>parsedSubcommand</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>ParsedSubcommand</span> <span class='hs-varid'>action</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-varid'>action</span>
<a name="line-809"></a><span class='hs-definition'>parsedSubcommand</span> <span class='hs-layout'>(</span><span class='hs-conid'>ParsedSubcommand</span> <span class='hs-varid'>x</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span>
<a name="line-810"></a>
<a name="line-811"></a><a name="parsedError"></a><span class='hs-comment'>-- | Get the error that prevented options from being parsed from argv,</span>
<a name="line-812"></a><span class='hs-comment'>-- or @Nothing@ if no error was detected.</span>
<a name="line-813"></a><span class='hs-definition'>parsedError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Parsed</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-conid'>String</span>
<a name="line-814"></a><span class='hs-definition'>parsedError</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parsedError_</span>
<a name="line-815"></a>
<a name="line-816"></a><a name="parsedHelp"></a><span class='hs-comment'>-- | Get a help message to show the user. If the arguments included</span>
<a name="line-817"></a><span class='hs-comment'>-- a help flag, this will be a message appropriate to that flag.</span>
<a name="line-818"></a><span class='hs-comment'>-- Otherwise, it is a summary (equivalent to @--help@).</span>
<a name="line-819"></a><span class='hs-comment'>--</span>
<a name="line-820"></a><span class='hs-comment'>-- This is always a non-empty string, regardless of whether the parse</span>
<a name="line-821"></a><span class='hs-comment'>-- succeeded or failed. If you need to perform additional validation</span>
<a name="line-822"></a><span class='hs-comment'>-- on the options value, this message can be displayed if validation</span>
<a name="line-823"></a><span class='hs-comment'>-- fails.</span>
<a name="line-824"></a><span class='hs-definition'>parsedHelp</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Parsed</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=></span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>String</span>
<a name="line-825"></a><span class='hs-definition'>parsedHelp</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parsedHelp_</span>
<a name="line-826"></a>
<a name="line-827"></a><a name="parseOptions"></a><span class='hs-comment'>-- | Attempt to convert a list of command-line arguments into an options</span>
<a name="line-828"></a><span class='hs-comment'>-- value. This can be used by application developers who want finer control</span>
<a name="line-829"></a><span class='hs-comment'>-- over error handling, or who want to perform additional validation on the</span>
<a name="line-830"></a><span class='hs-comment'>-- options value.</span>
<a name="line-831"></a><span class='hs-comment'>--</span>
<a name="line-832"></a><span class='hs-comment'>-- The argument list must be in the same encoding as the result of</span>
<a name="line-833"></a><span class='hs-comment'>-- 'System.Environment.getArgs'.</span>
<a name="line-834"></a><span class='hs-comment'>--</span>
<a name="line-835"></a><span class='hs-comment'>-- Use @'parsedOptions'@, @'parsedArguments'@, @'parsedError'@, and</span>
<a name="line-836"></a><span class='hs-comment'>-- @'parsedHelp'@ to inspect the result of @'parseOptions'@.</span>
<a name="line-837"></a><span class='hs-comment'>--</span>
<a name="line-838"></a><span class='hs-comment'>-- Example:</span>
<a name="line-839"></a><span class='hs-comment'>--</span>
<a name="line-840"></a><span class='hs-comment'>-- @</span>
<a name="line-841"></a><span class='hs-comment'>--getOptionsOrDie :: Options a => IO a</span>
<a name="line-842"></a><span class='hs-comment'>--getOptionsOrDie = do</span>
<a name="line-843"></a><span class='hs-comment'>-- argv <- System.Environment.getArgs</span>
<a name="line-844"></a><span class='hs-comment'>-- let parsed = 'parseOptions' argv</span>
<a name="line-845"></a><span class='hs-comment'>-- case 'parsedOptions' parsed of</span>
<a name="line-846"></a><span class='hs-comment'>-- Just opts -> return opts</span>
<a name="line-847"></a><span class='hs-comment'>-- Nothing -> case 'parsedError' parsed of</span>
<a name="line-848"></a><span class='hs-comment'>-- Just err -> do</span>
<a name="line-849"></a><span class='hs-comment'>-- hPutStrLn stderr ('parsedHelp' parsed)</span>
<a name="line-850"></a><span class='hs-comment'>-- hPutStrLn stderr err</span>
<a name="line-851"></a><span class='hs-comment'>-- exitFailure</span>
<a name="line-852"></a><span class='hs-comment'>-- Nothing -> do</span>
<a name="line-853"></a><span class='hs-comment'>-- hPutStr stdout ('parsedHelp' parsed)</span>
<a name="line-854"></a><span class='hs-comment'>-- exitSuccess</span>
<a name="line-855"></a><span class='hs-comment'>-- @</span>
<a name="line-856"></a><span class='hs-definition'>parseOptions</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Options</span> <span class='hs-varid'>opts</span> <span class='hs-keyglyph'>=></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-varid'>opts</span>
<a name="line-857"></a><span class='hs-definition'>parseOptions</span> <span class='hs-varid'>argv</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parsed</span> <span class='hs-keyword'>where</span>
<a name="line-858"></a> <span class='hs-layout'>(</span><span class='hs-conid'>DefineOptions</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>getInfos</span> <span class='hs-varid'>parser</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>defineOptions</span>
<a name="line-859"></a> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span> <span class='hs-varid'>optionInfos</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>getInfos</span> <span class='hs-num'>0</span>
<a name="line-860"></a> <span class='hs-varid'>parseTokens</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parser</span> <span class='hs-num'>0</span>
<a name="line-861"></a>
<a name="line-862"></a> <span class='hs-varid'>parsed</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>validateOptionDefs</span> <span class='hs-varid'>optionInfos</span> <span class='hs-conid'>[]</span> <span class='hs-keyword'>of</span>
<a name="line-863"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>err</span><span class='hs-layout'>)</span> <span class='hs-str'>""</span> <span class='hs-conid'>[]</span>
<a name="line-864"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>optionDefs</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>tokenize</span> <span class='hs-layout'>(</span><span class='hs-varid'>addHelpFlags</span> <span class='hs-varid'>optionDefs</span><span class='hs-layout'>)</span> <span class='hs-varid'>argv</span> <span class='hs-keyword'>of</span>
<a name="line-865"></a> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>err</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>helpFor</span> <span class='hs-conid'>HelpSummary</span> <span class='hs-varid'>optionDefs</span> <span class='hs-conid'>Nothing</span><span class='hs-layout'>)</span> <span class='hs-conid'>[]</span>
<a name="line-866"></a> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span> <span class='hs-conid'>Right</span> <span class='hs-varid'>tokens</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>checkHelpFlag</span> <span class='hs-varid'>tokens</span> <span class='hs-keyword'>of</span>
<a name="line-867"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>helpFlag</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-conid'>Nothing</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-varid'>helpFor</span> <span class='hs-varid'>helpFlag</span> <span class='hs-varid'>optionDefs</span> <span class='hs-conid'>Nothing</span><span class='hs-layout'>)</span> <span class='hs-conid'>[]</span>
<a name="line-868"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parseTokens</span> <span class='hs-layout'>(</span><span class='hs-varid'>tokensMap</span> <span class='hs-varid'>tokens</span><span class='hs-layout'>)</span> <span class='hs-keyword'>of</span>
<a name="line-869"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>err</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>helpFor</span> <span class='hs-conid'>HelpSummary</span> <span class='hs-varid'>optionDefs</span> <span class='hs-conid'>Nothing</span><span class='hs-layout'>)</span> <span class='hs-conid'>[]</span>
<a name="line-870"></a> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span> <span class='hs-varid'>opts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedOptions</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>opts</span><span class='hs-layout'>)</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-varid'>helpFor</span> <span class='hs-conid'>HelpSummary</span> <span class='hs-varid'>optionDefs</span> <span class='hs-conid'>Nothing</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>tokensArgv</span> <span class='hs-varid'>tokens</span><span class='hs-layout'>)</span>
<a name="line-871"></a>
<a name="line-872"></a><a name="runCommand"></a><span class='hs-comment'>-- | Retrieve 'System.Environment.getArgs', and attempt to parse it into a</span>
<a name="line-873"></a><span class='hs-comment'>-- valid value of an 'Options' type plus a list of left-over arguments. The</span>
<a name="line-874"></a><span class='hs-comment'>-- options and arguments are then passed to the provided computation.</span>
<a name="line-875"></a><span class='hs-comment'>--</span>
<a name="line-876"></a><span class='hs-comment'>-- If parsing fails, this computation will print an error and call</span>
<a name="line-877"></a><span class='hs-comment'>-- 'exitFailure'.</span>
<a name="line-878"></a><span class='hs-comment'>--</span>
<a name="line-879"></a><span class='hs-comment'>-- If parsing succeeds, and the user has passed a @--help@ flag, and the</span>
<a name="line-880"></a><span class='hs-comment'>-- developer is using the default help flag definitions, then this computation</span>
<a name="line-881"></a><span class='hs-comment'>-- will print documentation and call 'exitSuccess'.</span>
<a name="line-882"></a><span class='hs-comment'>--</span>
<a name="line-883"></a><span class='hs-comment'>-- See 'runSubcommand' for details on subcommand support.</span>
<a name="line-884"></a><span class='hs-definition'>runCommand</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span><span class='hs-layout'>,</span> <span class='hs-conid'>Options</span> <span class='hs-varid'>opts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-layout'>(</span><span class='hs-varid'>opts</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-885"></a><span class='hs-definition'>runCommand</span> <span class='hs-varid'>io</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-886"></a> <span class='hs-varid'>argv</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>liftIO</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Environment</span><span class='hs-varop'>.</span><span class='hs-varid'>getArgs</span>
<a name="line-887"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>parsed</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parseOptions</span> <span class='hs-varid'>argv</span>
<a name="line-888"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>parsedOptions</span> <span class='hs-varid'>parsed</span> <span class='hs-keyword'>of</span>
<a name="line-889"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>opts</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>io</span> <span class='hs-varid'>opts</span> <span class='hs-layout'>(</span><span class='hs-varid'>parsedArguments</span> <span class='hs-varid'>parsed</span><span class='hs-layout'>)</span>
<a name="line-890"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>liftIO</span> <span class='hs-varop'>$</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parsedError</span> <span class='hs-varid'>parsed</span> <span class='hs-keyword'>of</span>
<a name="line-891"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-892"></a> <span class='hs-varid'>hPutStrLn</span> <span class='hs-varid'>stderr</span> <span class='hs-layout'>(</span><span class='hs-varid'>parsedHelp</span> <span class='hs-varid'>parsed</span><span class='hs-layout'>)</span>
<a name="line-893"></a> <span class='hs-varid'>hPutStrLn</span> <span class='hs-varid'>stderr</span> <span class='hs-varid'>err</span>
<a name="line-894"></a> <span class='hs-varid'>exitFailure</span>
<a name="line-895"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-896"></a> <span class='hs-varid'>hPutStr</span> <span class='hs-varid'>stdout</span> <span class='hs-layout'>(</span><span class='hs-varid'>parsedHelp</span> <span class='hs-varid'>parsed</span><span class='hs-layout'>)</span>
<a name="line-897"></a> <span class='hs-varid'>exitSuccess</span>
<a name="line-898"></a>
<a name="line-899"></a><a name="Subcommand"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>Subcommand</span> <span class='hs-varid'>cmdOpts</span> <span class='hs-varid'>action</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Subcommand</span> <span class='hs-conid'>String</span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>[</span><span class='hs-conid'>OptionInfo</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-layout'>(</span><span class='hs-varid'>cmdOpts</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tokens</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Either</span> <span class='hs-conid'>String</span> <span class='hs-varid'>action</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-conid'>Integer</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-900"></a>
<a name="line-901"></a><a name="subcommand"></a><span class='hs-definition'>subcommand</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Options</span> <span class='hs-varid'>cmdOpts</span><span class='hs-layout'>,</span> <span class='hs-conid'>Options</span> <span class='hs-varid'>subcmdOpts</span><span class='hs-layout'>)</span>
<a name="line-902"></a> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>String</span> <span class='hs-comment'>-- ^ The subcommand name.</span>
<a name="line-903"></a> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>cmdOpts</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>subcmdOpts</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>action</span><span class='hs-layout'>)</span> <span class='hs-comment'>-- ^ The action to run.</span>
<a name="line-904"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Subcommand</span> <span class='hs-varid'>cmdOpts</span> <span class='hs-varid'>action</span>
<a name="line-905"></a><span class='hs-definition'>subcommand</span> <span class='hs-varid'>name</span> <span class='hs-varid'>fn</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Subcommand</span> <span class='hs-varid'>name</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>initialKey</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>let</span>
<a name="line-906"></a> <span class='hs-layout'>(</span><span class='hs-conid'>DefineOptions</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>getInfos</span> <span class='hs-varid'>parser</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>defineOptions</span>
<a name="line-907"></a> <span class='hs-layout'>(</span><span class='hs-varid'>nextKey</span><span class='hs-layout'>,</span> <span class='hs-varid'>optionInfos</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>getInfos</span> <span class='hs-varid'>initialKey</span>
<a name="line-908"></a> <span class='hs-varid'>parseTokens</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parser</span> <span class='hs-varid'>initialKey</span>
<a name="line-909"></a>
<a name="line-910"></a> <span class='hs-varid'>runAction</span> <span class='hs-varid'>cmdOpts</span> <span class='hs-varid'>tokens</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parseTokens</span> <span class='hs-layout'>(</span><span class='hs-varid'>tokensMap</span> <span class='hs-varid'>tokens</span><span class='hs-layout'>)</span> <span class='hs-keyword'>of</span>
<a name="line-911"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-912"></a> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span> <span class='hs-varid'>subOpts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-varid'>fn</span> <span class='hs-varid'>cmdOpts</span> <span class='hs-varid'>subOpts</span> <span class='hs-layout'>(</span><span class='hs-varid'>tokensArgv</span> <span class='hs-varid'>tokens</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-913"></a> <span class='hs-keyword'>in</span> <span class='hs-layout'>(</span><span class='hs-varid'>optionInfos</span><span class='hs-layout'>,</span> <span class='hs-varid'>runAction</span><span class='hs-layout'>,</span> <span class='hs-varid'>nextKey</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-914"></a>
<a name="line-915"></a><a name="parseSubcommand"></a><span class='hs-comment'>-- | Attempt to convert a list of command-line arguments into a subcommand</span>
<a name="line-916"></a><span class='hs-comment'>-- action. This can be used by application developers who want finer control</span>
<a name="line-917"></a><span class='hs-comment'>-- over error handling, or who want subcommands that run in an unusual monad.</span>
<a name="line-918"></a><span class='hs-comment'>--</span>
<a name="line-919"></a><span class='hs-comment'>-- The argument list must be in the same encoding as the result of</span>
<a name="line-920"></a><span class='hs-comment'>-- 'System.Environment.getArgs'.</span>
<a name="line-921"></a><span class='hs-comment'>--</span>
<a name="line-922"></a><span class='hs-comment'>-- Use @'parsedSubcommand'@, @'parsedError'@, and @'parsedHelp'@ to inspect the</span>
<a name="line-923"></a><span class='hs-comment'>-- result of @'parseSubcommand'@.</span>
<a name="line-924"></a><span class='hs-comment'>--</span>
<a name="line-925"></a><span class='hs-comment'>-- Example:</span>
<a name="line-926"></a><span class='hs-comment'>--</span>
<a name="line-927"></a><span class='hs-comment'>-- @</span>
<a name="line-928"></a><span class='hs-comment'>--runSubcommand :: Options cmdOpts => [Subcommand cmdOpts (IO a)] -> IO a</span>
<a name="line-929"></a><span class='hs-comment'>--runSubcommand subcommands = do</span>
<a name="line-930"></a><span class='hs-comment'>-- argv <- System.Environment.getArgs</span>
<a name="line-931"></a><span class='hs-comment'>-- let parsed = 'parseSubcommand' subcommands argv</span>
<a name="line-932"></a><span class='hs-comment'>-- case 'parsedSubcommand' parsed of</span>
<a name="line-933"></a><span class='hs-comment'>-- Just cmd -> cmd</span>
<a name="line-934"></a><span class='hs-comment'>-- Nothing -> case 'parsedError' parsed of</span>
<a name="line-935"></a><span class='hs-comment'>-- Just err -> do</span>
<a name="line-936"></a><span class='hs-comment'>-- hPutStrLn stderr ('parsedHelp' parsed)</span>
<a name="line-937"></a><span class='hs-comment'>-- hPutStrLn stderr err</span>
<a name="line-938"></a><span class='hs-comment'>-- exitFailure</span>
<a name="line-939"></a><span class='hs-comment'>-- Nothing -> do</span>
<a name="line-940"></a><span class='hs-comment'>-- hPutStr stdout ('parsedHelp' parsed)</span>
<a name="line-941"></a><span class='hs-comment'>-- exitSuccess</span>
<a name="line-942"></a><span class='hs-comment'>-- @</span>
<a name="line-943"></a><span class='hs-comment'>--</span>
<a name="line-944"></a><span class='hs-definition'>parseSubcommand</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Options</span> <span class='hs-varid'>cmdOpts</span> <span class='hs-keyglyph'>=></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Subcommand</span> <span class='hs-varid'>cmdOpts</span> <span class='hs-varid'>action</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>String</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedSubcommand</span> <span class='hs-varid'>action</span>
<a name="line-945"></a><span class='hs-definition'>parseSubcommand</span> <span class='hs-varid'>subcommands</span> <span class='hs-varid'>argv</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parsed</span> <span class='hs-keyword'>where</span>
<a name="line-946"></a> <span class='hs-layout'>(</span><span class='hs-conid'>DefineOptions</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>getInfos</span> <span class='hs-varid'>parser</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>defineOptions</span>
<a name="line-947"></a> <span class='hs-layout'>(</span><span class='hs-varid'>cmdNextKey</span><span class='hs-layout'>,</span> <span class='hs-varid'>cmdInfos</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>getInfos</span> <span class='hs-num'>0</span>
<a name="line-948"></a> <span class='hs-varid'>cmdParseTokens</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parser</span> <span class='hs-num'>0</span>
<a name="line-949"></a>
<a name="line-950"></a> <span class='hs-varid'>subcmdInfos</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-951"></a> <span class='hs-conid'>Subcommand</span> <span class='hs-varid'>name</span> <span class='hs-varid'>fn</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>subcommands</span>
<a name="line-952"></a> <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-varid'>infos</span><span class='hs-layout'>,</span> <span class='hs-keyword'>_</span><span class='hs-layout'>,</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fn</span> <span class='hs-varid'>cmdNextKey</span>
<a name="line-953"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>name</span><span class='hs-layout'>,</span> <span class='hs-varid'>infos</span><span class='hs-layout'>)</span>
<a name="line-954"></a>
<a name="line-955"></a> <span class='hs-varid'>subcmdRunners</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>fromList</span> <span class='hs-varop'>$</span> <span class='hs-keyword'>do</span>
<a name="line-956"></a> <span class='hs-conid'>Subcommand</span> <span class='hs-varid'>name</span> <span class='hs-varid'>fn</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>subcommands</span>
<a name="line-957"></a> <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span> <span class='hs-varid'>runner</span><span class='hs-layout'>,</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fn</span> <span class='hs-varid'>cmdNextKey</span>
<a name="line-958"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>name</span><span class='hs-layout'>,</span> <span class='hs-varid'>runner</span><span class='hs-layout'>)</span>
<a name="line-959"></a>
<a name="line-960"></a> <span class='hs-varid'>parsed</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>validateOptionDefs</span> <span class='hs-varid'>cmdInfos</span> <span class='hs-varid'>subcmdInfos</span> <span class='hs-keyword'>of</span>
<a name="line-961"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedSubcommand</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>err</span><span class='hs-layout'>)</span> <span class='hs-str'>""</span>
<a name="line-962"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>optionDefs</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>tokenize</span> <span class='hs-layout'>(</span><span class='hs-varid'>addHelpFlags</span> <span class='hs-varid'>optionDefs</span><span class='hs-layout'>)</span> <span class='hs-varid'>argv</span> <span class='hs-keyword'>of</span>
<a name="line-963"></a> <span class='hs-layout'>(</span><span class='hs-varid'>subcmd</span><span class='hs-layout'>,</span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedSubcommand</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>err</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>helpFor</span> <span class='hs-conid'>HelpSummary</span> <span class='hs-varid'>optionDefs</span> <span class='hs-varid'>subcmd</span><span class='hs-layout'>)</span>
<a name="line-964"></a> <span class='hs-layout'>(</span><span class='hs-varid'>subcmd</span><span class='hs-layout'>,</span> <span class='hs-conid'>Right</span> <span class='hs-varid'>tokens</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>checkHelpFlag</span> <span class='hs-varid'>tokens</span> <span class='hs-keyword'>of</span>
<a name="line-965"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>helpFlag</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedSubcommand</span> <span class='hs-conid'>Nothing</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-varid'>helpFor</span> <span class='hs-varid'>helpFlag</span> <span class='hs-varid'>optionDefs</span> <span class='hs-varid'>subcmd</span><span class='hs-layout'>)</span>
<a name="line-966"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>findAction</span> <span class='hs-varid'>tokens</span> <span class='hs-varid'>subcmd</span> <span class='hs-keyword'>of</span>
<a name="line-967"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedSubcommand</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>err</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>helpFor</span> <span class='hs-conid'>HelpSummary</span> <span class='hs-varid'>optionDefs</span> <span class='hs-varid'>subcmd</span><span class='hs-layout'>)</span>
<a name="line-968"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>action</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>ParsedSubcommand</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>action</span><span class='hs-layout'>)</span> <span class='hs-conid'>Nothing</span> <span class='hs-layout'>(</span><span class='hs-varid'>helpFor</span> <span class='hs-conid'>HelpSummary</span> <span class='hs-varid'>optionDefs</span> <span class='hs-varid'>subcmd</span><span class='hs-layout'>)</span>
<a name="line-969"></a>
<a name="line-970"></a> <span class='hs-varid'>findAction</span> <span class='hs-keyword'>_</span> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Left</span> <span class='hs-str'>"No subcommand specified"</span>
<a name="line-971"></a> <span class='hs-varid'>findAction</span> <span class='hs-varid'>tokens</span> <span class='hs-layout'>(</span><span class='hs-conid'>Just</span> <span class='hs-varid'>subcmdName</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>cmdParseTokens</span> <span class='hs-layout'>(</span><span class='hs-varid'>tokensMap</span> <span class='hs-varid'>tokens</span><span class='hs-layout'>)</span> <span class='hs-keyword'>of</span>
<a name="line-972"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-973"></a> <span class='hs-conid'>Right</span> <span class='hs-layout'>(</span><span class='hs-keyword'>_</span><span class='hs-layout'>,</span> <span class='hs-varid'>cmdOpts</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-conid'>Map</span><span class='hs-varop'>.</span><span class='hs-varid'>lookup</span> <span class='hs-varid'>subcmdName</span> <span class='hs-varid'>subcmdRunners</span> <span class='hs-keyword'>of</span>
<a name="line-974"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-layout'>(</span><span class='hs-str'>"Unknown subcommand "</span> <span class='hs-varop'>++</span> <span class='hs-varid'>show</span> <span class='hs-varid'>subcmdName</span> <span class='hs-varop'>++</span> <span class='hs-str'>"."</span><span class='hs-layout'>)</span>
<a name="line-975"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>getRunner</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>case</span> <span class='hs-varid'>getRunner</span> <span class='hs-varid'>cmdOpts</span> <span class='hs-varid'>tokens</span> <span class='hs-keyword'>of</span>
<a name="line-976"></a> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Left</span> <span class='hs-varid'>err</span>
<a name="line-977"></a> <span class='hs-conid'>Right</span> <span class='hs-varid'>action</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Right</span> <span class='hs-varid'>action</span>
<a name="line-978"></a>
<a name="line-979"></a><a name="runSubcommand"></a><span class='hs-comment'>-- | Used to run applications that are split into subcommands.</span>
<a name="line-980"></a><span class='hs-comment'>--</span>
<a name="line-981"></a><span class='hs-comment'>-- Use 'subcommand' to define available commands and their actions, then pass</span>
<a name="line-982"></a><span class='hs-comment'>-- them to this computation to select one and run it. If the user specifies</span>
<a name="line-983"></a><span class='hs-comment'>-- an invalid subcommand, this computation will print an error and call</span>
<a name="line-984"></a><span class='hs-comment'>-- 'exitFailure'. In handling of invalid flags or @--help@, 'runSubcommand'</span>
<a name="line-985"></a><span class='hs-comment'>-- acts like 'runCommand'.</span>
<a name="line-986"></a><span class='hs-comment'>--</span>
<a name="line-987"></a><span class='hs-comment'>-- @</span>
<a name="line-988"></a><span class='hs-comment'>--import Control.Applicative</span>
<a name="line-989"></a><span class='hs-comment'>--import Control.Monad (unless)</span>
<a name="line-990"></a><span class='hs-comment'>--import Options</span>
<a name="line-991"></a><span class='hs-comment'>--</span>
<a name="line-992"></a><span class='hs-comment'>--data MainOptions = MainOptions { optQuiet :: Bool }</span>
<a name="line-993"></a><span class='hs-comment'>--instance 'Options' MainOptions where</span>
<a name="line-994"></a><span class='hs-comment'>-- 'defineOptions' = pure MainOptions</span>
<a name="line-995"></a><span class='hs-comment'>-- \<*\> 'simpleOption' \"quiet\" False \"Whether to be quiet.\"</span>
<a name="line-996"></a><span class='hs-comment'>--</span>
<a name="line-997"></a><span class='hs-comment'>--data HelloOpts = HelloOpts { optHello :: String }</span>
<a name="line-998"></a><span class='hs-comment'>--instance 'Options' HelloOpts where</span>
<a name="line-999"></a><span class='hs-comment'>-- 'defineOptions' = pure HelloOpts</span>
<a name="line-1000"></a><span class='hs-comment'>-- \<*\> 'simpleOption' \"hello\" \"Hello!\" \"How to say hello.\"</span>
<a name="line-1001"></a><span class='hs-comment'>--</span>
<a name="line-1002"></a><span class='hs-comment'>--data ByeOpts = ByeOpts { optName :: String }</span>
<a name="line-1003"></a><span class='hs-comment'>--instance 'Options' ByeOpts where</span>
<a name="line-1004"></a><span class='hs-comment'>-- 'defineOptions' = pure ByeOpts</span>
<a name="line-1005"></a><span class='hs-comment'>-- \<*\> 'simpleOption' \"name\" \"\" \"The user's name.\"</span>
<a name="line-1006"></a><span class='hs-comment'>--</span>
<a name="line-1007"></a><span class='hs-comment'>--hello :: MainOptions -> HelloOpts -> [String] -> IO ()</span>
<a name="line-1008"></a><span class='hs-comment'>--hello mainOpts opts args = unless (optQuiet mainOpts) $ do</span>
<a name="line-1009"></a><span class='hs-comment'>-- putStrLn (optHello opts)</span>
<a name="line-1010"></a><span class='hs-comment'>--</span>
<a name="line-1011"></a><span class='hs-comment'>--bye :: MainOptions -> ByeOpts -> [String] -> IO ()</span>
<a name="line-1012"></a><span class='hs-comment'>--bye mainOpts opts args = unless (optQuiet mainOpts) $ do</span>
<a name="line-1013"></a><span class='hs-comment'>-- putStrLn (\"Good bye \" ++ optName opts)</span>
<a name="line-1014"></a><span class='hs-comment'>--</span>
<a name="line-1015"></a><span class='hs-comment'>--main :: IO ()</span>
<a name="line-1016"></a><span class='hs-comment'>--main = 'runSubcommand'</span>
<a name="line-1017"></a><span class='hs-comment'>-- [ 'subcommand' \"hello\" hello</span>
<a name="line-1018"></a><span class='hs-comment'>-- , 'subcommand' \"bye\" bye</span>
<a name="line-1019"></a><span class='hs-comment'>-- ]</span>
<a name="line-1020"></a><span class='hs-comment'>-- @</span>
<a name="line-1021"></a><span class='hs-comment'>--</span>
<a name="line-1022"></a><span class='hs-comment'>-- >$ ./app hello</span>
<a name="line-1023"></a><span class='hs-comment'>-- >Hello!</span>
<a name="line-1024"></a><span class='hs-comment'>-- >$ ./app hello --hello='Allo!'</span>
<a name="line-1025"></a><span class='hs-comment'>-- >Allo!</span>
<a name="line-1026"></a><span class='hs-comment'>-- >$ ./app bye</span>
<a name="line-1027"></a><span class='hs-comment'>-- >Good bye </span>
<a name="line-1028"></a><span class='hs-comment'>-- >$ ./app bye --name='Alice'</span>
<a name="line-1029"></a><span class='hs-comment'>-- >Good bye Alice</span>
<a name="line-1030"></a><span class='hs-definition'>runSubcommand</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Options</span> <span class='hs-varid'>opts</span><span class='hs-layout'>,</span> <span class='hs-conid'>MonadIO</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Subcommand</span> <span class='hs-varid'>opts</span> <span class='hs-layout'>(</span><span class='hs-varid'>m</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>m</span> <span class='hs-varid'>a</span>
<a name="line-1031"></a><span class='hs-definition'>runSubcommand</span> <span class='hs-varid'>subcommands</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-1032"></a> <span class='hs-varid'>argv</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>liftIO</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Environment</span><span class='hs-varop'>.</span><span class='hs-varid'>getArgs</span>
<a name="line-1033"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>parsed</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>parseSubcommand</span> <span class='hs-varid'>subcommands</span> <span class='hs-varid'>argv</span>
<a name="line-1034"></a> <span class='hs-keyword'>case</span> <span class='hs-varid'>parsedSubcommand</span> <span class='hs-varid'>parsed</span> <span class='hs-keyword'>of</span>
<a name="line-1035"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>cmd</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>cmd</span>
<a name="line-1036"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>liftIO</span> <span class='hs-varop'>$</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>parsedError</span> <span class='hs-varid'>parsed</span> <span class='hs-keyword'>of</span>
<a name="line-1037"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>err</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-1038"></a> <span class='hs-varid'>hPutStrLn</span> <span class='hs-varid'>stderr</span> <span class='hs-layout'>(</span><span class='hs-varid'>parsedHelp</span> <span class='hs-varid'>parsed</span><span class='hs-layout'>)</span>
<a name="line-1039"></a> <span class='hs-varid'>hPutStrLn</span> <span class='hs-varid'>stderr</span> <span class='hs-varid'>err</span>
<a name="line-1040"></a> <span class='hs-varid'>exitFailure</span>
<a name="line-1041"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyword'>do</span>
<a name="line-1042"></a> <span class='hs-varid'>hPutStr</span> <span class='hs-varid'>stdout</span> <span class='hs-layout'>(</span><span class='hs-varid'>parsedHelp</span> <span class='hs-varid'>parsed</span><span class='hs-layout'>)</span>
<a name="line-1043"></a> <span class='hs-varid'>exitSuccess</span>
</pre></body>
</html>
|