Commit eee2451
authored
fix HelpError handling (#36)
* fix: catch HelpError and display help instead of throwing
When a HelpError is thrown (e.g., unknown command, no command specified),
`parse()` and `parseAsync()` now catch it and handle gracefully:
- Error message is printed to stderr
- Help text is displayed to stderr
- `process.exitCode` is set to 1 (no `process.exit()` call)
- Returns result with `helpShown: true` flag
This prevents HelpError from bubbling up to global exception handlers
while still providing useful feedback to the user.
Closes #31
* refactor: remove all process.exit() calls
Replace `process.exit()` with `process.exitCode` throughout `parseCore()`:
- `--help` now sets `process.exitCode = 0` and returns `{ helpShown: true }`
- `--version` now sets `process.exitCode = 0` and returns `{ helpShown: true }`
- `--completion-script` now sets appropriate exit code and returns
- `--get-bargs-completions` now sets exit code and returns
- `showNestedCommandHelp()` now returns a result instead of calling exit
This allows the process to terminate naturally, enabling proper cleanup
handlers and making the code more testable and composable.
* fix: address review comments
- Rename `helpShown` to `earlyExit` for clarity (issue #4)
The flag is now accurately named since it's set for help, version,
and completion output, not just help display.
- Extract shared test helper `withCapturedStderr()` (issues #2, #3)
Reduces code duplication in tests that capture stderr and exitCode.
- Handle HelpError in nested command delegation (issue #1)
`__parseWithParentGlobals()` now catches HelpError so nested builders
can render their own help instead of bubbling up to the parent.
* fix: use process.exit() for early exit scenarios
Restore standard CLI behavior where --help, --version, completion flags,
and error conditions (unknown/missing commands) terminate the process.
Changes:
- `exitProcess()` calls `process.exit()` for help/version/completions
- `handleHelpError()` calls `process.exit(1)` after displaying help
- Remove `earlyExit` flag from return types (no longer needed)
- Update tests to mock `process.exit` instead of checking return values
- Document process termination behavior in README.md
This is what users expect from a CLI - these flags print output and exit.
* refactor: extract shared test helper for mocking process.exit
Address review feedback:
- Extract `MockExitError` and `withMockedExit` to `test/helpers/mock-exit.ts`
- Use `Promise.resolve().then(fn)` to handle any thenable, not just Promise
- Consolidate duplicate JSDoc blocks for `handleHelpError`
- Update README example to use sentinel error and `finally` block1 parent 7b6d197 commit eee2451
File tree
6 files changed
+323
-79
lines changed- src
- test
- helpers
6 files changed
+323
-79
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
707 | 707 | | |
708 | 708 | | |
709 | 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 | + | |
710 | 754 | | |
711 | 755 | | |
712 | | - | |
| 756 | + | |
713 | 757 | | |
714 | 758 | | |
715 | | - | |
716 | | - | |
717 | | - | |
718 | | - | |
719 | | - | |
720 | | - | |
| 759 | + | |
721 | 760 | | |
722 | 761 | | |
723 | 762 | | |
| |||
726 | 765 | | |
727 | 766 | | |
728 | 767 | | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | | - | |
733 | 768 | | |
734 | 769 | | |
735 | 770 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
557 | 557 | | |
558 | 558 | | |
559 | 559 | | |
| 560 | + | |
560 | 561 | | |
561 | 562 | | |
562 | 563 | | |
| |||
565 | 566 | | |
566 | 567 | | |
567 | 568 | | |
568 | | - | |
569 | | - | |
570 | | - | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
571 | 586 | | |
572 | 587 | | |
573 | 588 | | |
| |||
761 | 776 | | |
762 | 777 | | |
763 | 778 | | |
764 | | - | |
765 | | - | |
766 | | - | |
767 | | - | |
768 | | - | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
769 | 792 | | |
770 | | - | |
771 | 793 | | |
772 | 794 | | |
773 | 795 | | |
774 | 796 | | |
775 | 797 | | |
776 | | - | |
777 | | - | |
778 | | - | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
779 | 808 | | |
780 | 809 | | |
781 | 810 | | |
| |||
799 | 828 | | |
800 | 829 | | |
801 | 830 | | |
802 | | - | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
803 | 845 | | |
804 | 846 | | |
805 | 847 | | |
| |||
832 | 874 | | |
833 | 875 | | |
834 | 876 | | |
835 | | - | |
836 | | - | |
| 877 | + | |
837 | 878 | | |
838 | 879 | | |
839 | 880 | | |
840 | 881 | | |
841 | 882 | | |
842 | 883 | | |
843 | 884 | | |
844 | | - | |
845 | | - | |
| 885 | + | |
846 | 886 | | |
847 | 887 | | |
848 | 888 | | |
849 | 889 | | |
850 | | - | |
| 890 | + | |
851 | 891 | | |
852 | 892 | | |
853 | 893 | | |
854 | 894 | | |
855 | | - | |
| 895 | + | |
856 | 896 | | |
857 | 897 | | |
858 | 898 | | |
| |||
863 | 903 | | |
864 | 904 | | |
865 | 905 | | |
866 | | - | |
| 906 | + | |
867 | 907 | | |
868 | 908 | | |
869 | 909 | | |
| |||
876 | 916 | | |
877 | 917 | | |
878 | 918 | | |
879 | | - | |
| 919 | + | |
880 | 920 | | |
881 | 921 | | |
882 | 922 | | |
883 | 923 | | |
884 | | - | |
| 924 | + | |
885 | 925 | | |
886 | 926 | | |
887 | | - | |
| 927 | + | |
888 | 928 | | |
889 | 929 | | |
890 | 930 | | |
| |||
894 | 934 | | |
895 | 935 | | |
896 | 936 | | |
897 | | - | |
| 937 | + | |
898 | 938 | | |
899 | 939 | | |
900 | 940 | | |
| |||
904 | 944 | | |
905 | 945 | | |
906 | 946 | | |
907 | | - | |
| 947 | + | |
908 | 948 | | |
909 | 949 | | |
910 | | - | |
| 950 | + | |
911 | 951 | | |
912 | 952 | | |
913 | 953 | | |
914 | | - | |
915 | 954 | | |
916 | 955 | | |
917 | 956 | | |
| |||
924 | 963 | | |
925 | 964 | | |
926 | 965 | | |
| 966 | + | |
| 967 | + | |
927 | 968 | | |
928 | 969 | | |
929 | 970 | | |
930 | | - | |
931 | 971 | | |
932 | 972 | | |
933 | 973 | | |
934 | | - | |
| 974 | + | |
935 | 975 | | |
936 | 976 | | |
937 | | - | |
| 977 | + | |
938 | 978 | | |
939 | 979 | | |
940 | 980 | | |
941 | | - | |
| 981 | + | |
942 | 982 | | |
943 | 983 | | |
944 | 984 | | |
| |||
948 | 988 | | |
949 | 989 | | |
950 | 990 | | |
951 | | - | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
952 | 994 | | |
953 | 995 | | |
954 | 996 | | |
955 | 997 | | |
956 | 998 | | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
957 | 1003 | | |
958 | | - | |
959 | 1004 | | |
960 | 1005 | | |
961 | 1006 | | |
962 | 1007 | | |
963 | 1008 | | |
964 | 1009 | | |
965 | | - | |
| 1010 | + | |
966 | 1011 | | |
967 | 1012 | | |
968 | 1013 | | |
| |||
973 | 1018 | | |
974 | 1019 | | |
975 | 1020 | | |
976 | | - | |
977 | | - | |
| 1021 | + | |
| 1022 | + | |
978 | 1023 | | |
979 | | - | |
980 | | - | |
| 1024 | + | |
981 | 1025 | | |
982 | 1026 | | |
983 | 1027 | | |
| |||
1004 | 1048 | | |
1005 | 1049 | | |
1006 | 1050 | | |
1007 | | - | |
| 1051 | + | |
1008 | 1052 | | |
1009 | 1053 | | |
1010 | 1054 | | |
| |||
1053 | 1097 | | |
1054 | 1098 | | |
1055 | 1099 | | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
1056 | 1134 | | |
1057 | 1135 | | |
1058 | 1136 | | |
| |||
0 commit comments