blib/lib/Langertha/Knarr/CLI.pm

filter: Statementstmt 43.9% 29 / 66 Branchbran 75.0% 6 / 8 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 77.7% 7 / 9 Podpod 50.0% 1 / 2 totaltotal 53.8% 49 / 91 CC 7 SCAR 12.0
CC
7
Cov
50%
normalize_argv
6.1
__ANON__
2.1
execute
2.0
CRAP 3.3
« blib/lib/Langertha/Knarr/Role/UpstreamHTTP.pm ↑ Summary blib/lib/Langertha/Knarr/Protocol/ACP.pm »
1 package Langertha::Knarr::CLI;
2 # ABSTRACT: CLI entry point for Knarr LLM Proxy
3 49 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 49 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 49 ▶use MooX::Options protect_argv => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 49 our $VERSION = '1.102';
9  
10 option config => (
11 is => 'ro',
12 format => 's',
13 short => 'c',
14 doc => 'Config file path (default: ./knarr.yaml)',
15 43 ▶ default => sub { './knarr.yaml' },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 49 );
17  
18  
19 option verbose => (
20 is => 'ro',
21 short => 'v',
22 doc => 'Enable verbose logging (or set KNARR_DEBUG=1)',
23 44 ▶ default => sub { $ENV{KNARR_DEBUG} ? 1 : 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'KNARR_DEBUG'} ? : 0 44 44
24 49 negativable => 1,
25 );
26  
27  
28 # MooX::Options maps a dashed long option (--log-file) to its underscore
29 # attribute (log_file), but its argv rewrite takes the token after a
30 # boolean flag as that flag's value and passes it through unmapped — so
31 # `--from-env --log-file X` reached Getopt::Long as `--log-file` and died
32 # with "Unknown option". Map every long option name up front; arguments
33 # not starting with `--` and everything after a bare `--` are left alone.
34 48 ▶around new_with_cmd => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
35 48 my ( $orig, $class, @args ) = @_;
36 48 local @ARGV = $class->normalize_argv(@ARGV);
37 48 return $class->$orig(@args);
38 49 };
39  
40 48 ▶sub normalize_argv {
Subroutine normalize_argv: called CC=6 SCAR=18.1
Pod documented
41 48 my ( $class, @argv ) = @_;
42 48 my @out;
43 48 ▶ while (@argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 182 48 230
44 182 my $arg = shift @argv;
45 182 ▶ if ( $arg eq '--' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq "--") 0 182 182
46 0 push @out, $arg, @argv;
47 0 last;
48 }
49 182 ▶ if ( my ( $neg, $name, $value ) = $arg =~ /\A--(no-)?([A-Za-z][A-Za-z0-9_-]*)(=.*)?\z/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $arg =~ /\A--(no-)?([A-Za-z][A-Za-z0-9_-]*)(=.*)?\z/s) 23 159 182
50 23 $name =~ tr/-/_/;
51 23 ▶ $arg = '--'.( $neg // '' ).$name.( $value // '' );
Condition100%4/4
$neg // ""
l!l
221
$value // ""
l!l
419
MC/DC100%1/1
$neg // ""
$neg
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $neg
Aresult
11
00
Truth table100%2/2
A
A= $value
Aresult
11
00
52 }
53 182 push @out, $arg;
54 }
55 48 return @out;
56 }
57  
58  
59 0 ▶sub execute {
Subroutine execute: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my ($self) = @_;
61 0 print _banner();
62 0 print "\n";
63 0 print "USAGE\n";
64 0 print " knarr <command> [options]\n\n";
65 0 print "COMMANDS\n";
66 0 print " start Start the proxy server\n";
67 0 print " init Scan environment and generate configuration\n";
68 0 print " models List configured models and their backends\n";
69 0 print " check Validate configuration file\n\n";
70 0 print "GLOBAL OPTIONS\n";
71 0 print " -c, --config <path> Config file (default: ./knarr.yaml)\n";
72 0 print " -v, --verbose Enable verbose logging\n\n";
73 0 print "QUICK START (Docker)\n";
74 0 print " docker run -e OPENAI_API_KEY=sk-... -p 8080:8080 raudssus/langertha-knarr\n\n";
75 0 print "QUICK START (Local)\n";
76 0 print " knarr init > knarr.yaml\n";
77 0 print " knarr start\n\n";
78 0 print "EXAMPLES\n";
79 0 print " knarr start # Start with ./knarr.yaml\n";
80 0 print " knarr start -c production.yaml -p 9090 # Custom config and port\n";
81 0 print " knarr start --from-env # Auto-detect from ENV\n";
82 0 print " knarr start --from-env -p 8080 -p 11434 # ENV config, custom ports\n";
83 0 print " knarr init > knarr.yaml # Generate config\n";
84 0 print " knarr models # List configured models\n";
85 0 print " knarr check # Validate config\n\n";
86 0 print "ENVIRONMENT\n";
87 0 print " OPENAI_API_KEY OpenAI API key\n";
88 0 print " ANTHROPIC_API_KEY Anthropic API key\n";
89 0 print " LANGFUSE_PUBLIC_KEY Langfuse public key (enables tracing)\n";
90 0 print " LANGFUSE_SECRET_KEY Langfuse secret key\n";
91 0 print " LANGFUSE_URL Langfuse URL (default: https://cloud.langfuse.com)\n";
92 0 print " KNARR_API_KEY Proxy authentication key (optional)\n\n";
93 0 print "Version $VERSION | https://github.com/Getty/langertha-knarr\n";
94 }
95  
96 0 ▶sub _banner {
Subroutine _banner: not called CC=1 SCAR=6.9
97 0 return <<'BANNER';
98 . * .
99 . _/|_ . KNARR
100 . /| |\ . Langertha LLM Proxy
101 ~~~~~|______|~~~~~
102 ~~ ~~~~~~~~~~~~~ ~~ Cargo transport for your LLM calls
103 ~~~~~~~~~~~~~~~~~~~~
104 BANNER
105 }
106  
107 49 1;
108  
109 __END__
110  
111 =pod
112  
113 =encoding UTF-8
114  
115 =head1 NAME
116  
117 Langertha::Knarr::CLI - CLI entry point for Knarr LLM Proxy
118  
119 =head1 VERSION
120  
121 version 1.102
122  
123 =head1 DESCRIPTION
124  
125 MooX::Cmd entry point for the C<knarr> CLI. Dispatches to subcommand classes
126 under C<Langertha::Knarr::CLI::Cmd::*>. When invoked without a subcommand,
127 prints a banner and usage summary.
128  
129 For full CLI documentation see L<knarr> and L<Langertha::Knarr>.
130  
131 =head1 SEE ALSO
132  
133 =over
134  
135 =item * L<knarr> — CLI synopsis and option reference
136  
137 =item * L<Langertha::Knarr> — Full documentation
138  
139 =item * L<Langertha::Knarr::CLI::Cmd::Start> — C<knarr start>
140  
141 =item * L<Langertha::Knarr::CLI::Cmd::Init> — C<knarr init>
142  
143 =item * L<Langertha::Knarr::CLI::Cmd::Models> — C<knarr models>
144  
145 =item * L<Langertha::Knarr::CLI::Cmd::Check> — C<knarr check>
146  
147 =back
148  
149 =head2 --config
150  
151 Path to the YAML configuration file. Short form: C<-c>. Defaults to
152 C<./knarr.yaml>. Accepted before the subcommand (C<knarr -c prod.yaml
153 start>) and after C<start>, C<check> and C<models> (C<knarr start -c
154 prod.yaml>, see L<Langertha::Knarr::CLI::Role::GlobalOptions>); a value
155 after the subcommand wins.
156  
157 =head2 --verbose
158  
159 Enable verbose logging to stderr. Short form: C<-v>. Also enabled by
160 C<KNARR_DEBUG=1>. Accepted before the subcommand and after C<start>,
161 C<check> and C<models>, like L</--config>.
162  
163 =head2 normalize_argv
164  
165 my @argv = Langertha::Knarr::CLI->normalize_argv(@ARGV);
166  
167 Rewrites long option names from their dashed to their underscore spelling
168 (C<--log-file=x> becomes C<--log_file=x>, C<--no-verbose> keeps its C<no->
169 prefix). Arguments that do not start with C<--> (short options and option
170 values) and everything after a bare C<--> are left untouched.
171 C<new_with_cmd> applies it to C<@ARGV> before parsing, so every documented
172 dashed option works in any position.
173  
174 =head1 SUPPORT
175  
176 =head2 Issues
177  
178 Please report bugs and feature requests on GitHub at
179 L<https://github.com/Getty/langertha-knarr/issues>.
180  
181 =head2 IRC
182  
183 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
184  
185 =head1 CONTRIBUTING
186  
187 Contributions are welcome! Please fork the repository and submit a pull request.
188  
189 =head1 AUTHOR
190  
191 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
192  
193 =head1 COPYRIGHT AND LICENSE
194  
195 This software is copyright (c) 2026 by Torsten Raudssus.
196  
197 This is free software; you can redistribute it and/or modify it under
198 the same terms as the Perl 5 programming language system itself.
199  
200 =cut
« blib/lib/Langertha/Knarr/Role/UpstreamHTTP.pm ↑ Summary blib/lib/Langertha/Knarr/Protocol/ACP.pm »