KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_cli.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2004-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2004-2024 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25
26#include <wx/filename.h>
27#include <wx/log.h>
28#include <wx/stdpaths.h>
29#include <wx/wxcrtvararg.h> //for wxPrintf
30
31#include <kiway.h>
32#include <string_utils.h>
33#include <paths.h>
36#include <systemdirsappend.h>
37#include <trace_helpers.h>
38
39#include <stdexcept>
40
41#include "pgm_kicad.h"
42#include "kicad_manager_frame.h"
43
44#include <build_version.h>
45#include <kiplatform/app.h>
47#include <locale_io.h>
48
49#include "cli/command_pcb.h"
51#include "cli/command_pcb_drc.h"
66#include "cli/command_fp.h"
70#include "cli/command_sch.h"
71#include "cli/command_sch_erc.h"
73#include "cli/command_sym.h"
77#include "cli/command_version.h"
78#include "cli/exit_codes.h"
79
80// Add this header after all others, to avoid a collision name in a Windows header
81// on mingw.
82#include <wx/app.h>
83
84// a dummy to quiet linking with EDA_BASE_FRAME::config();
85#include <kiface_base.h>
87{
88 // This function should never be called. It is only referenced from
89 // EDA_BASE_FRAME::config() and this is only provided to satisfy the linker,
90 // not to be actually called.
91 wxLogFatalError( wxT( "Unexpected call to Kiface() in kicad/kicad.cpp" ) );
92
93 throw std::logic_error( "Unexpected call to Kiface() in kicad/kicad.cpp" );
94}
95
96
98{
100
101 std::vector<COMMAND_ENTRY> subCommands;
102
103 COMMAND_ENTRY( CLI::COMMAND* aHandler ) : handler( aHandler ){};
104 COMMAND_ENTRY( CLI::COMMAND* aHandler, std::vector<COMMAND_ENTRY> aSub ) :
105 handler( aHandler ), subCommands( aSub ){};
106};
107
145
146
147static std::vector<COMMAND_ENTRY> commandStack = {
148 {
149 &fpCmd,
150 {
151 {
153 {
155 }
156 },
157 {
159 }
160 }
161 },
162 {
163 &pcbCmd,
164 {
165 {
166 &pcbDrcCmd
167 },
168 {
170 },
171 {
173 {
187 }
188 }
189 }
190 },
191 {
192 &schCmd,
193 {
194 {
195 &schErcCmd
196 },
197 {
199 {
208 }
209 }
210 }
211 },
212 {
213 &symCmd,
214 {
215 {
217 {
219 }
220 },
221 {
223 }
224 }
225 },
226 {
227 &versionCmd,
228 }
229};
230
231
232static void recurseArgParserBuild( argparse::ArgumentParser& aArgParser, COMMAND_ENTRY& aEntry )
233{
234 aArgParser.add_subparser( aEntry.handler->GetArgParser() );
235
236 for( COMMAND_ENTRY& subEntry : aEntry.subCommands )
237 {
238 recurseArgParserBuild( aEntry.handler->GetArgParser(), subEntry );
239 }
240}
241
242
243static COMMAND_ENTRY* recurseArgParserSubCommandUsed( argparse::ArgumentParser& aArgParser,
244 COMMAND_ENTRY& aEntry )
245{
246 COMMAND_ENTRY* cliCmd = nullptr;
247
248 if( aArgParser.is_subcommand_used( aEntry.handler->GetName() ) )
249 {
250 for( COMMAND_ENTRY& subentry : aEntry.subCommands )
251 {
252 cliCmd = recurseArgParserSubCommandUsed( aEntry.handler->GetArgParser(), subentry );
253 if( cliCmd )
254 break;
255 }
256
257 if(!cliCmd)
258 cliCmd = &aEntry;
259 }
260
261 return cliCmd;
262}
263
264
265static void printHelp( argparse::ArgumentParser& argParser )
266{
267 std::stringstream ss;
268 ss << argParser;
269 wxPrintf( From_UTF8( ss.str().c_str() ) );
270}
271
272
274{
276 App().SetAppDisplayName( wxT( "kicad-cli" ) );
277
278#if defined( DEBUG )
279 wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
280
281 if( !wxIsAbsolutePath( absoluteArgv0 ) )
282 {
283 wxLogError( wxT( "No meaningful argv[0]" ) );
284 return false;
285 }
286#endif
287
288 if( !InitPgm( true, true) )
289 return false;
290
294 m_bm.Init();
295
296 return true;
297}
298
299
301{
302 argparse::ArgumentParser argParser( std::string( "kicad-cli" ), GetMajorMinorVersion().ToStdString(),
303 argparse::default_arguments::none );
304
305 argParser.add_argument( "-v", ARG_VERSION )
306 .default_value( false )
307 .help( UTF8STDSTR( _( "prints version information and exits" ) ) )
308 .implicit_value( true )
309 .nargs( 0 );
310
311 argParser.add_argument( ARG_HELP_SHORT, ARG_HELP )
312 .default_value( false )
313 .help( UTF8STDSTR( ARG_HELP_DESC ) )
314 .implicit_value( true )
315 .nargs( 0 );
316
317 for( COMMAND_ENTRY& entry : commandStack )
318 {
319 recurseArgParserBuild( argParser, entry );
320 }
321
322 try
323 {
324 // Use the C locale to parse arguments
325 // Otherwise the decimal separator for the locale will be applied
327 argParser.parse_args( m_argcUtf8, m_argvUtf8 );
328 }
329 // std::runtime_error doesn't seem to be enough for the scan<>()
330 catch( const std::exception& err )
331 {
332 wxPrintf( "%s\n", err.what() );
333
334 // find the correct argparser object to output the command usage info
335 COMMAND_ENTRY* cliCmd = nullptr;
336 for( COMMAND_ENTRY& entry : commandStack )
337 {
338 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
339 {
340 cliCmd = recurseArgParserSubCommandUsed( argParser, entry );
341 }
342 }
343
344 // arg parser uses a stream overload for printing the help
345 // we want to intercept so we can wxString the utf8 contents
346 // because on windows our terminal codepage might not be utf8
347 if( cliCmd )
348 cliCmd->handler->PrintHelp();
349 else
350 {
351 printHelp( argParser );
352 }
353
355 }
356
357 if( argParser[ ARG_HELP ] == true )
358 {
359 std::stringstream ss;
360 ss << argParser;
361 wxPrintf( From_UTF8( ss.str().c_str() ) );
362
363 return 0;
364 }
365
366 CLI::COMMAND* cliCmd = nullptr;
367
368 // the version arg gets redirected to the version subcommand
369 if( argParser[ARG_VERSION] == true )
370 {
371 cliCmd = &versionCmd;
372 }
373
374 if( !cliCmd )
375 {
376 for( COMMAND_ENTRY& entry : commandStack )
377 {
378 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
379 {
380 COMMAND_ENTRY* cmdSubEntry = recurseArgParserSubCommandUsed( argParser, entry );
381 if( cmdSubEntry != nullptr )
382 {
383 cliCmd = cmdSubEntry->handler;
384 break;
385 }
386 }
387 }
388 }
389
390 if( cliCmd )
391 {
392 int exitCode = cliCmd->Perform( Kiway );
393
394 if( exitCode != CLI::EXIT_CODES::AVOID_CLOSING )
395 {
396 return exitCode;
397 }
398 else
399 {
400 return 0;
401 }
402 }
403 else
404 {
405 printHelp( argParser );
406
408 }
409}
410
411
413{
415
417 {
419 m_settings_manager->Save();
420 }
421
422 // Destroy everything in PGM_KICAD,
423 // especially wxSingleInstanceCheckerImpl earlier than wxApp and earlier
424 // than static destruction would.
425 Destroy();
426}
427
428
429void PGM_KICAD::MacOpenFile( const wxString& aFileName )
430{
431#if defined( __WXMAC__ )
432 wxFAIL_MSG( "kicad-cli should not call MacOpenFile" );
433#endif
434}
435
436
438{
439 // unlike a normal destructor, this is designed to be called more
440 // than once safely:
441
442 m_bm.End();
443
445}
446
447
449
451
455struct APP_KICAD_CLI : public wxAppConsole
456{
457 APP_KICAD_CLI() : wxAppConsole()
458 {
459 SetPgm( &program );
460
461 // Init the environment each platform wants
463 }
464
465
466 bool OnInit() override
467 {
468 // Perform platform-specific init tasks
469 if( !KIPLATFORM::APP::Init() )
470 return false;
471
472#ifndef DEBUG
473 // Enable logging traces to the console in release build.
474 // This is usually disabled, but it can be useful for users to run to help
475 // debug issues and other problems.
476 if( wxGetEnv( wxS( "KICAD_ENABLE_WXTRACE" ), nullptr ) )
477 {
478 wxLog::EnableLogging( true );
479 wxLog::SetLogLevel( wxLOG_Trace );
480 }
481#endif
482
483 if( !program.OnPgmInit() )
484 {
486 return false;
487 }
488
489 return true;
490 }
491
492 int OnExit() override
493 {
495
496#if defined( __FreeBSD__ )
497 // Avoid wxLog crashing when used in destructors.
498 wxLog::EnableLogging( false );
499#endif
500
501 return wxAppConsole::OnExit();
502 }
503
504 int OnRun() override
505 {
506 try
507 {
508 return program.OnPgmRun();
509 }
510 catch( ... )
511 {
512 Pgm().HandleException( std::current_exception() );
513 }
514
515 return -1;
516 }
517
518 int FilterEvent( wxEvent& aEvent ) override
519 {
520 return Event_Skip;
521 }
522
523#if defined( DEBUG )
527 bool ProcessEvent( wxEvent& aEvent ) override
528 {
529 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
530 {
531 wxKeyEvent* keyEvent = static_cast<wxKeyEvent*>( &aEvent );
532
533 if( keyEvent )
534 {
535 wxLogTrace( kicadTraceKeyEvent, "APP_KICAD::ProcessEvent %s", dump( *keyEvent ) );
536 }
537 }
538
539 aEvent.Skip();
540 return false;
541 }
542
550 bool OnExceptionInMainLoop() override
551 {
552 try
553 {
554 throw;
555 }
556 catch( ... )
557 {
558 Pgm().HandleException( std::current_exception() );
559 }
560
561 return false; // continue on. Return false to abort program
562 }
563#endif
564};
565
566IMPLEMENT_APP_CONSOLE( APP_KICAD_CLI )
567
568
569// The C++ project manager supports one open PROJECT, so Prj() calls within
570// this link image need this function.
572{
573 return Kiway.Prj();
574}
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
argparse::ArgumentParser & GetArgParser()
Definition: command.h:59
const std::string & GetName() const
Definition: command.h:60
void PrintHelp()
Definition: command.cpp:48
int Perform(KIWAY &aKiway)
Entry point to processing commands from args and doing work.
Definition: command.cpp:56
A KIFACE implementation.
Definition: kiface_base.h:39
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:279
void OnKiwayEnd()
Definition: kiway.cpp:732
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition: kiway.cpp:196
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:181
int m_argcUtf8
argv parameters converted to utf8 form, because wxwidgets has opinions and will return argv as either...
Definition: pgm_base.h:441
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition: pgm_base.h:408
void Destroy()
Definition: pgm_base.cpp:169
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition: pgm_base.cpp:456
char ** m_argvUtf8
Definition: pgm_base.h:438
void BuildArgvUtf8()
Builds the UTF8 based argv variable.
Definition: pgm_base.cpp:411
void HandleException(std::exception_ptr aPtr)
A exception handler to be used at the top level if exceptions bubble up that for.
Definition: pgm_base.cpp:938
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition: pgm_base.cpp:669
PGM_KICAD extends PGM_BASE to bring in FileHistory() and PdfBrowser() which were moved from EDA_APP i...
Definition: pgm_kicad.h:38
bool OnPgmInit()
Definition: kicad.cpp:86
void Destroy()
Definition: kicad.cpp:408
void MacOpenFile(const wxString &aFileName) override
Specific to MacOSX (not used under Linux or Windows).
Definition: kicad.cpp:395
void OnPgmExit()
Definition: kicad.cpp:373
APP_SETTINGS_BASE * PgmSettings()
Definition: pgm_kicad.h:55
int OnPgmRun()
Definition: kicad.cpp:367
BIN_MOD m_bm
Definition: pgm_kicad.h:68
Container for project specific data.
Definition: project.h:62
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
void SetKiway(KIWAY *aKiway)
Associate this setting manager with the given Kiway.
#define ARG_HELP
Definition: command.h:30
#define UTF8STDSTR(s)
Definition: command.h:27
#define ARG_HELP_DESC
Definition: command.h:32
#define ARG_VERSION
Definition: command.h:29
#define ARG_HELP_SHORT
Definition: command.h:31
#define _(s)
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
static CLI::PCB_EXPORT_SVG_COMMAND exportPcbSvgCmd
Definition: kicad_cli.cpp:118
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchHpglCmd
Definition: kicad_cli.cpp:132
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchSvgCmd
Definition: kicad_cli.cpp:135
static CLI::PCB_EXPORT_3D_COMMAND exportPcbVrmlCmd
Definition: kicad_cli.cpp:117
static CLI::PCB_EXPORT_DXF_COMMAND exportPcbDxfCmd
Definition: kicad_cli.cpp:112
static CLI::SCH_ERC_COMMAND schErcCmd
Definition: kicad_cli.cpp:127
static CLI::PCB_DRC_COMMAND pcbDrcCmd
Definition: kicad_cli.cpp:109
static CLI::PCB_EXPORT_GERBER_COMMAND exportPcbGerberCmd
Definition: kicad_cli.cpp:121
static CLI::FP_EXPORT_SVG_COMMAND fpExportSvgCmd
Definition: kicad_cli.cpp:138
static CLI::PCB_EXPORT_COMMAND exportPcbCmd
Definition: kicad_cli.cpp:124
static CLI::PCB_RENDER_COMMAND pcbRenderCmd
Definition: kicad_cli.cpp:110
static CLI::SYM_UPGRADE_COMMAND symUpgradeCmd
Definition: kicad_cli.cpp:143
static CLI::FP_EXPORT_COMMAND fpExportCmd
Definition: kicad_cli.cpp:137
static CLI::SCH_EXPORT_PYTHONBOM_COMMAND exportSchPythonBomCmd
Definition: kicad_cli.cpp:129
static CLI::FP_UPGRADE_COMMAND fpUpgradeCmd
Definition: kicad_cli.cpp:139
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPdfCmd
Definition: kicad_cli.cpp:133
static void printHelp(argparse::ArgumentParser &argParser)
Definition: kicad_cli.cpp:265
static CLI::PCB_EXPORT_POS_COMMAND exportPcbPosCmd
Definition: kicad_cli.cpp:120
static CLI::SYM_COMMAND symCmd
Definition: kicad_cli.cpp:140
PROJECT & Prj()
Definition: kicad_cli.cpp:571
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStepCmd
Definition: kicad_cli.cpp:114
static CLI::PCB_EXPORT_GERBERS_COMMAND exportPcbGerbersCmd
Definition: kicad_cli.cpp:122
static PGM_KICAD program
Definition: kicad_cli.cpp:450
static CLI::SCH_EXPORT_BOM_COMMAND exportSchBomCmd
Definition: kicad_cli.cpp:128
static CLI::SCH_COMMAND schCmd
Definition: kicad_cli.cpp:126
static CLI::PCB_COMMAND pcbCmd
Definition: kicad_cli.cpp:108
static std::vector< COMMAND_ENTRY > commandStack
Definition: kicad_cli.cpp:147
static CLI::PCB_EXPORT_DRILL_COMMAND exportPcbDrillCmd
Definition: kicad_cli.cpp:111
static CLI::PCB_EXPORT_IPC2581_COMMAND exportPcbIpc2581Cmd
Definition: kicad_cli.cpp:123
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchDxfCmd
Definition: kicad_cli.cpp:131
static CLI::SYM_EXPORT_COMMAND symExportCmd
Definition: kicad_cli.cpp:141
static CLI::SYM_EXPORT_SVG_COMMAND symExportSvgCmd
Definition: kicad_cli.cpp:142
static COMMAND_ENTRY * recurseArgParserSubCommandUsed(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:243
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: kicad_cli.cpp:86
static CLI::VERSION_COMMAND versionCmd
Definition: kicad_cli.cpp:144
static CLI::PCB_EXPORT_PDF_COMMAND exportPcbPdfCmd
Definition: kicad_cli.cpp:119
static CLI::PCB_EXPORT_3D_COMMAND exportPcbXaoCmd
Definition: kicad_cli.cpp:116
static void recurseArgParserBuild(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:232
static CLI::SCH_EXPORT_NETLIST_COMMAND exportSchNetlistCmd
Definition: kicad_cli.cpp:130
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPostscriptCmd
Definition: kicad_cli.cpp:134
static CLI::FP_COMMAND fpCmd
Definition: kicad_cli.cpp:136
static CLI::PCB_EXPORT_3D_COMMAND exportPcbGlbCmd
Definition: kicad_cli.cpp:113
static CLI::SCH_EXPORT_COMMAND exportSchCmd
Definition: kicad_cli.cpp:125
static CLI::PCB_EXPORT_3D_COMMAND exportPcbBrepCmd
Definition: kicad_cli.cpp:115
#define KFCTL_CPP_PROJECT_SUITE
Running under C++ project mgr, possibly with others.
Definition: kiway.h:159
#define KFCTL_CLI
Running as CLI app.
Definition: kiway.h:160
static const int ERR_ARGS
Definition: exit_codes.h:31
static const int AVOID_CLOSING
Definition: exit_codes.h:28
bool Init()
Perform application-specific initialization tasks.
Definition: gtk/app.cpp:40
void Init()
Perform environment initialization tasks.
void SetPgm(PGM_BASE *pgm)
Definition: pgm_base.cpp:1071
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
KIWAY Kiway(KFCTL_STANDALONE)
std::vector< FAB_LAYER_COLOR > dummy
wxString From_UTF8(const char *cstring)
Not publicly visible because most of the action is in PGM_KICAD these days.
Definition: kicad_cli.cpp:456
int OnExit() override
Definition: kicad_cli.cpp:492
bool OnInit() override
Definition: kicad_cli.cpp:466
int FilterEvent(wxEvent &aEvent) override
Definition: kicad_cli.cpp:518
int OnRun() override
Definition: kicad_cli.cpp:504
void End()
Definition: bin_mod.cpp:50
void Init()
Definition: bin_mod.cpp:38
void InitSettings(APP_SETTINGS_BASE *aPtr)
Takes ownership of a new application settings object.
Definition: bin_mod.h:53
Definition: kicad_cli.cpp:98
COMMAND_ENTRY(CLI::COMMAND *aHandler)
Definition: kicad_cli.cpp:103
COMMAND_ENTRY(CLI::COMMAND *aHandler, std::vector< COMMAND_ENTRY > aSub)
Definition: kicad_cli.cpp:104
CLI::COMMAND * handler
Definition: kicad_cli.cpp:99
std::vector< COMMAND_ENTRY > subCommands
Definition: kicad_cli.cpp:101
System directories search utilities.
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
wxLogTrace helper definitions.