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_jobset.h"
51#include "cli/command_pcb.h"
53#include "cli/command_pcb_drc.h"
69#include "cli/command_fp.h"
73#include "cli/command_sch.h"
74#include "cli/command_sch_erc.h"
76#include "cli/command_sym.h"
80#include "cli/command_version.h"
81#include "cli/exit_codes.h"
82
83// Add this header after all others, to avoid a collision name in a Windows header
84// on mingw.
85#include <wx/app.h>
86
87// a dummy to quiet linking with EDA_BASE_FRAME::config();
88#include <kiface_base.h>
90{
91 // This function should never be called. It is only referenced from
92 // EDA_BASE_FRAME::config() and this is only provided to satisfy the linker,
93 // not to be actually called.
94 wxLogFatalError( wxT( "Unexpected call to Kiface() in kicad/kicad.cpp" ) );
95
96 throw std::logic_error( "Unexpected call to Kiface() in kicad/kicad.cpp" );
97}
98
99
101{
103
104 std::vector<COMMAND_ENTRY> subCommands;
105
106 COMMAND_ENTRY( CLI::COMMAND* aHandler ) : handler( aHandler ){};
107 COMMAND_ENTRY( CLI::COMMAND* aHandler, std::vector<COMMAND_ENTRY> aSub ) :
108 handler( aHandler ), subCommands( aSub ){};
109};
110
153
154
155static std::vector<COMMAND_ENTRY> commandStack = {
156 {
157 &jobsetCmd,
158 {
159 {
161 }
162 }
163 },
164 {
165 &fpCmd,
166 {
167 {
169 {
171 }
172 },
173 {
175 }
176 }
177 },
178 {
179 &pcbCmd,
180 {
181 {
182 &pcbDrcCmd
183 },
184 {
186 },
187 {
189 {
206 }
207 }
208 }
209 },
210 {
211 &schCmd,
212 {
213 {
214 &schErcCmd
215 },
216 {
218 {
227 }
228 }
229 }
230 },
231 {
232 &symCmd,
233 {
234 {
236 {
238 }
239 },
240 {
242 }
243 }
244 },
245 {
246 &versionCmd,
247 }
248};
249
250
251static void recurseArgParserBuild( argparse::ArgumentParser& aArgParser, COMMAND_ENTRY& aEntry )
252{
253 aArgParser.add_subparser( aEntry.handler->GetArgParser() );
254
255 for( COMMAND_ENTRY& subEntry : aEntry.subCommands )
256 {
257 recurseArgParserBuild( aEntry.handler->GetArgParser(), subEntry );
258 }
259}
260
261
262static COMMAND_ENTRY* recurseArgParserSubCommandUsed( argparse::ArgumentParser& aArgParser,
263 COMMAND_ENTRY& aEntry )
264{
265 COMMAND_ENTRY* cliCmd = nullptr;
266
267 if( aArgParser.is_subcommand_used( aEntry.handler->GetName() ) )
268 {
269 for( COMMAND_ENTRY& subentry : aEntry.subCommands )
270 {
271 cliCmd = recurseArgParserSubCommandUsed( aEntry.handler->GetArgParser(), subentry );
272 if( cliCmd )
273 break;
274 }
275
276 if(!cliCmd)
277 cliCmd = &aEntry;
278 }
279
280 return cliCmd;
281}
282
283
284static void printHelp( argparse::ArgumentParser& argParser )
285{
286 std::stringstream ss;
287 ss << argParser;
288 wxPrintf( From_UTF8( ss.str().c_str() ) );
289}
290
291
293{
295 App().SetAppDisplayName( wxT( "kicad-cli" ) );
296
297#if defined( DEBUG )
298 wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
299
300 if( !wxIsAbsolutePath( absoluteArgv0 ) )
301 {
302 wxLogError( wxT( "No meaningful argv[0]" ) );
303 return false;
304 }
305#endif
306
307 if( !InitPgm( true, true) )
308 return false;
309
313 m_bm.Init();
314
315 return true;
316}
317
318
320{
321 argparse::ArgumentParser argParser( std::string( "kicad-cli" ), GetMajorMinorVersion().ToStdString(),
322 argparse::default_arguments::none );
323
324 argParser.add_argument( "-v", ARG_VERSION )
325 .default_value( false )
326 .help( UTF8STDSTR( _( "prints version information and exits" ) ) )
327 .implicit_value( true )
328 .nargs( 0 );
329
330 argParser.add_argument( ARG_HELP_SHORT, ARG_HELP )
331 .default_value( false )
332 .help( UTF8STDSTR( ARG_HELP_DESC ) )
333 .implicit_value( true )
334 .nargs( 0 );
335
336 for( COMMAND_ENTRY& entry : commandStack )
337 {
338 recurseArgParserBuild( argParser, entry );
339 }
340
341 try
342 {
343 // Use the C locale to parse arguments
344 // Otherwise the decimal separator for the locale will be applied
346 argParser.parse_args( m_argcUtf8, m_argvUtf8 );
347 }
348 // std::runtime_error doesn't seem to be enough for the scan<>()
349 catch( const std::exception& err )
350 {
351 wxPrintf( "%s\n", err.what() );
352
353 // find the correct argparser object to output the command usage info
354 COMMAND_ENTRY* cliCmd = nullptr;
355 for( COMMAND_ENTRY& entry : commandStack )
356 {
357 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
358 {
359 cliCmd = recurseArgParserSubCommandUsed( argParser, entry );
360 }
361 }
362
363 // arg parser uses a stream overload for printing the help
364 // we want to intercept so we can wxString the utf8 contents
365 // because on windows our terminal codepage might not be utf8
366 if( cliCmd )
367 cliCmd->handler->PrintHelp();
368 else
369 {
370 printHelp( argParser );
371 }
372
374 }
375
376 if( argParser[ ARG_HELP ] == true )
377 {
378 std::stringstream ss;
379 ss << argParser;
380 wxPrintf( From_UTF8( ss.str().c_str() ) );
381
382 return 0;
383 }
384
385 CLI::COMMAND* cliCmd = nullptr;
386
387 // the version arg gets redirected to the version subcommand
388 if( argParser[ARG_VERSION] == true )
389 {
390 cliCmd = &versionCmd;
391 }
392
393 if( !cliCmd )
394 {
395 for( COMMAND_ENTRY& entry : commandStack )
396 {
397 if( argParser.is_subcommand_used( entry.handler->GetName() ) )
398 {
399 COMMAND_ENTRY* cmdSubEntry = recurseArgParserSubCommandUsed( argParser, entry );
400 if( cmdSubEntry != nullptr )
401 {
402 cliCmd = cmdSubEntry->handler;
403 break;
404 }
405 }
406 }
407 }
408
409 if( cliCmd )
410 {
411 int exitCode = cliCmd->Perform( Kiway );
412
413 if( exitCode != CLI::EXIT_CODES::AVOID_CLOSING )
414 {
415 return exitCode;
416 }
417 else
418 {
419 return 0;
420 }
421 }
422 else
423 {
424 printHelp( argParser );
425
427 }
428}
429
430
432{
434
436 {
438 m_settings_manager->Save();
439 }
440
441 // Destroy everything in PGM_KICAD,
442 // especially wxSingleInstanceCheckerImpl earlier than wxApp and earlier
443 // than static destruction would.
444 Destroy();
445}
446
447
448void PGM_KICAD::MacOpenFile( const wxString& aFileName )
449{
450#if defined( __WXMAC__ )
451 wxFAIL_MSG( "kicad-cli should not call MacOpenFile" );
452#endif
453}
454
455
457{
458 // unlike a normal destructor, this is designed to be called more
459 // than once safely:
460
461 m_bm.End();
462
464}
465
466
468
470
474struct APP_KICAD_CLI : public wxAppConsole
475{
476 APP_KICAD_CLI() : wxAppConsole()
477 {
478 SetPgm( &program );
479
480 // Init the environment each platform wants
482 }
483
484
485 bool OnInit() override
486 {
487 // Perform platform-specific init tasks
488 if( !KIPLATFORM::APP::Init() )
489 return false;
490
491#ifndef DEBUG
492 // Enable logging traces to the console in release build.
493 // This is usually disabled, but it can be useful for users to run to help
494 // debug issues and other problems.
495 if( wxGetEnv( wxS( "KICAD_ENABLE_WXTRACE" ), nullptr ) )
496 {
497 wxLog::EnableLogging( true );
498 wxLog::SetLogLevel( wxLOG_Trace );
499 }
500#endif
501
502 if( !program.OnPgmInit() )
503 {
505 return false;
506 }
507
508 return true;
509 }
510
511 int OnExit() override
512 {
514
515#if defined( __FreeBSD__ )
516 // Avoid wxLog crashing when used in destructors.
517 wxLog::EnableLogging( false );
518#endif
519
520 return wxAppConsole::OnExit();
521 }
522
523 int OnRun() override
524 {
525 try
526 {
527 return program.OnPgmRun();
528 }
529 catch( ... )
530 {
531 Pgm().HandleException( std::current_exception() );
532 }
533
534 return -1;
535 }
536
537 int FilterEvent( wxEvent& aEvent ) override
538 {
539 return Event_Skip;
540 }
541
542#if defined( DEBUG )
546 bool ProcessEvent( wxEvent& aEvent ) override
547 {
548 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
549 {
550 wxKeyEvent* keyEvent = static_cast<wxKeyEvent*>( &aEvent );
551
552 if( keyEvent )
553 {
554 wxLogTrace( kicadTraceKeyEvent, "APP_KICAD::ProcessEvent %s", dump( *keyEvent ) );
555 }
556 }
557
558 aEvent.Skip();
559 return false;
560 }
561
569 bool OnExceptionInMainLoop() override
570 {
571 try
572 {
573 throw;
574 }
575 catch( ... )
576 {
577 Pgm().HandleException( std::current_exception() );
578 }
579
580 return false; // continue on. Return false to abort program
581 }
582#endif
583};
584
585IMPLEMENT_APP_CONSOLE( APP_KICAD_CLI )
586
587
588// The C++ project manager supports one open PROJECT, so Prj() calls within
589// this link image need this function.
591{
592 return Kiway.Prj();
593}
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:284
void OnKiwayEnd()
Definition: kiway.cpp:740
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:182
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:170
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition: pgm_base.cpp:457
char ** m_argvUtf8
Definition: pgm_base.h:438
void BuildArgvUtf8()
Builds the UTF8 based argv variable.
Definition: pgm_base.cpp:412
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:939
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:670
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:64
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:125
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchHpglCmd
Definition: kicad_cli.cpp:140
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchSvgCmd
Definition: kicad_cli.cpp:143
static CLI::PCB_EXPORT_3D_COMMAND exportPcbVrmlCmd
Definition: kicad_cli.cpp:122
static CLI::PCB_EXPORT_DXF_COMMAND exportPcbDxfCmd
Definition: kicad_cli.cpp:117
static CLI::SCH_ERC_COMMAND schErcCmd
Definition: kicad_cli.cpp:135
static CLI::PCB_DRC_COMMAND pcbDrcCmd
Definition: kicad_cli.cpp:114
static CLI::PCB_EXPORT_GERBER_COMMAND exportPcbGerberCmd
Definition: kicad_cli.cpp:128
static CLI::FP_EXPORT_SVG_COMMAND fpExportSvgCmd
Definition: kicad_cli.cpp:146
static CLI::PCB_EXPORT_COMMAND exportPcbCmd
Definition: kicad_cli.cpp:132
static CLI::PCB_RENDER_COMMAND pcbRenderCmd
Definition: kicad_cli.cpp:115
static CLI::SYM_UPGRADE_COMMAND symUpgradeCmd
Definition: kicad_cli.cpp:151
static CLI::FP_EXPORT_COMMAND fpExportCmd
Definition: kicad_cli.cpp:145
static CLI::SCH_EXPORT_PYTHONBOM_COMMAND exportSchPythonBomCmd
Definition: kicad_cli.cpp:137
static CLI::FP_UPGRADE_COMMAND fpUpgradeCmd
Definition: kicad_cli.cpp:147
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPdfCmd
Definition: kicad_cli.cpp:141
static void printHelp(argparse::ArgumentParser &argParser)
Definition: kicad_cli.cpp:284
static CLI::JOBSET_RUN_COMMAND jobsetRunCmd
Definition: kicad_cli.cpp:112
static CLI::PCB_EXPORT_POS_COMMAND exportPcbPosCmd
Definition: kicad_cli.cpp:127
static CLI::SYM_COMMAND symCmd
Definition: kicad_cli.cpp:148
PROJECT & Prj()
Definition: kicad_cli.cpp:590
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStepCmd
Definition: kicad_cli.cpp:119
static CLI::PCB_EXPORT_3D_COMMAND exportPcbPlyCmd
Definition: kicad_cli.cpp:123
static CLI::PCB_EXPORT_GERBERS_COMMAND exportPcbGerbersCmd
Definition: kicad_cli.cpp:129
static PGM_KICAD program
Definition: kicad_cli.cpp:469
static CLI::SCH_EXPORT_BOM_COMMAND exportSchBomCmd
Definition: kicad_cli.cpp:136
static CLI::SCH_COMMAND schCmd
Definition: kicad_cli.cpp:134
static CLI::PCB_COMMAND pcbCmd
Definition: kicad_cli.cpp:113
static std::vector< COMMAND_ENTRY > commandStack
Definition: kicad_cli.cpp:155
static CLI::PCB_EXPORT_DRILL_COMMAND exportPcbDrillCmd
Definition: kicad_cli.cpp:116
static CLI::PCB_EXPORT_IPC2581_COMMAND exportPcbIpc2581Cmd
Definition: kicad_cli.cpp:131
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchDxfCmd
Definition: kicad_cli.cpp:139
static CLI::JOBSET_COMMAND jobsetCmd
Definition: kicad_cli.cpp:111
static CLI::SYM_EXPORT_COMMAND symExportCmd
Definition: kicad_cli.cpp:149
static CLI::SYM_EXPORT_SVG_COMMAND symExportSvgCmd
Definition: kicad_cli.cpp:150
static COMMAND_ENTRY * recurseArgParserSubCommandUsed(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:262
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: kicad_cli.cpp:89
static CLI::VERSION_COMMAND versionCmd
Definition: kicad_cli.cpp:152
static CLI::PCB_EXPORT_3D_COMMAND exportPcbStlCmd
Definition: kicad_cli.cpp:124
static CLI::PCB_EXPORT_PDF_COMMAND exportPcbPdfCmd
Definition: kicad_cli.cpp:126
static CLI::PCB_EXPORT_3D_COMMAND exportPcbXaoCmd
Definition: kicad_cli.cpp:121
static void recurseArgParserBuild(argparse::ArgumentParser &aArgParser, COMMAND_ENTRY &aEntry)
Definition: kicad_cli.cpp:251
static CLI::SCH_EXPORT_NETLIST_COMMAND exportSchNetlistCmd
Definition: kicad_cli.cpp:138
static CLI::SCH_EXPORT_PLOT_COMMAND exportSchPostscriptCmd
Definition: kicad_cli.cpp:142
static CLI::PCB_EXPORT_GENCAD_COMMAND exportPcbGencadCmd
Definition: kicad_cli.cpp:130
static CLI::FP_COMMAND fpCmd
Definition: kicad_cli.cpp:144
static CLI::PCB_EXPORT_3D_COMMAND exportPcbGlbCmd
Definition: kicad_cli.cpp:118
static CLI::SCH_EXPORT_COMMAND exportSchCmd
Definition: kicad_cli.cpp:133
static CLI::PCB_EXPORT_3D_COMMAND exportPcbBrepCmd
Definition: kicad_cli.cpp:120
#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: unix/app.cpp:40
void Init()
Perform environment initialization tasks.
void SetPgm(PGM_BASE *pgm)
Definition: pgm_base.cpp:1072
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
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:475
int OnExit() override
Definition: kicad_cli.cpp:511
bool OnInit() override
Definition: kicad_cli.cpp:485
int FilterEvent(wxEvent &aEvent) override
Definition: kicad_cli.cpp:537
int OnRun() override
Definition: kicad_cli.cpp:523
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:101
COMMAND_ENTRY(CLI::COMMAND *aHandler)
Definition: kicad_cli.cpp:106
COMMAND_ENTRY(CLI::COMMAND *aHandler, std::vector< COMMAND_ENTRY > aSub)
Definition: kicad_cli.cpp:107
CLI::COMMAND * handler
Definition: kicad_cli.cpp:102
std::vector< COMMAND_ENTRY > subCommands
Definition: kicad_cli.cpp:104
System directories search utilities.
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
wxLogTrace helper definitions.