| 1 | # ABSTRACT: The one dispatch path shared by bin/karr and the in-process test runner | ||
| 2 | |||
| 3 | package App::karr::Dispatch; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | 20 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 20 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 20 | ▶ | use Exporter qw( import ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 20 | ▶ | use Scalar::Util qw( blessed ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 20 | ▶ | use Module::Runtime qw( use_module ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use App::karr; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use App::karr::SyncGuard; | ||
| 12 | use App::karr::Encoding qw( decode_argv enable_std_utf8 ); | ||
| 13 | use App::karr::Error qw( is_usage_error set_original_argv ); | ||
| 14 | use App::karr::Role::CliArgs; | ||
| 15 | |||
| 16 | our @EXPORT_OK = qw( dispatch ); | ||
| 17 | |||
| 18 | |||
| 19 | # An empty argument never reaches a command, so it is refused here while it is | ||
| 20 | # still visible (ticket #243). | ||
| 21 | # | ||
| 22 | # MooX::Cmd copies argv with `shellwords(join ' ', map { quotemeta } @ARGV)` | ||
| 23 | # (MooX::Cmd::Role 1.000, line 132) before it even looks for the command name. | ||
| 24 | # That round trip is the identity on every non-empty argument -- quotemeta | ||
| 25 | # escapes, shellwords unescapes -- but the empty string contributes nothing to | ||
| 26 | # the joined line and shellwords never returns an empty field, so the transform | ||
| 27 | # is exactly `grep { length }`. No spelling of an empty argument survives it, | ||
| 28 | # which is why this could not be fixed by letting the token through to the | ||
| 29 | # `defined && length` guards of #239: by the time any karr code runs there is | ||
| 30 | # nothing left to hand them. | ||
| 31 | # | ||
| 32 | # What the deletion leaves is a shorter argv, so every argument after the empty | ||
| 33 | # one moves one place to the left. `karr move "$IDS" todo` with an unset $IDS | ||
| 34 | # arrives as `karr move todo`, reads `todo` as the id, and reports "Task todo | ||
| 35 | # not found" -- an error about a card, for a mistake in the caller's variable. | ||
| 36 | # Elsewhere the loss was worse than misleading rather than merely wrong: | ||
| 37 | # `karr show ""` printed whichever card was touched last and exited 0, and | ||
| 38 | # `karr skill ""` fell through to its default action and installed skill files. | ||
| 39 | # | ||
| 40 | # Refusing takes nothing away that ever worked. An empty argument is dropped in | ||
| 41 | # every position of every command, so no invocation that produced the result | ||
| 42 | # its caller wanted changes meaning -- empty option values (`--claim ""`, | ||
| 43 | # `--body ""`) included, which are dropped by the same rule and today either | ||
| 44 | # eat the following token as the value or fail with "Option claim requires an | ||
| 45 | # argument". Clearing a value by passing an empty one is a feature karr does | ||
| 46 | # not have; if it ever gets one, this guard is where the exception belongs. | ||
| 47 | sub _refuse_empty_argument { | ||
| 48 | my @at = grep { !length $ARGV[$_] } 0 .. $#ARGV; | ||
| 49 | return unless @at; | ||
| 50 | |||
| 51 | # Echo the command line back with the empty slots visible: that is the | ||
| 52 | # whole diagnosis, because the caller is looking for a variable and not for | ||
| 53 | # a card. Whitespace is folded and long values are cut so that a --body | ||
| 54 | # cannot bury the message underneath itself. | ||
| 55 | my $line = join ' ', 'karr', map { | ||
| 56 | my $arg = $_; | ||
| 57 | $arg =~ s/\s+/ /g; | ||
| 58 | $arg = substr( $arg, 0, 37 ) . '...' if length $arg > 40; | ||
| 59 | length $arg && $arg !~ /\s/ ? $arg : "'" . $arg . "'"; | ||
| 60 | } @ARGV; | ||
| 61 | |||
| 62 | my $where = join ', ', map { $_ + 1 } @at; | ||
| 63 | die 'Usage error: ' | ||
| 64 | . ( @at == 1 ? "argument $where is empty" : "arguments $where are empty" ) | ||
| 65 | . ": $line\n" | ||
| 66 | . "An empty argument is dropped before any command sees it, and every\n" | ||
| 67 | . "argument after it moves one place to the left. Check the shell\n" | ||
| 68 | . "variable expanded there -- it is unset or empty.\n"; | ||
| 69 | } | ||
| 70 | |||
| 71 | # An option name with a dash in it does not survive standing behind a boolean | ||
| 72 | # flag, so the flags are respelled with underscores here, before MooX::Cmd and | ||
| 73 | # MooX::Options ever look at argv (ticket #256). | ||
| 74 | # | ||
| 75 | # App::karr::Role::CliArgs/normalize_option_argv carries the whole diagnosis and | ||
| 76 | # the condition under which this call may go again. What belongs here is only | ||
| 77 | # why the CALL is here: the rewrite has to know which token is a flag and which | ||
| 78 | # is the value of the flag in front of it, that answer comes out of the option | ||
| 79 | # table of the command being run, and this is the last point at which the whole | ||
| 80 | # argv is still in one piece. | ||
| 81 | # | ||
| 82 | # Which command that is, is decided exactly the way MooX::Cmd decides it half a | ||
| 83 | # millisecond later: the first argv token that names a command | ||
| 84 | # (MooX::Cmd::Role::_initialize_from_cmd, `first_index` across the WHOLE argv -- | ||
| 85 | # see the %COMMAND_ALIASES comment in App::karr for why the table is asked and | ||
| 86 | # argv is not rewritten). Everything before that token is the root's own argv | ||
| 87 | # and is normalized against App::karr, everything after it against the command | ||
| 88 | # class. The command name itself is passed through untouched, which is what | ||
| 89 | # keeps the dashed command spellings (`get-refs`) findable in the table. | ||
| 90 | sub _normalize_option_argv { | ||
| 91 | my $commands = App::karr->_build_command_commands( {} ); | ||
| 92 | |||
| 93 | my ( $at, $class ); | ||
| 94 | for my $i ( 0 .. $#ARGV ) { | ||
| 95 | next unless defined $commands->{ $ARGV[$i] }; | ||
| 96 | ( $at, $class ) = ( $i, $commands->{ $ARGV[$i] } ); | ||
| 97 | last; | ||
| 98 | } | ||
| 99 | |||
| 100 | my @root = defined $at ? @ARGV[ 0 .. $at - 1 ] : @ARGV; | ||
| 101 | my @name = defined $at ? ( $ARGV[$at] ) : (); | ||
| 102 | my @rest = defined $at ? @ARGV[ $at + 1 .. $#ARGV ] : (); | ||
| 103 | |||
| 104 | # Both calls are class-method calls: normalize_option_argv reads the option | ||
| 105 | # table and nothing else, so it needs no instance -- and no instance exists | ||
| 106 | # yet, which is the point of doing this here. Every App::karr::Cmd::* class | ||
| 107 | # composes App::karr::Role::CliArgs (t/256 pins that, because a command that | ||
| 108 | # forgot to would silently keep the defect for its own dashed options), but | ||
| 109 | # the guard stays: MooX::Cmd's plugin scan decides what a command class is, | ||
| 110 | # not this file. | ||
| 111 | @root = App::karr->normalize_option_argv( \@root ); | ||
| 112 | if ( defined $class ) { | ||
| 113 | use_module($class); | ||
| 114 | @rest = $class->normalize_option_argv( \@rest ) | ||
| 115 | if $class->can('normalize_option_argv'); | ||
| 116 | } | ||
| 117 | |||
| 118 | @ARGV = ( @root, @name, @rest ); | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub dispatch { | ||
| 123 | my (@argv) = @_; | ||
| 124 | |||
| 125 | # dispatch operates on the global @ARGV, exactly as bin/karr did inline: | ||
| 126 | # the two rewrites above and MooX::Cmd::new_with_cmd all read and write it. | ||
| 127 | # Localising it lets an embedding host call dispatch repeatedly, and lets | ||
| 128 | # bin/karr pass its own @ARGV in unchanged. | ||
| 129 | local @ARGV = @argv; | ||
| 130 | |||
| 131 | # The character/octet boundary (ticket #53). Everything the OS hands in is | ||
| 132 | # bytes; everything a command body sees is Perl characters. @ARGV comes in | ||
| 133 | # decoded, STDOUT and STDERR encode on the way out, and no command body | ||
| 134 | # encodes anything itself. STDIN stays raw on purpose -- every reader of it | ||
| 135 | # decodes its own payload (#246). | ||
| 136 | enable_std_utf8(); | ||
| 137 | decode_argv(); | ||
| 138 | |||
| 139 | # The caller's own words, kept for the suggestion line an option-parse error | ||
| 140 | # ends on (ticket k263). Recorded HERE because both rewrites below change | ||
| 141 | # argv and neither leaves what anyone typed: _refuse_empty_argument's | ||
| 142 | # diagnosis reads the raw line, and _normalize_option_argv respells | ||
| 143 | # --claimed-by as --claimed_by and folds a flag-shaped value onto its option | ||
| 144 | # with an `=`. App::karr::Role::ExitCodes reads it back through | ||
| 145 | # App::karr::Error, and prints no suggestion at all where nothing was | ||
| 146 | # recorded. | ||
| 147 | set_original_argv(@ARGV); | ||
| 148 | |||
| 149 | # Inside the eval on purpose: the "Usage error:" marker is what | ||
| 150 | # App::karr::Error::is_usage_error keys on, so the handler below turns this | ||
| 151 | # into exit 2 through the same path as every other usage error. | ||
| 152 | my $ran = eval { _refuse_empty_argument(); _normalize_option_argv(); App::karr->new_with_cmd; 1 }; | ||
| 153 | if ( !$ran ) { | ||
| 154 | my $err = $@; | ||
| 155 | |||
| 156 | # An embedding host's exit-signal (see EMBEDDING) is not a command that | ||
| 157 | # died: hand it back rather than classifying it. Nothing in karr's own | ||
| 158 | # code answers this, so under bin/karr -- where exit() really exits and | ||
| 159 | # never reaches this eval -- it never fires. | ||
| 160 | die $err if blessed($err) && $err->can('__karr_dispatch_exit'); | ||
| 161 | |||
| 162 | # Exit-code contract (ADR 0002): 0 success / 1 runtime failure / 2 usage | ||
| 163 | # error. This is the central handler the ADR calls for: it catches every | ||
| 164 | # uncaught die from a command body and turns it into a deterministic 1 | ||
| 165 | # or 2, replacing the accidental 255 an uncaught die used to leak. | ||
| 166 | # | ||
| 167 | # Usage-error dies carry one of these stable leading markers: | ||
| 168 | # "Unknown command:" the dispatch guard in App::karr | ||
| 169 | # "unexpected extra argument" surplus positionals (Role::CliArgs) | ||
| 170 | # "Usage:" a missing required positional | ||
| 171 | # "Usage error:" anything a command rejects as misuse | ||
| 172 | # that is not one of the shapes above -- | ||
| 173 | # raised via App::karr::Role::ExitCodes' | ||
| 174 | # usage_error(), the generic entry point | ||
| 175 | # for e.g. an out-of-range option value | ||
| 176 | # Any new usage-error die must start with one of these; prefer | ||
| 177 | # usage_error() over inventing a fifth marker. Everything else -- task | ||
| 178 | # not found, board missing, a Git/sync failure, a refused destructive | ||
| 179 | # operation -- is a runtime failure (1). | ||
| 180 | # | ||
| 181 | # The markers themselves live in App::karr::Error::is_usage_error, | ||
| 182 | # because this handler is no longer their only reader: the batch runner | ||
| 183 | # in App::karr::Role::TaskMutation asks the same question to decide | ||
| 184 | # whether a failure belongs to one id or to the whole invocation (#61). | ||
| 185 | # | ||
| 186 | # Option-parse errors (unknown option, unparseable option value) never | ||
| 187 | # reach here: MooX::Options exits 2 directly via | ||
| 188 | # App::karr::Role::ExitCodes (and the root's _print_help), so those | ||
| 189 | # exits bypass this eval. | ||
| 190 | my $is_usage = is_usage_error($err); | ||
| 191 | |||
| 192 | print STDERR $err if defined $err && length $err; | ||
| 193 | exit( $is_usage ? 2 : 1 ); | ||
| 194 | } | ||
| 195 | |||
| 196 | return; | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | 1; | ||
| 202 | |||
| 203 | __END__ | ||